1

I am working on an application where i have to generate a json like this:

[
  {"title":"Culture","start":"Salary","end":"Work"},
  {"title":"Work","start":"Salary","end":"Work"}
]

But my code generates json like this:

{{"name":"Culture"},[{"name":"Salary"},{"name":"Work"}],}

My code:

public class ParseJson {



public static class EntryListContainer {

    public List<Entry> children = new ArrayList<Entry>();
    public Entry name;

}

public static class Entry {

    private String name;

    public Entry(String name) {
        this.name = name;
    }

}



public static void main(String[] args) {
    EntryListContainer elc1 = new EntryListContainer();
    elc1.name = new Entry("Culture");
    elc1.children.add(new Entry("Salary"));
    elc1.children.add(new Entry("Work"));




    ArrayList<EntryListContainer> al = new ArrayList<EntryListContainer>();
    Gson g = new Gson();

    al.add(elc1);


    StringBuilder sb = new StringBuilder("{");
    for (EntryListContainer elc : al) {

        sb.append(g.toJson(elc.name));
        sb.append(",");
        sb.append(g.toJson(elc.children));
        sb.append(",");
    }

    String partialJson = sb.toString();

    if (al.size() > 1) {
        int c = partialJson.lastIndexOf(",");
        partialJson = partialJson.substring(0, c);
    }

    String finalJson = partialJson + "}";
    System.out.println(finalJson);

    }

}

Can anyone help me to generate this json in my required format ?? please thanks in advance

8
  • 2
    Create a class that represents your data, then use Gson to stringify the class Commented Sep 8, 2015 at 14:45
  • can you edit my code ??i have tried to make one ?? Commented Sep 8, 2015 at 14:46
  • Welcome to Stack Overflow! I added formatting to your required json schema, Also I rewrote a couple of lines to correct gramma and make your question concise. Good luck Commented Sep 8, 2015 at 14:51
  • Hint: for a start you need fields called title, start and end ... Commented Sep 8, 2015 at 14:51
  • btw I also suggest you to use library for rendering json. It might be trickier than you think. Commented Sep 8, 2015 at 14:52

3 Answers 3

5

Try this

public class Entry {
    public String title;
    public String start;
    public String end;
}

And in another part of your code

private ArrayList<Entry> entries = new ArrayList<>();

// Fill the entries...

String the_json = new Gson().toJson(entries);
Sign up to request clarification or add additional context in comments.

Comments

1

1) First Create your POJO

public class MyJSONObject {

    private String title;
    private String start;
    private String end;

    //getter and setter methods

   [...]

    @Override
    public String toString() {

    }

}

2) Use com.google.code.gson library

public static void main(String[] args) {

 {
    ArrayList<MyJSONObject> myJSONArray = new ArrayList<>();

    MyJSONObject obj = new MyJSONObject();
    obj.setTitle="Culture";
    obj.set[...]

    myJSONArray.add(obj);

    Gson gson = new Gson();

    // convert java object to JSON format,
    // and returned as JSON formatted string
    String json = gson.toJson(myJSONArray);

    System.out.println(json);
}

Output : [{"title":"Culture","start":"Salary","end":"Work"}, ...]

Comments

0

I recommend you to use some JSON Java API, like Gson. It's very simple to generate a string json from a POJO object or to create a POJO object from a string json.

The code for generating a string json from a POJO object is like this:

Gson gson = new Gson();
String stringJson = gson.toJson(somePojoObject);

The code for creating a POJO object from a string json is like this:

Gson gson = new Gson();
SomePojoClass object = gson.fromJson(stringJson, SomePojoClass.class);

Note that you can not serialize objects with circular references. This causes infinite recursion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.