煎饼

煎饼为你分享Android有关的技术文章
不断分享,点滴积累,共同提高

关注微信公众号[developers]
更快的了解新的技术动态

Json特殊格式解析

例1:动态变化的key

Json数据:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
    "0": {
        "title": "冯巩王祖贤明星惊人的身世之谜",
        "description": "冯巩王祖贤明星惊人的身世之谜...",
        "picUrl": "http://img521.lieqi.com/upload/picture/39/11652.jpg",
        "url": "http://www.lieqi.com/read/4/11652/"
    },
    "1": {
        "title": "西西里巴勒莫唯一的地下墓穴",
        "description": "西西里巴勒莫唯一的地下墓穴...",
        "picUrl": "http://img521.lieqi.com/upload/picture/37/10968.jpg",
        "url": "http://www.lieqi.com/read/4/10968/"
    },
    "2": {
        "title": "神秘的北纬30度为何如此诡异?",
        "description": "神秘的北纬30度为何如此诡异?...",
        "picUrl": "http://img521.lieqi.com/upload/picture/35/10462.jpg",
        "url": "http://www.lieqi.com/read/4/10462/"
    },
    "3": {
        "title": "秦陵又出怪事了:惊天秘事内幕",
        "description": "秦陵又出怪事了:惊天秘事内幕...",
        "picUrl": "http://img521.lieqi.com/upload/picture/37/11017.jpg",
        "url": "http://www.lieqi.com/read/4/11017/"
    },
    "code": 200,
    "msg": "ok"
}

Java解析:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
   void praseJson1(String str) throws Exception {
        org.json.JSONObject obj = new JSONObject(str);
        Iterator<String> keys = obj.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            JSONObject object = obj.getJSONObject(key);
            String title = object.getString("title");
            String description = object.getString("description");
            String picUrl = object.getString("picUrl");
            String url = object.getString("url");
        }
    }

例2:无key的数组解析

Json数据:

1
2
3
4
5
6
7
{
    "schema": "http://json.org/example",
    "required": [
        "id",
        "name"
    ]
}

Java解析:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
void praseJson1(String str) throws Exception {
        org.json.JSONObject obj = new JSONObject(str);
        String schema = obj.getString("schema");
        JSONArray array = obj.getJSONArray("required");
        ArrayMap<String, String> arrayMap = new ArrayMap<>(2);
        for (int i = 0; i < array.length(); i++) {
            String value = array.getString(i);
            arrayMap.put(value, value);  //use a map to store the values
        }
    }
354

分享本文:

Gradle Tips2:使用archivesBaseName改变Apk名称