使用深度数组创建JSON文件 [英] Create JSON file with deep array

查看:108
本文介绍了使用深度数组创建JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用以下structre创建新的json文件,但由于结构/数组内部具有结构,因此无法成功执行,该文件应如下所示: 知道如何创建这样的人吗?

I need to create new json file with the following structre I am not able to success since i have structure inside structure/array ,the file should look like : any idea how to create one like this?

 {

    "Entry1": [
        {
            "id": "0001",
            "type": "USER",

        },
        {
            "id": "0002",
            "type": "EMP",
        "property":"Salery",

        }

],

"Entry2": [

    {
        "id": "0005",
        "name": "Vacation",
        "property":"user",

    },
    {
        "id": "0008",
        "name": "Work",

        }
    ]

}

我尝试使用以下代码失败.

I was tried to use the following code without success.

JSONObject obj = new JSONObject();
obj.put("entry1", null);

JSONArray list = new JSONArray();
list.add("id");
list.add("USER");

....

推荐答案

[UPDATE] 我已经编辑了答案,因为它应该返回不同的json.我不知道这是否是bes解决方案,但是它可以正常工作,而且很容易阅读和维护.

[UPDATE] I've edited my answer because it should return different json. I don't know if this is the bes solution but it works and also it is clear to read and maintain.

根据阿南德(Anand)的回答,这是一个使用Gson的简单示例.我认为这是一个很好的解决方案,因为您要做的就是创建POJO并使用Gson/Jackson将其序列化为json.您无需手动创建json对象.

According to Anand answer here is an simple example using Gson. I think that it's great solution because all you need to do is to create POJO and serialize it to json using Gson/Jackson. You don't need to create json object manually.

JsonTest主类:

JsonTest main class:

import java.util.ArrayList;

import com.google.gson.Gson;

public class JsonTest {

    public static void main(String[] main) {
        Entry entry1 = new Entry();
        entry1.setId(1);
        entry1.setType("USER");
        entry1.setProperty("Salary");
        Entry entry2 = new Entry();
        entry2.setId(2);
        entry2.setType("EMP");
        Entry entry3 = new Entry();
        entry3.setId(2);
        entry3.setType("EMP");
        entry3.setProperty("Work");
        Entry entry4 = new Entry();
        entry4.setId(2);
        entry4.setType("EMP");

        EntryListContainer entryListContainer = new EntryListContainer();
        ArrayList<Entry> entryList1 = new ArrayList<Entry>();
        ArrayList<Entry> entryList2 = new ArrayList<Entry>();

        entryList1.add(entry1);
        entryList1.add(entry2);
        entryList2.add(entry3);
        entryList2.add(entry4);

        entryListContainer.setEntryList1(entryList1);
        entryListContainer.setEntryList2(entryList2);

        String json = new Gson().toJson(entryListContainer, EntryListContainer.class);
        System.out.println(json);
    }

}

EntryListContainer类:

EntryListContainer class:

import java.util.ArrayList;

import com.google.gson.annotations.SerializedName;

public class EntryListContainer {

    @SerializedName("Entry1")
    private ArrayList<Entry> entryList1;
    @SerializedName("Entry2")
    private ArrayList<Entry> entryList2;

    public void setEntryList1(ArrayList<Entry> entryList1) {
        this.entryList1 = entryList1;
    }

    public ArrayList<Entry> getEntryList1() {
        return entryList1;
    }

    public void setEntryList2(ArrayList<Entry> entryList2) {
        this.entryList2 = entryList2;
    }

    public ArrayList<Entry> getEntryList2() {
        return entryList2;
    }

}

输入POJO:

public class Entry {

    private int id;
    private String type;
    private String property;

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }

}

它将返回此json:

{

    "Entry1":[
        {
            "id":1,
            "type":"USER",
            "property":"Salary"
        },
        {
            "id":2,
            "type":"EMP"
        }
    ],
    "Entry2":[
        {
            "id":2,
            "type":"EMP",
            "property":"Work"
        },
        {
            "id":2,
            "type":"EMP"
        }
    ]

}

这篇关于使用深度数组创建JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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