String/JSONObject/JSONArray/List/Map互转

1)JSONArray转List

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

2)List转JSONArray

String value = "111,222";

List<String> strList = Arrays.asList(value.split(","));

JSONArray authValueArray = JSONArray.parseArray(JSON.toJSONString(strList));

 

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

List<T> list = new ArrayList<T>();

JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));

 

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);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2019-09-08 15:53  劈石  阅读(1401)  评论(0编辑  收藏  举报