Create JSON file with deep array

半世苍凉 提交于 2019-12-12 00:28:23

问题


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

....


回答1:


[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.

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

}

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

}

It will return this json:

{

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

}



回答2:


Try google gson project. it is having simple method to conert to & from objects. doesn't matter how deeper it is. Supports all collections.

[https://code.google.com/p/google-gson/][1]




回答3:


Use Jackson. It is very simple to create arbitrary JSON values with it:

final JsonNodeFactory factory = JsonNodeFactory.instance;

final ObjectNode baseNode = factory.objectNode();

ArrayNode array;
ObjectNode element1, element2;

// create elements
element1 = factory.objectNode();
element1.put("id", whatever);
// etc, then

array = factory.arrayNode();
array.add(element1);
array.add(element2);
baseNode.put("Entry1"), array;

// etc; then print baseNode.toString() in the file

It also has excellent support for {de,}serialization of POJOs, with smart guesses, so you could even deserialize a list of POJOs without even using any annotations (see ObjectMapper).



来源:https://stackoverflow.com/questions/16937544/create-json-file-with-deep-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!