欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Java中String JSONObject JSONArray List<實(shí)體類>轉(zhuǎn)換

 更新時(shí)間:2020年11月13日 09:37:26   作者:進(jìn)階仙女  
這篇文章主要介紹了詳解String JSONObject JSONArray List<實(shí)體類>轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

JSON使用阿里的fastJson為依賴包

gradle依賴管理如下:

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

1、String轉(zhuǎn)JSONObject

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

eg:

JSONObject jSONObject = JSONObject.parseObject(String);

2、String轉(zhuǎn)JSONArray

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

eg:

JSONArray jsonArray= JSONArray.parseArray(String);

3、JSONObject中的數(shù)組提取為JSONArray

eg:

{
  "AreaName": "北京",
  "CityId": 110100,
  "NoMarket": false,
  "OldCityId": 646,
  "Pinyin": "beijing",
  "ProvinceId": 110000,
  "Result": [
    {
      "ItemName": "優(yōu)惠",
      "ItemUrl": "/list/a646c12-1.html",
      "Title": "Stelvio 鉅惠23.4萬起",
      "Url": "http://www.autohome.com.cn/market/201904/100223763.html"
    },
    {
      "ItemName": "優(yōu)惠",
      "ItemUrl": "/list/a646c12-1.html",
      "Title": "馬駒橋林肯中心年中大促",
      "Url": "http://www.autohome.com.cn/market/201906/100230932.html"
    },
    {
      "ItemName": "優(yōu)惠",
      "ItemUrl": "/list/a646c12-1.html",
      "Title": "星越平價(jià)銷售13.58萬元起",
      "Url": "http://www.autohome.com.cn/dealer/201906/367011492.html"
    },
    {
      "ItemName": "優(yōu)惠",
      "ItemUrl": "/list/a646c12-1.html",
      "Title": "哈弗F5限時(shí)優(yōu)惠8000元",
      "Url": "http://www.autohome.com.cn/dealer/201906/366897778.html"
    },
    {
      "ItemName": "優(yōu)惠",
      "ItemUrl": "/list/a646c12-1.html",
      "Title": "購元新能源價(jià)格暫無優(yōu)惠",
      "Url": "http://www.autohome.com.cn/dealer/201906/366897034.html"
    },
    {
      "ItemName": "優(yōu)惠",
      "ItemUrl": "/list/a646c12-1.html",
      "Title": "瑞虎3xe冰點(diǎn)價(jià)促銷中!",
      "Url": "http://www.autohome.com.cn/dealer/201906/366889724.html"
    },
    {
      "ItemName": "優(yōu)惠",
      "ItemUrl": "/list/a646c12-1.html",
      "Title": "購奔奔EV現(xiàn)鉅惠5.1萬元",
      "Url": "http://www.autohome.com.cn/dealer/201906/366843204.html"
    },
    {
      "ItemName": "優(yōu)惠",
      "ItemUrl": "/list/a646c12-1.html",
      "Title": "購寶馬7系價(jià)格暫無優(yōu)惠",
      "Url": "http://www.autohome.com.cn/dealer/201906/366588080.html"
    },
    {
      "ItemName": "預(yù)定",
      "ItemUrl": "/list/a646c14-1.html",
      "Title": "途觀L價(jià)格直降7.6萬元",
      "Url": "http://www.autohome.com.cn/dealer/201906/366568937.html"
    },
    {
      "ItemName": "預(yù)定",
      "ItemUrl": "/list/a646c14-1.html",
      "Title": "購凱迪拉克XTS降8萬",
      "Url": "http://www.autohome.com.cn/dealer/201906/366500646.html"
    },
    {
      "ItemName": "預(yù)定",
      "ItemUrl": "/list/a646c14-1.html",
      "Title": "漢蘭達(dá)可試駕購車無優(yōu)惠",
      "Url": "http://www.autohome.com.cn/dealer/201906/366384207.html"
    },
    {
      "ItemName": "預(yù)定",
      "ItemUrl": "/list/a646c14-1.html",
      "Title": "寶馬M4價(jià)格穩(wěn)定無優(yōu)惠",
      "Url": "http://www.autohome.com.cn/dealer/201906/366156789.html"
    },
    {
      "ItemName": "預(yù)定",
      "ItemUrl": "/list/a646c14-1.html",
      "Title": "奧迪A8促銷直降26.33萬元",
      "Url": "http://www.autohome.com.cn/dealer/201906/366925378.html"
    },
    {
      "ItemName": "預(yù)定",
      "ItemUrl": "/list/a646c14-1.html",
      "Title": "英菲尼迪Q50L可降6.3萬",
      "Url": "http://www.autohome.com.cn/dealer/201906/366863516.html"
    },
    {
      "ItemName": "預(yù)定",
      "ItemUrl": "/list/a646c14-1.html",
      "Title": "帝豪新能源價(jià)格降8.25萬",
      "Url": "http://www.autohome.com.cn/dealer/201906/366877669.html"
    },
    {
      "ItemName": "預(yù)定",
      "ItemUrl": "/list/a646c14-1.html",
      "Title": "撼路者在售現(xiàn)鉅惠5萬",
      "Url": "http://www.autohome.com.cn/dealer/201906/366912121.html"
    }
  ]
}

提取Result對應(yīng)的數(shù)組

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循環(huán)
        JSONArray seriesArr = new JSONArray();
        for(int i=0;i<seriesArr .size();i++){
          JSONObject object = eggsArr.getJSONObject(i);
        }


第二種for增強(qiáng)
        JSONArray pzListArr = new JSONArray();
        for (Object obj:pzListArr) {
          JSONObject dataObj = JSONObject.parseObject(obj.toString());
        }

8、

Map<String, Object> paraMap = new HashMap<String, Object>();

JSONObject.toJSONString(paraMap)

自動過濾參數(shù)為null的數(shù)值

8、javaBean轉(zhuǎn)為JSONObject

未完待續(xù)······

9、List<實(shí)體類>轉(zhuǎn)String

import com.alibaba.fastjson.JSONObject;


List<實(shí)體類> value1 = 。。。。。。
JSONObject.toJSONString(value1 )10、JSONArray轉(zhuǎn)List<實(shí)體類>

看你開心用哪個(gè),object和array的區(qū)別沒有細(xì)究

10、JSONArray轉(zhuǎn)List<實(shí)體類>

import com.alibaba.fastjson.JSONArray;
JSONArray objects = JSONArray.parseArray(categoryConstantInfoFromRedis);
List<實(shí)體類> categoryConstantInfos = objects.toJavaList(實(shí)體類名.class);
眾里尋他千百度?。。oJavaList

找不到方法的時(shí)候,去看看JSONArray,JSONObject的源碼,很多都有封裝好的,你不會失望的

到此這篇關(guān)于詳解Java中String JSONObject JSONArray List<實(shí)體類>轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)String JSONObject JSONArray List<實(shí)體類>轉(zhuǎn)換 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論