JAVA 将上面包体转换成下面

JAVA 将上面包体转换成下面


这个是JSONArray
                        [[
                                {
                                        "Type": 4,
                                        "Name": "ExecID"
                                },
                                {
                                        "Type": 4,
                                        "Name": "TaskID"
                                }
                                
                        ],[
                                {
                                        "Value": "123"
                                },
                                {
                                        "Value": "456"
                                }
                                
                        ],[
                                {
                                        "Value": "789"
                                },
                                {
                                        "Value": "abc"
                                }
                                
                        ]
                        
                        ..............
                        ]
                        变这样这样
                        [
                                {
                                        "ExecID": "123",
                                        "TaskID": "456",
                                },{
                                        "ExecID": "789",
                                        "TaskID": "abc",
                                }
                                .............
                        ]

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class Test {
    public static void main(String[] args) {
        //jsonArray:要处理的
        String str = "[[{\"Type\": 4,\"Name\": \"ExecID\"},{\"Type\": 4,\"Name\": \"TaskID\"}],[{\"Value\": \"123\"},{\"Value\": \"456\"}],[{\"Value\": \"789\"},{\"Value\": \"abc\"}]]";
        JSONArray jsonArray = JSONArray.parseArray(str);

        //保存转换后的结果
        JSONArray resArray = new JSONArray();

        //把ExecID那些取出来,作为firstArray
        JSONArray firstArray = JSONArray.parseArray(jsonArray.get(0).toString());
        
        //剩下的从下标1开始,逐个取值、赋值
        for (int i = 1; i < jsonArray.size(); i++) {
            //用来存{"TaskID":"456","ExecID":"123"}这种结构
            JSONObject resObj = new JSONObject();
            JSONArray array = JSONArray.parseArray(jsonArray.get(i).toString());
            for (int j = 0; j < array.size(); j++) {
                //获取Name
                JSONObject firstObj = JSONArray.parseObject(firstArray.get(j).toString());
                String Name = firstObj.getString("Name");

                //获取Value
                JSONObject restObj = JSONArray.parseObject(array.get(j).toString());
                String Value = restObj.getString("Value");
                //存到resObj
                resObj.put(Name, Value);
            }
            //存到resArray
            resArray.add(resObj);
        }

        System.out.println(resArray.toString());
    }
}

很简单

        //array为你的JSONArray
        String jsonStr=JSON.toJSONString(array);
        List<List<Map<String, String>>> list = (List<List<Map<String, String>>>) JSONArray.parse(jsonStr);
        List<Map<String, String>>  splitList= list.stream().flatMap(v -> v.stream()).collect(Collectors.toList());
        JSONArray result = JSONArray.parseArray(JSON.toJSONString(splitList));

这段代码可以转换成那个模式,望采纳

img

public class test001 {undefined
public static void main(String[] args) throws Exception {undefined
String abc="[{"ID":"4","Name":"张山","JoinDate":"2019-05-16","loginName":"81000004","No":"81000004"},{"ID":"6","Name":"李四","JoinDate":"2019-05-16","loginName":"81000007","No":"81000007"}]";
System.out.println(""+abc+"\t");
//将集合转成JSONArray
JSONArray jsonArray = JSONArray.parseArray((String) abc);
//打印JSONArray数组
System.out.println("jsonArray ="+jsonArray);
for (int i = 0; i <jsonArray.size() ; i++) { //遍历JSONArray数组
JSONObject object1 = JSONArray.parseObject(jsonArray.get(i).toString()); //将JSONArray数组转成JSONObject对象
System.out.println("object1"+object1); //打印JSONObject对象
System.out.println(object1.getString("loginName")); //通过getString获取对象的属性
}
}
}