转换JSON结构 [英] Convert JSON structure

查看:105
本文介绍了转换JSON结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个JSON结构中有一组数据:

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]] 

并需要将其转换为另一个JSON结构:

and need to convert it to another JSON structure:

[{  
  label: "task1",  
  data: [[10, 99], [11, 99]]  
},  
{   
  label: "task2",  
  data: [[10, 99], [11, 99]] 
},  
{  
  label: "task3",  
  data: [[10, 99], [11, 99]]
}]

javascript / java的最佳方法是什么?

What's the best way to do this with javascript/java?

推荐答案

对于Java,您可以使用JSON.org中的json-simple。我不认为有一种聪明的方式,正如其他人所说,你只需要将JSON转换为真实对象,操纵和转换回来。 (见下文)另外我相信你必须引用文字字符串,例如。 task1,label,data表示它是有效的JSON(或者至少,json-simple不接受它,如果你没有)

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);
    }
}

这里是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]);
}

这篇关于转换JSON结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆