0

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

....

Ben Hendo
  • 13
  • 4

3 Answers3

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]

Anand Shah
  • 611
  • 7
  • 14
2

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

fge
  • 119,121
  • 33
  • 254
  • 329
  • Thanks,when i use your code i have error since I dont have this jar for jackson,how can i add it?is this source free? – Ben Hendo Jun 05 '13 at 12:22
  • It is dual licensed GPL/ASL. There are maven artifacts for it etc. – fge Jun 05 '13 at 12:23
  • What does it mean,I cannot use it for free in my company?how can i get this maven artifacts? – Ben Hendo Jun 05 '13 at 12:36
  • Yes you can (since the ASL allows for it). As to how to get it, don't you use maven already? If not you can just download jars directly. – fge Jun 05 '13 at 12:38
2

[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"
        }
    ]

}
pepuch
  • 6,346
  • 7
  • 51
  • 84
  • Thanks,is it source free?if yes how can i get the jars to avoid the errors? – Ben Hendo Jun 05 '13 at 12:37
  • Yes, of course, you can use it. I used gradle to resolve dependencies but you can also use maven or just download jars from maven central repository and add to classpath. Gson can be downloaded from here http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.google.code.gson%22 ("download: column) – pepuch Jun 05 '13 at 12:41
  • Thanks Patrick ,I still miss in the code how should i put Entry 1 and Entry 2 which is the parent of the array of the key value fields since when i print it i dont see entry1&2 – Ben Hendo Jun 05 '13 at 14:10
  • I'm sorry but I don't know. The best will be if you will read gson user guide. – pepuch Jun 05 '13 at 15:20