您的位置:

JsonArray转字符串

一、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();
}
JsonArray转字符串

2023-05-19
java笔记,大学java笔记

2022-11-28
JavaJson字符串转数组详解

2023-05-17
json字符转javamap(json字符串转换)

本文目录一览: 1、java在后台如何将前台传过来的json格式数据转换为map? 2、java json字符串转换Map 3、json字符串怎样转换成map 4、json字符串转java map 5

2023-12-08
core解析json的笔记(c json解析)

本文目录一览: 1、如何解析json中map数据 2、fasterxml.jackson.core.jsonparser.feature是哪个jar包的 3、spring mvc 怎么获取json 4

2023-12-08
java笔记,尚硅谷java笔记

2022-12-01
json字符串转成map存入数据库(java map转化为j

本文目录一览: 1、怎么把json字符串转换成map 2、怎么把json字符串转换map? 3、JAVA中json字符串如何转化为map对象获取数据? 4、JAVA中json字符串如何转化为map对象

2023-12-08
重学java笔记,java笔记总结

2022-11-23
java实体类转换json字符串,字符串转json对象 ja

2022-11-24
JsonArray转String的详细阐述

2023-05-19
javajson转list,java把字符串转为json

2023-01-08
将Android字符串转换成JSON格式的实用方法

2023-05-14
java转义json字符串,带转义字符的json字符串如何处

2022-11-22
javajson字符串转list的简单介绍

本文目录一览: 1、java 怎么将jsonarray转换成list 2、java 中json.parsearray怎么将结果装换成list 3、java String字符串怎么转换成List,还是说

2023-12-08
javajson,javajson字符串转对象

本文目录一览: 1、java怎么处理json格式数据 2、java开发,json是干什么的 3、Java解析json数据 4、请问Java中json是什么? 5、java怎么读取json格式的数据 6

2023-12-08
印象笔记记录java学习(Java成长笔记)

2022-11-12
c语言字符串转换成int,C语言字符串转换成小写

2023-01-06
python基础学习整理笔记,Python课堂笔记

2022-11-21
如何将JsonArray转为Map

2023-05-20
java给jsonarray赋值(java数组转jsonar

本文目录一览: 1、java的JSONArray如何使用? 2、java怎么将json对象转换成json数组 3、java后台的json值怎么传给jsp页面,并进行遍历 4、java类里的数组 如何

2023-12-08