Convert JSON structure

前端 未结 3 1430
-上瘾入骨i
-上瘾入骨i 2021-01-22 13:15

I have a set of data in one JSON structure:

[[task1, 10, 99],  
[task2, 10, 99],  
[task3, 10, 99],  
[task1, 11, 99],  
[task2, 11, 99],  
[task3, 11, 99]] 


        
相关标签:
3条回答
  • 2021-01-22 13:20

    Try Jolt. It's a java library that allows you to specify a transformation in json.

    0 讨论(0)
  • 2021-01-22 13:23

    For Java you can use json-simple from JSON.org. I don't think there's a 'clever' way to do it, as others have said, you just have to convert the JSON to real objects, manipulate and convert back. (see below) Also I believe that you have to quote literal strings, eg. "task1", "label", "data" for it to be valid JSON (or at least, json-simple won't accept it if you don't)

        package com.another;
    
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;
    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;
    
    public class NewClass {
    
        public static final String inputString = "[ [\"task1\", 10, 99],   [\"task2\", 10, 99],   [\"task3\", 10, 99],   [\"task1\", 11, 99],   [\"task2\", 11, 99],   [\"task3\", 11, 99]]";
    
        public static void main(String[] args) throws ParseException {
            JSONArray obj = (JSONArray) new JSONParser().parse(inputString);
            Map<Object, JSONObject> output = new HashMap<Object, JSONObject>();
            for (Object o : obj) {
                JSONArray item = (JSONArray) o;
                Object title = item.get(0);
                Object first = item.get(1);
                Object second = item.get(2);
                JSONObject dest = output.get(title);
                if (dest == null) {
                    dest = new JSONObject();
                    output.put(title, dest);
                    dest.put("label", title);
                    dest.put("data", new JSONArray());
                }
                JSONArray data = new JSONArray();
                data.add(first);
                data.add(second);
                ((JSONArray) dest.get("data")).add(data);
            }
            String outputString = JSONArray.toJSONString(Arrays.asList(output.values().toArray()));
            System.out.println(outputString);
        }
    }
    

    Here it is in JS

    var inputString = "[ [\"task1\", 10, 99],   [\"task2\", 10, 99],   [\"task3\", 10, 99],   [\"task1\", 11, 99],   [\"task2\", 11, 99],   [\"task3\", 11, 99]]";
    
    var obj = JSON.parse(inputString);
    var output = new Object();
    for (var i in obj) {
        var item = obj[i];
        var title = item[0];
        var first = item[1];
        var second = item[2];
        var dest = output[title];
        if (dest == null) {
            dest = {
                "label" : title,
                "data": new Array()
                };
            output[title] = dest;
        }
        dest.data.push([first, second]);
    }
    var outputArray = new Array();
    for (var t in output) {
        outputArray.push(output[t]);
    }
    
    0 讨论(0)
  • 2021-01-22 13:23

    First you need to parse the JSON to get real objects.

    With JavaScript you can just eval() it, or use JSON.parse(). There is a JSON implementation at JSON.org for browsers that don't have this built in.

    For Java (an entirely different language), JSON.org also has an implementation of a JSON parser that you can use.

    Once you've got the objects out of it, you can then just re-arrange until your heart's content, then JSON.stringify() (in JS) to convert back to JSON.

    0 讨论(0)
提交回复
热议问题