jsonobject如何转jsonarray?

阳光 2021-03-01 21:31:18 java常见问答 10386

在JSON知识中,有很多字符串是可以相互转换的,例如String转JSONObject、jsonobject和jsonarray,那么大家知道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"
}
        
]
}

jsonobject如何转jsonarray?.png

提取Result对应的数组

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

jsonobject和jsonarray的区别是什么?

JSONObject的数据是用 { } 来表示的:

例如:{ "id" : "123", "courseID" : "huangt-test", "title" : "提交作业", "content" : null }

而JSONArray,顾名思义是由JSONObject构成的数组,用[ { } , { } , ...... , { } ]来表示:

例如:[{ "id" : "123", "courseID" : "huangt-test", "title" : "提交作业" },{ "content" : null, "beginTime" : 1398873600000 "endTime" } ] ;

表示了包含2个JSONObject的JSONArray。可以看到一个很明显的区别,一个用的是 { } ,一个最外面用的是 [ ] ;

jsonobject和jsonarray之间的转换还是有些繁琐的,不过只要清楚逻辑思维就好了,另外,jsonobject和jsonarray的区别也是一目了然的,JSONObject是一个对象,而JSONArray是一个数组。最后大家如果想要了解更多json相关知识,敬请关注奇Q工具网。

推荐阅读:

java怎么做客户端?实例分享

eclipse字体放大缩小快捷键是什么?

json文件怎么创建?json有哪些形式?