一、String转JSON字符串
1、使用JSONObject
String str = "{\"name\":\"John\", \"age\":30}";
JSONObject json = new JSONObject(str);
System.out.println(json.toString());
2、使用Gson
String str = "{\"name\":\"John\", \"age\":30}";
Gson gson = new Gson();
JsonElement element = gson.fromJson(str, JsonElement.class);
String json = gson.toJson(element);
System.out.println(json);
3、使用Jackson
String str = "{\"name\":\"John\", \"age\":30}";
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(str);
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);
System.out.println(json);
二、JSON字符串转Map
1、使用Gson
String json = "{\"name\":\"John\", \"age\":30}";
Gson gson = new Gson();
Type type = new TypeToken<Map<String, Object>>(){}.getType();
Map<String, Object> map = gson.fromJson(json, type);
for(Map.Entry<String, Object> entry: map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
2、使用Jackson
String json = "{\"name\":\"John\", \"age\":30}";
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
for(Map.Entry<String, Object> entry: map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
3、使用JSON类库
String json = "{\"name\":\"John\", \"age\":30}";
Map<String, Object> map = new HashMap<>();
try{
JSONObject jsonObject = new JSONObject(json);
Iterator<String> keys = jsonObject.keys();
while (keys.hasNext()){
String key = keys.next();
map.put(key, jsonObject.get(key));
}
for(Map.Entry<String, Object> entry: map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}catch(Exception e){
e.printStackTrace();
}
三、JsonArray转逗号隔开字符串
JSONArray jsonArray = new JSONArray("[1,2,3,4,5]");
String str = StringUtils.join(jsonArray.iterator(), ",");
System.out.println(str);
四、JSON字符串转Map对象
String json = "{\"person\":{\"name\":\"John\", \"age\":30}}";
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
Map<String, Object> personMap = (Map<String, Object>)map.get("person");
System.out.println("name:" + personMap.get("name") + ", age:" + personMap.get("age"));
五、JSON字符串转Map集合
String json = "{\"persons\":[{\"name\":\"John\", \"age\":30},{\"name\":\"Tom\", \"age\":40}]}";
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
List<Map<String, Object>> personsMap = (List<Map<String, Object>>)map.get("persons");
for(Map<String, Object> personMap: personsMap) {
System.out.println("name:" + personMap.get("name") + ", age:" + personMap.get("age"));
}
六、JSON字符串转换成List
1、使用Gson
String json = "[{\"name\":\"John\", \"age\":30},{\"name\":\"Tom\", \"age\":40}]";
Gson gson = new Gson();
Type type = new TypeToken<List<Map<String, Object>>>(){}.getType();
List<Map<String, Object>> list = gson.fromJson(json, type);
for(Map<String, Object> map: list) {
System.out.println("name:" + map.get("name") + ", age:" + map.get("age"));
}
2、使用Jackson
String json = "[{\"name\":\"John\", \"age\":30},{\"name\":\"Tom\", \"age\":40}]";
ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> list = mapper.readValue(json, new TypeReference<List<Map<String, Object>>>(){});
for(Map<String, Object> map: list) {
System.out.println("name:" + map.get("name") + ", age:" + map.get("age"));
}
七、JSON字符串转JSON对象
String json = "{\"name\":\"John\", \"age\":30}";
JSONObject jsonObject = new JSONObject(json);
System.out.println("name:" + jsonObject.get("name") + ", age:" + jsonObject.get("age"));
八、JSONObject转JSONArray
JSONObject jsonObject = new JSONObject("{\"persons\":[{\"name\":\"John\", \"age\":30},{\"name\":\"Tom\", \"age\":40}]}");
JSONArray jsonArray = jsonObject.getJSONArray("persons");
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject person = (JSONObject)jsonArray.get(i);
System.out.println("name:" + person.get("name") + ", age:" + person.get("age"));
}
九、JSONArray字符串转换成JSONArray
String jsonArrayStr = "[{\"name\":\"John\",\"age\":30},{\"name\":\"Tom\",\"age\":40}]";
JSONArray jsonArray = new JSONArray(jsonArrayStr);
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject person = (JSONObject)jsonArray.get(i);
System.out.println("name:" + person.get("name") + ", age:" + person.get("age"));
}
十、JSON字符串转List
1、使用Gson
String json = "[{\"name\":\"John\", \"age\":30},{\"name\":\"Tom\", \"age\":40}]";
Gson gson = new Gson();
Type type = new TypeToken<List<Map<String, Object>>>(){}.getType();
List<Map<String, Object>> list = gson.fromJson(json, type);
for(Map<String, Object> map: list) {
System.out.println("name:" + map.get("name") + ", age:" + map.get("age"));
}
2、使用JSON类库
String json = "[{\"name\":\"John\", \"age\":30},{\"name\":\"Tom\", \"age\":40}]";
List<Map<String, Object>> list = new ArrayList<>();
try{
JSONArray jsonArray = new JSONArray(json);
for(int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.optJSONObject(i);
Map<String, Object> map = new HashMap<>();
Iterator<String> keys = jsonObject.keys();
while (keys.hasNext()){
String key = keys.next();
map.put(key, jsonObject.get(key));
}
list.add(map);
}
for(Map<String, Object> map: list) {
System.out.println("name:" + map.get("name") + ", age:" + map.get("age"));
}
}catch(Exception e){
e.printStackTrace();
}