0

So I am trying to pull data from my database and essentially create a JSON file that looks like this. I know of that I can use my_json_object.put(); but that seems to only work for String, String. I and I want to create the value with 2 entries, and the second entryw ill actually be an array of many values. How can I achieve that? Thanks guys.

{"value": {
"name": "History",
"values": [ 
{"front": "History Market", "back": " It is the world of the world that the world    could not say that", "color": "Yellow"},
{"front": "History Market", "back": " It is the world of the world that the world could not say that", "color": "Red"},
{"front": "History Market", "back": " It is the world of the world that the world could not say that", "color": "Purple"},
{"front": "History Market", "back": " It is the world of the world that the world could not say that", "color": "Blue"},
{"front": "History Market", "back": " It is the world of the world that the world could not say that", "color": "Blue"}
]}}
1
  • Construct the desired "nest" of arrays and dictionaries/maps and use a JSON serializer to create the JSON string. Commented Jan 26, 2013 at 19:52

3 Answers 3

4

Create your current jsonObject as :

//Create main json object
JSONObject json = new JSONObject();

//Create value json object
JSONObject valueJson = new JSONObject();

//Create values JSONArray
JSONArray valuesarray = new JSONArray();

for(int i=0;i<=4;i++){
 // Create values jsonObject
JSONObject valuesJson = new JSONObject();
valuesJson.put("front","front_value");
valuesJson.put("back","back_value");
valuesJson.put("color","color_value");
 // put values jsonObject to valuesarray JSONArray
valuesarray.put(valuesJson);    

}
 // put name key-value to value jsonObject
valueJson.put("name","History");
 // put values JSONArray to value jsonObject
valueJson.put("values",valuesarray);

 // put value jsonObject to final json Object
json.put("value",valueJson);
Sign up to request clarification or add additional context in comments.

Comments

2

Use google gson, it is very simple and ferfectly works with arrays and lists

public class CollectionToJson {
public static void main(String[] args) {
    //
    // Converts a collection of string object into JSON string.
    //
    List<String> names = new ArrayList<String>();
    names.add("Alice");
    names.add("Bob");
    names.add("Carol");
    names.add("Mallory");

    Gson gson = new Gson();
    String jsonNames = gson.toJson(names);
    System.out.println("jsonNames = " + jsonNames);

    //
    // Converts a collection Student object into JSON string
    //
    Student a = new Student("Alice", "Apple St",
            CollectionToJson.getDOB(2000, 10, 1));
    Student b = new Student("Bob", "Banana St", null);
    Student c = new Student("Carol", "Grape St",
            CollectionToJson.getDOB(2000, 5, 21));
    Student d = new Student("Mallory", "Mango St", null);

    List<Student> students = new ArrayList<Student>();
    students.add(a);
    students.add(b);
    students.add(c);
    students.add(d);

    gson = new Gson();
    String jsonStudents = gson.toJson(students);
    System.out.println("jsonStudents = " + jsonStudents);

    //
    // Converts JSON string into a collection of Student object.
    //
    Type type = new TypeToken<List<Student>>(){}.getType();
    List<Student> studentList = gson.fromJson(jsonStudents, type);

    for (Student student : studentList) {
        System.out.println("student.getName() = " + student.getName());
    }
}

private static Date getDOB(int year, int month, int date) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month - 1);
    calendar.set(Calendar.DATE, date);
    calendar.set(Calendar.HOUR, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    return calendar.getTime();
}

}

Output

 jsonNames = ["Alice","Bob","Carol","Mallory"]
jsonStudents = [{"name":"Alice","address":"Apple St","dateOfBirth":"Nov 1, 3900 12:00:00 AM"},{"name":"Bob","address":"Banana St"},{"name":"Carol","address":"Grape St","dateOfBirth":"Jun 21, 3900 12:00:00 AM"},{"name":"Mallory","address":"Mango St"}]
student.getName() = Alice
student.getName() = Bob
student.getName() = Carol
student.getName() = Mallory

1 Comment

thanks for your answer also. I chose the answer above becuase I did't want to use a library. I am sure it works perfectly though.
0

JSONObject can have JSONArray nested inside it and visa versa .

in your example you will have to create :

  • List item
  • JSONObject
  • inside it a JSONObject called "value"
  • Inside it a String entry called "name" with a String value "History".
  • And a JSONArray entry.
  • List item
  • Inside that JSONArray a set of JSONObjects.

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.