亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

淺談在Java中JSON的多種使用方式

 更新時(shí)間:2021年01月18日 08:53:48   作者:LeeShaoQing  
這篇文章主要介紹了淺談在Java中JSON的多種使用方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1. 常用的JSON轉(zhuǎn)換

 JSONObject 轉(zhuǎn) JSON 字符串

JSONObject json = new JSONObject();
jsonObject.put("name", "test");
String str = JSONObject.toJSONString(json);

JSON字符串轉(zhuǎn)JSON

String str = "{\"name\":\"test\"}";
JSONObject json = JSONObject.parseObject(str);

實(shí)體類轉(zhuǎn)JSON

Test test = new Test();
test.setName("test");
String testStr = JSONObject.toJSONString(test);
JSONObject json = JSONObject.parseObject(testStr);

Map轉(zhuǎn)JSON

JSONObject json = JSONObject.parseObject(JSON.toJSONString(map));

JSON轉(zhuǎn)Map

Map jsonToMap = JSONObject.parseObject(jsonObject.toJSONString()); 

2. 將多個(gè)JSON合并一個(gè)

JSONObject totalJSON = new JSONObject();
totalJSON.putAll(json1);
totalJSON.putAll(json2);

json1,json2 為JSONObject。 最終的代碼格式:

{
 json1:{},
 json2:{}
}

3.JSON拆分

不同的需求有不同的做法,以下提供兩種解決思路

  • 定義兩個(gè)或多個(gè)JSON進(jìn)行put和remove 比如明確需要哪些字段的時(shí)候可以定義一個(gè)數(shù)組用來存放key信息。存放和刪除的時(shí)候只需要遍歷數(shù)組就可以。
  • 遍歷JSON,獲取key,value再重新put

4.JSON遍歷

定義一個(gè)工具類,獲取key和value

if(object instanceof JSONObject) {
  JSONObject jsonObject = (JSONObject) object;
  for (Map.Entry<String, Object> entry: jsonObject.entrySet()) {
    Object o = entry.getValue();
    if(o instanceof String) {
      System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue());
    } else {
      jsonLoop(o);
    }
  }
}
if(object instanceof JSONArray) {
  JSONArray jsonArray = (JSONArray) object;
  for(int i = 0; i < jsonArray.size(); i ++) {
    jsonLoop(jsonArray.get(i));
  }
}

JSONArray遍歷的方式有很多種

for

for(int i = 0; i < jsonArray.size(); i++){
	JSONObject json = jsonArray.getJSONObject(i);
}

foreach

jsonArray.forEach(o -> {
  if (o instanceof JSONObject) {
    JSONObject json = (JSONObject) o;
  }

Iterator

JSONObject jsonObject = new JSONObject(jsonString);
Iterator iterator = jsonObject.keys();
while(iterator.hasNext()){
	key = (String) iterator.next();
	value = jsonObject.getString(key);
}

5.JSONPath

另外向大家推薦一個(gè)非常好用的工具:JSONPath。

JSONPath是一種簡(jiǎn)單的方法來提取給定JSON的部分內(nèi)容,使用方式類似于正則表達(dá)式。 GitHub地址: https://github.com/json-path/JsonPath

簡(jiǎn)單描述下使用方法已經(jīng)自己使用的案例 pom文件依賴:

<dependency>
  <groupId>com.jayway.jsonpath</groupId>
  <artifactId>json-path</artifactId>
  <version>2.2.0</version>
</dependency>

JsonPath表達(dá)式總是以與XPath表達(dá)式結(jié)合使用XML文檔相同的方式引用JSON結(jié)構(gòu)。

JsonPath中的“根成員對(duì)象”始終稱為$,無(wú)論是對(duì)象還是數(shù)組。

JsonPath表達(dá)式可以使用點(diǎn)表示法。

這里有個(gè)表格,說明JSONPath語(yǔ)法元素和對(duì)應(yīng)XPath元素的對(duì)比。

官方案例:

 

 

詳細(xì)大家還是參照官方解說。 下面是我寫的案例:

JSONArray jsonArray = JSONPath.read("$.ePrint.common..label");

需要注意的是這里的JSONArray是JSONPath的,所以導(dǎo)包是:net.minidev.json.JSONPath JSON格式不會(huì)變,所以可以轉(zhuǎn)換為alibaba的JSONArray:

com.alibaba.fastjson.JSONArray jsonArr = JSON.parse(jsonArray.toString());

這里要注意一點(diǎn)也是我踩過的坑:如果獲取一個(gè)JSONObject下有多個(gè)同名的JSONArray,那么返回的[]也是多個(gè)。要先遍歷獲取到的數(shù)據(jù),在取其中的一個(gè)JSON塊。

到此這篇關(guān)于淺談在Java中JSON的多種使用方式的文章就介紹到這了,更多相關(guān)Java中JSON使用方式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論