String JSONObject JSONArray 转换

Posted ljangle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了String JSONObject JSONArray 转换相关的知识,希望对你有一定的参考价值。

JSON使用阿里的fastJson为依赖包

gradle依赖管理如下:

    compile group: ‘com.alibaba‘, name: ‘fastjson‘, version:‘1.2.41‘

 

1、String转JSONObject

前言:String 是JSONObject格式的字符串

eg:技术图片

JSONObject jSONObject = JSONObject.parseObject(String);

 

2、String转JSONArray

前言:String 是JSONArray格式的字符串

eg:技术图片

JSONArray jsonArray= JSONArray.parseArray(String);

 

 

3、JSONObject中的数组提取为JSONArray

eg:


    "AreaName": "北京",
    "CityId": 110100,
    "NoMarket": false,
    "OldCityId": 646,
    "Pinyin": "beijing",
    "ProvinceId": 110000,
    "Result": [
        
            "ItemName": "优惠",
            "ItemUrl": "/list/a646c12-1.html",
            "Title": "Stelvio 钜惠23.4万起",
            "Url": "//www.autohome.com.cn/market/201904/100223763.html"
        ,
        
            "ItemName": "优惠",
            "ItemUrl": "/list/a646c12-1.html",
            "Title": "马驹桥林肯中心年中大促",
            "Url": "//www.autohome.com.cn/market/201906/100230932.html"
        ,
        
            "ItemName": "优惠",
            "ItemUrl": "/list/a646c12-1.html",
            "Title": "星越平价销售13.58万元起",
            "Url": "//www.autohome.com.cn/dealer/201906/367011492.html"
        ,
        
            "ItemName": "优惠",
            "ItemUrl": "/list/a646c12-1.html",
            "Title": "哈弗F5限时优惠8000元",
            "Url": "//www.autohome.com.cn/dealer/201906/366897778.html"
        ,
        
            "ItemName": "优惠",
            "ItemUrl": "/list/a646c12-1.html",
            "Title": "购元新能源价格暂无优惠",
            "Url": "//www.autohome.com.cn/dealer/201906/366897034.html"
        ,
        
            "ItemName": "优惠",
            "ItemUrl": "/list/a646c12-1.html",
            "Title": "瑞虎3xe冰点价促销中!",
            "Url": "//www.autohome.com.cn/dealer/201906/366889724.html"
        ,
        
            "ItemName": "优惠",
            "ItemUrl": "/list/a646c12-1.html",
            "Title": "购奔奔EV现钜惠5.1万元",
            "Url": "//www.autohome.com.cn/dealer/201906/366843204.html"
        ,
        
            "ItemName": "优惠",
            "ItemUrl": "/list/a646c12-1.html",
            "Title": "购宝马7系价格暂无优惠",
            "Url": "//www.autohome.com.cn/dealer/201906/366588080.html"
        ,
        
            "ItemName": "预定",
            "ItemUrl": "/list/a646c14-1.html",
            "Title": "途观L价格直降7.6万元",
            "Url": "//www.autohome.com.cn/dealer/201906/366568937.html"
        ,
        
            "ItemName": "预定",
            "ItemUrl": "/list/a646c14-1.html",
            "Title": "购凯迪拉克XTS降8万",
            "Url": "//www.autohome.com.cn/dealer/201906/366500646.html"
        ,
        
            "ItemName": "预定",
            "ItemUrl": "/list/a646c14-1.html",
            "Title": "汉兰达可试驾购车无优惠",
            "Url": "//www.autohome.com.cn/dealer/201906/366384207.html"
        ,
        
            "ItemName": "预定",
            "ItemUrl": "/list/a646c14-1.html",
            "Title": "宝马M4价格稳定无优惠",
            "Url": "//www.autohome.com.cn/dealer/201906/366156789.html"
        ,
        
            "ItemName": "预定",
            "ItemUrl": "/list/a646c14-1.html",
            "Title": "奥迪A8促销直降26.33万元",
            "Url": "//www.autohome.com.cn/dealer/201906/366925378.html"
        ,
        
            "ItemName": "预定",
            "ItemUrl": "/list/a646c14-1.html",
            "Title": "英菲尼迪Q50L可降6.3万",
            "Url": "//www.autohome.com.cn/dealer/201906/366863516.html"
        ,
        
            "ItemName": "预定",
            "ItemUrl": "/list/a646c14-1.html",
            "Title": "帝豪新能源价格降8.25万",
            "Url": "//www.autohome.com.cn/dealer/201906/366877669.html"
        ,
        
            "ItemName": "预定",
            "ItemUrl": "/list/a646c14-1.html",
            "Title": "撼路者在售现钜惠5万",
            "Url": "//www.autohome.com.cn/dealer/201906/366912121.html"
        
    ]

技术图片

提取Result对应的数组

JSONArray jsonArray= jsonObject.getJSONArray("Result");

 

4、JSONArray提取为JSONObject

eg:技术图片

JSONObject jsonObject = jsonArray.getJSONObject(0);

 

5、JSONObject获取value

1、object.getString("key")

2、object.get("key")

 

6、获取JSONObject的ket value

        JSONArray dateArr = new JSONArray();
        Set<String> key = dateArr .keySet();
        for (String keyObj:key) 
            JSONArray hisData = history.getJSONArray(keyObj);           
        

 

7、遍历JSONArray

第一种for循环
                JSONArray seriesArr = new JSONArray();
                for(int i=0;i<seriesArr .size();i++)
                    JSONObject object = eggsArr.getJSONObject(i);
                


第二种for增强
                JSONArray pzListArr = new JSONArray();
                for (Object obj:pzListArr) 
                    JSONObject dataObj = JSONObject.parseObject(obj.toString());
                    

 

 

8、javaBean转为JSONObject

未完待续······

 

以上是关于String JSONObject JSONArray 转换的主要内容,如果未能解决你的问题,请参考以下文章

String JSONObject JSONArray 转换

java fastjson:Map与json以及JSONObject ,JSONObject与String互转

jsonObject和JsonArray转化String

Gson:直接将String转换为JsonObject(无POJO)

Spring - HttpMessageConversionException 使用 ResponseEntity 和 Map<String,JSONobject>

String转JSONObject