首页 > Web开发 > 详细

String/JSONObject/JSONArray/List/Map互转

时间:2019-09-08 16:40:36      阅读:581      评论:0      收藏:0      [点我收藏+]

---恢复内容开始---

1)JSONArray转List

JSONArray queryValueArray = queryJson.getJSONArray("value");
List<String> queryValueList = JSONObject.parseArray(queryValueArray.toJSONString(), String.class);

2)List转JSONArray

String value = "111,222";

JSONArray authValueArray = JSONArray.parseArray(JSON.toJSONString(authEntry.getValue().toString()));

 

1、 String

1.1)String转JSONObject

String jsonString = "{"model_id":"25","model_name":"ad_table"}";

JSONObject testJson = JSONObject.parseObject(jsonString);

1.2)JSONObject转String

String testString = testJson.toJSONString();

 

2.1)String转JSONArray

String queryString = "["111", "222"]";

JSONArray queryArray = JSONArray.parseArray(queryString);

2.2)JSONArray转String

queryArray.toJSONString();

 

3.1)String根据分隔符转List

String testString = "111,222";

List<String> list = Arrays.asList(testString.split(","));

3.2)List转String

String temp1 = list.toString(); // [111,222]

 

4.1)String转Map

String->JSONObject->Map

4.1)Map转String

 

 

2、JSONObject

1.1)JSONObject转JSONArray

JSONArray itemArray = jsonData.getJSONArray("items");

1.2)JSONArray转JSONObject

NA

 

2.1)JSONObject转List集合

NA

2.2)List集合转JSONObject

JSONObject.toJSON(list<Item>);

 

3.1)JSONObject转Map

Map<String, String> userMap = (Map) userJson;

3.2)Map转JSONObject

JSONObject newJson = (JSONObject) JSONObject.toJSON(userMap);

 

3、JSONArray

1.1)JSONArray转List

 

1.2)List转JSONArray

 

2.1)JSONArray转Map

NA

2.2)Map转JSONArray

NA

 

4、List

1.1)List转Map

NA

1.2)Map转List

NA

 

5、其他

1)JSONObject转Java Object

Object object = JSONObject.toJavaObject(jsonData, Object.class);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

---恢复内容结束---

1)JSONArray转List

JSONArray queryValueArray = queryJson.getJSONArray("value");
List<String> queryValueList = JSONObject.parseArray(queryValueArray.toJSONString(), String.class);

2)List转JSONArray

String value = "111,222";

JSONArray authValueArray = JSONArray.parseArray(JSON.toJSONString(authEntry.getValue().toString()));

 

1、 String

1.1)String转JSONObject

String jsonString = "{"model_id":"25","model_name":"ad_table"}";

JSONObject testJson = JSONObject.parseObject(jsonString);

1.2)JSONObject转String

String testString = testJson.toJSONString();

 

2.1)String转JSONArray

String queryString = "["111", "222"]";

JSONArray queryArray = JSONArray.parseArray(queryString);

2.2)JSONArray转String

queryArray.toJSONString();

 

3.1)String根据分隔符转List

String testString = "111,222";

List<String> list = Arrays.asList(testString.split(","));

3.2)List转String

String temp1 = list.toString(); // [111,222]

 

4.1)String转Map

String->JSONObject->Map

4.1)Map转String

 

 

2、JSONObject

1.1)JSONObject转JSONArray

JSONArray itemArray = jsonData.getJSONArray("items");

1.2)JSONArray转JSONObject

NA

 

2.1)JSONObject转List集合

NA

2.2)List集合转JSONObject

JSONObject.toJSON(list<Item>);

 

3.1)JSONObject转Map

Map<String, String> userMap = (Map) userJson;

3.2)Map转JSONObject

JSONObject newJson = (JSONObject) JSONObject.toJSON(userMap);

 

3、JSONArray

1.1)JSONArray转List

 

1.2)List转JSONArray

 

2.1)JSONArray转Map

NA

2.2)Map转JSONArray

NA

 

4、List

1.1)List转Map

NA

1.2)Map转List

NA

 

5、其他

1)JSONObject转Java Object

Object object = JSONObject.toJavaObject(jsonData, Object.class);

 

 

 

 

 

 

 

 

 

 

 

 

 

String/JSONObject/JSONArray/List/Map互转

原文:https://www.cnblogs.com/midgard/p/10979651.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!