Java代码如何快速解析JSON字符串,Java解析json字符串,逻辑清晰一看就懂

以下举例数据结构:

{

    "error": 0,

    "status": "success",

    "results": [

        {

            "currentCity": "青岛",

            "index": [

                {

                    "title": "穿衣",

                    "zs": "较冷",

                    "tipt": "穿衣指数",

                    "des": "建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"

                },

                {

                    "title": "紫外线强度",

                    "zs": "中等",

                    "tipt": "紫外线强度指数",

                    "des": "属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。"

                }

            ]

        }

    ]

}

简单来说:

“key”:“value” --> 此时value为String

“key":0 --> 此时value为int

“key”:{“k1”:“v1”} --> 此时value为JSONObject

“key”:[v] --> 此时value为JSONArray

解析代码:

public class Test {



public static void main(String[] args) {



    String s = "{\"error\":0,\"status\":\"success\",\"results\":[{\"currentCity\":\"青岛\",\"index\":[{\"title\":\"穿衣\",\"zs\":\"较冷\",\"tipt\":\"穿衣指数\",\"des\":\"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。\"},{\"title\":\"紫外线强度\",\"zs\":\"最弱\",\"tipt\":\"紫外线强度指数\",\"des\":\"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。\"}],}]}";


    //也可以用alibaba的JSON对象替换,JSON.pareObject(s),相应的下边的方法也有所不同

    JSONObject jsonObject = JSONObject.fromObject(s);



    //提取出error为 0

    int error = jsonObject.getInt("error");

    System.out.println("error:" + error);



    //提取出status为 success

    String status = jsonObject.getString("status");

    System.out.println("status:" + status);   



    //注意:results中的内容带有中括号[],所以要转化为JSONArray类型的对象

    JSONArray result = jsonObject.getJSONArray("results");



    for (int i = 0; i < result.size(); i++) {

        //提取出currentCity为 青岛

        String currentCity = result.getJSONObject(i).getString("currentCity");

        System.out.println("currentCity:" + currentCity);



        //注意:index中的内容带有中括号[],所以要转化为JSONArray类型的对象

        JSONArray index = result.getJSONObject(i).getJSONArray("index");



        for (int j = 0; j < index.size(); j++) {

            String title = index.getJSONObject(j).getString("title");

            System.out.println("title:" + title);

            String zs = index.getJSONObject(j).getString("zs");

            System.out.println("zs:" + zs);

            String tipt = index.getJSONObject(j).getString("tipt");

            System.out.println("tipt:" + tipt);

            String des = index.getJSONObject(j).getString("des");

            System.out.println("des:" + des);

        }

    }

}

}

当解析json串如下,我们要获取数据该如何解析:

"index": {

                "79002":{

                    "title": "穿衣",

                    "zs": "较冷",

                    "tipt": "穿衣指数",

                    "des": "建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"

                },

                "14314":{

                    "title": "紫外线强度",

                    "zs": "中等",

                    "tipt": "紫外线强度指数",

                    "des": "属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。"

                },
                .....

            }

        可以看到key-value中的key是不固定的,我们要如何获取所有数据请看下边的代码:

    public static void main(String[] args) {
        String s="\"index\": { \"79002\":{ \"title\": \"穿衣\", \"zs\": \"较冷\", \"tipt\": \"穿衣指数\", \"des\": \"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。\" }, \"14314\":{ \"title\": \"紫外线强度\", \"zs\": \"中等\", \"tipt\": \"紫外线强度指数\", \"des\": \"属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。\" }, ..... }";
        JSONObject indexParse= JSON.parseObject(s);
        JSONObject data=indexParse.getJSONObject("index");
        for (Map.Entry entry : data.entrySet()) {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }

    }

你可能感兴趣的:(Java开发百花筒,json,java)