8 ways to convert JsonArray array strings to ListMapString, String

8 ways to convert JsonArray array strings to List<Map<String, String>>

Special note: The main content of this article is reproduced from the blog of why flowers are so popular , the blog address is https://www.cnblogs.com/chancy/p/10179151.html

This article is mainly used for the JSON data we obtained from an interface address, and we want to convert it into Map<String, String> type data and store it in the list.

For example: the JSON data we obtained from the interface address is as follows

insert image description here

At this point we need to take out the userlist content in the body and convert it into a Map<String, String> of the user object and store it in the list

insert image description here

My main code implementation method is as follows:

// resp表示图一的JSON数据
JSONObject jsonObject = JSON.parseObject(resp);
JSONObject body = jsonObject.getJSONObject("body");

// 将userList取出并且转换成json数组字符串,结果为[{"id":"1","name":"张三"},{"id":"2","name":"李四"}]
String userList = body.getString("userList");

// 最关键一步: 利用JSONArray中的parse方法来解析json数组字符串
List<Map<String,String>> list = (List<Map<String,String>>) JSONArray.parse(userList);

// 使用数组遍历,每个数据都是一个Map集合
for(Map<String,String> mapList : list){
	// 通过key便可取出value值
	String value = mapList.get("kay")
}

The following are the 8 methods shown in the author's blog of why flowers are so popular. They are all practical methods, and you can try them. You can choose which one you like according to your personal preference.

public class JsonToMapTest02 {
 
    public static void main(String[] args){
 
        String strArr = "[{\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"}," +
                "{\"00\":\"zhangsan\",\"11\":\"lisi\",\"22\":\"wangwu\",\"33\":\"maliu\"}]";
        //第一种方式
        List<Map<String,String>> listObjectFir = (List<Map<String,String>>) JSONArray.parse(strArr);
        System.out.println("利用JSONArray中的parse方法来解析json数组字符串");
        for(Map<String,String> mapList : listObjectFir){
            for (Map.Entry entry : mapList.entrySet()){
               System.out.println( entry.getKey()  + "  " +entry.getValue());
            }
        }
        //第二种方式
        List<Map<String,String>> listObjectSec = JSONArray.parseObject(strArr,List.class);
        System.out.println("利用JSONArray中的parseObject方法并指定返回类型来解析json数组字符串");
        for(Map<String,String> mapList : listObjectSec){
            for (Map.Entry entry : mapList.entrySet()){
                System.out.println( entry.getKey()  + "  " +entry.getValue());
            }
        }
        //第三种方式
        JSONArray listObjectThir = JSONArray.parseArray(strArr);
        System.out.println("利用JSONArray中的parseArray方法来解析json数组字符串");
        for(Object mapList : listObjectThir){
            for (Object entry : ((Map)mapList).entrySet()){
                System.out.println(((Map.Entry)entry).getKey()  + "  " +((Map.Entry)entry).getValue());
            }
        }
        //第四种方式
        List listObjectFour = JSONArray.parseArray(strArr,Map.class);
        System.out.println("利用JSONArray中的parseArray方法并指定返回类型来解析json数组字符串");
        for(Object mapList : listObjectFour){
            for (Object entry : ((Map)mapList).entrySet()){
                System.out.println(((Map.Entry)entry).getKey()  + "  " +((Map.Entry)entry).getValue());
            }
        }
        //第五种方式
        JSONArray listObjectFifth = JSONObject.parseArray(strArr);
        System.out.println("利用JSONObject中的parseArray方法来解析json数组字符串");
        for(Object mapList : listObjectFifth){
            for (Object entry : ((Map)mapList).entrySet()){
                System.out.println(((Map.Entry)entry).getKey()  + "  " +((Map.Entry)entry).getValue());
            }
        }
        //第六种方式
        List listObjectSix = JSONObject.parseArray(strArr,Map.class);
        System.out.println("利用JSONObject中的parseArray方法并指定返回类型来解析json数组字符串");
        for(Object mapList : listObjectSix){
            for (Object entry : ((Map)mapList).entrySet()){
                System.out.println(((Map.Entry)entry).getKey()  + "  " +((Map.Entry)entry).getValue());
            }
        }
        //第七种方式
        JSONArray listObjectSeven = JSON.parseArray(strArr);
        System.out.println("利用JSON中的parseArray方法来解析json数组字符串");
        for(Object mapList : listObjectSeven){
            for (Object entry : ((Map)mapList).entrySet()){
                System.out.println(((Map.Entry)entry).getKey()  + "  " +((Map.Entry)entry).getValue());
            }
        }
        //第八种方式
        List listObjectEigh = JSONObject.parseArray(strArr,Map.class);
        System.out.println("利用JSON中的parseArray方法并指定返回类型来解析json数组字符串");
        for(Object mapList : listObjectEigh){
            for (Object entry : ((Map)mapList).entrySet()){
                System.out.println(((Map.Entry)entry).getKey()  + "  " +((Map.Entry)entry).getValue());
            }
        }
    }
}
t.println(((Map.Entry)entry).getKey()  + "  " +((Map.Entry)entry).getValue());
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/xiri_/article/details/121509664