JOSNObject与JSONArray的关系

2023-05-21,,

这里简单介绍下JOSNObjectJSONArray的关系。

JOSNObject:json对象      用{}表示

JSONArray:json数组       用 [ ] 表示

服务器返回的json基本是这两种形式的搭配使用,他们之间可以互相嵌套使用,使用起来比较简单,不多说,上图:

private void text() throws Exception{

    //对象
    JSONObject jsonObject1=new JSONObject();
    jsonObject1.put("String","字符串");
    jsonObject1.put("key",110);
    jsonObject1.put("hello",false);
    //对象
    JSONObject jsonObject2=new JSONObject();
    jsonObject2.put("ok",35.66);
    jsonObject2.put("hi",new String());

    //数组
    JSONArray array = new JSONArray();
    array.put(jsonObject1);
    array.put(jsonObject2);
    //对象
    JSONObject jsonObject3=new JSONObject();
    jsonObject3.put("obj",array);

    Log.e("TAG","jsonObject3="+jsonObject3.toString());
}

以下为输出结果:

jsonObject3={"obj":[{"hello":false,"key":110,"String":"字符串"},{"ok":35.66,"hi":""}]}

《JOSNObject与JSONArray的关系.doc》

下载本文的Word格式文档,以方便收藏与打印。