Json特殊格式解析(动态解析)

时间:2025-03-19 17:36:29

动态变化的key

{
    "0": {
        "title": "冯巩王祖贤明星惊人的身世之谜",
        "description": "冯巩王祖贤明星惊人的身世之谜...",
        "picUrl": "/upload/picture/39/",
        "url": "/read/4/11652/"
    },
    "1": {
        "title": "西西里巴勒莫唯一的地下墓穴",
        "description": "西西里巴勒莫唯一的地下墓穴...",
        "picUrl": "/upload/picture/37/",
        "url": "/read/4/10968/"
    },
    "2": {
        "title": "神秘的北纬30度为何如此诡异?",
        "description": "神秘的北纬30度为何如此诡异?...",
        "picUrl": "/upload/picture/35/",
        "url": "/read/4/10462/"
    },
    "3": {
        "title": "秦陵又出怪事了:惊天秘事内幕",
        "description": "秦陵又出怪事了:惊天秘事内幕...",
        "picUrl": "/upload/picture/37/",
        "url": "/read/4/11017/"
    },
    "code": 200,
    "msg": "ok"
}
解析

void praseJson1(String str) throws Exception {
         obj = new JSONObject(str);
        Iterator<String> keys = ();
        while (()) {
            String key = ();
            JSONObject object = (key);
            String title = ("title");
            String description = ("description");
            String picUrl = ("picUrl");
            String url = ("url");
        }
    }

无key的数组解析

{
    "schema": "/example",
    "required": [
        "id",
        "name"
    ]
}
void praseJson1(String str) throws Exception {
         obj = new JSONObject(str);
        String schema = ("schema");
        JSONArray array = ("required");
        ArrayMap<String, String> arrayMap = new ArrayMap<>(2);
        for (int i = 0; i < (); i++) {
            String value = (i);
            (value, value);  //use a map to store the values
        }
    }