1

Can someone provide me java code to create a json object as shown below

 {"main":[
 ["one","two","three","four","five"],
 ["one","two","three","four","five"],
 ["one","two","three","four","five"],
 ["one","two","three","four","five"],
 ["one","two","three","four","five"]
]}

I have tried something like

 Gson gson = new Gson();
 JsonArray array = new JsonArray();
 array.add(new JsonPrimitive("One"));
 array.add(new JsonPrimitive("two"));
 array.add(new JsonPrimitive("three"));
 array.add(new JsonPrimitive("four"));
 array.add(new JsonPrimitive("five"));
 JsonObject jsonObject = new JsonObject();
 jsonObject.add("main", array);

I am getting the result like below even when I am looping

{"main":["one","two","three","four","five"]} 

like a single object. But I am expecting the result like

{"main":[
 ["one","two","three","four","five"],
 ["one","two","three","four","five"],
 ["one","two","three","four","five"],
 ["one","two","three","four","five"],
 ["one","two","three","four","five"]
]}

Many thanks in advance.

soorapadman
4,5137 gold badges38 silver badges49 bronze badges
asked May 18, 2017 at 4:50

7 Answers 7

2

try this code to create json

Gson gson = new Gson();
JsonArray array = new JsonArray();
JsonArray child = new JsonArray();
child.add(new JsonPrimitive("One"));
child.add(new JsonPrimitive("two"));
child.add(new JsonPrimitive("three"));
child.add(new JsonPrimitive("four"));
child.add(new JsonPrimitive("five"));
for(int i=0;i<5;i++)
array.add(child);
JsonObject jsonObject = new JsonObject();
jsonObject.add("main", array);
System.out.println(jsonObject);
answered May 18, 2017 at 5:11
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming you're using Gson, this isn't the way it was designed to be used. Though this way is supported, it is not suggested, as you could use any json library to do this (SimpleJson).

Instead, Gson is able to directly serialize java objects we are familiar with, so you should represent your json object as a java object. JsonObject maps to a Map. JsonArray maps to List or an array. JsonPrimitives are mapped to their respective java primitive types (boolean, double, string, null)

// generate the object
Map<List<List<String>>> object = new HashMap<>();
List<List<String>> main = new ArrayList<>();
List<String> counts = Arrays.asList("one", "two", "three", "four", "five");
for (int i = 0; i < 5; i++) {
 main.add(counts);
}
object.put("main", main);
// serialize it
String json = new Gson().toJson(object);
// deserializing it requires a typetoken or separate class representing the map object.
Map<List<List<String>>> desObj = new Gson().fromJson(json, new TypeToken<Map<List<List<String>>>>(){}.getType());
answered May 18, 2017 at 5:26

Comments

0

It appears "main" contains an array of arrays, so all you would have to do is add your array five times to another new array (e.g. call it mainArray) and then add mainArray to your jsonObject:

  • Create a new empty array: mainArray
  • Add array five times to mainArray
  • Add mainArray to jsonObject
answered May 18, 2017 at 4:56

Comments

0

You can try to convert the string representing your json data to JsonObject:

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class JsonQuestion {
 public static void main(String[] args) {
 String myJSON = "{\"main\":[\n"
 + " [\"one\",\"two\",\"three\",\"four\",\"five\"],\n"
 + " [\"one\",\"two\",\"three\",\"four\",\"five\"],\n"
 + " [\"one\",\"two\",\"three\",\"four\",\"five\"],\n"
 + " [\"one\",\"two\",\"three\",\"four\",\"five\"],\n"
 + " [\"one\",\"two\",\"three\",\"four\",\"five\"]\n"
 + "]}";
 JsonParser jsonParser = new JsonParser();
 JsonObject jsonObject = (JsonObject) jsonParser.parse(myJSON);
 System.out.println("jsonObject: " + jsonObject.toString());
 }
}
answered May 18, 2017 at 5:06

Comments

0
 Gson gson = new Gson();
 JsonArray array = new JsonArray();
 array.add(new JsonPrimitive("One"));
 array.add(new JsonPrimitive("two"));
 array.add(new JsonPrimitive("three"));
 array.add(new JsonPrimitive("four"));
 array.add(new JsonPrimitive("five"));
 JsonObject jsonObject = new JsonObject();
 JsonArray marray = new JsonArray();
 marray.add(array);
 marray.add(array);
 marray.add(array);
 marray.add(array);
 marray.add(array);
 jsonObject.add("main", marray);
answered May 18, 2017 at 5:14

Comments

0

You can use below method to make json :

 private void createJsonData(){
 final String[] units = {"One","Two","Three","Four",
 "Five"};
 try {
 JSONObject jsonObject = new JSONObject();
 JSONArray jsonArray = new JSONArray();
 JSONArray jsonArray1 = new JSONArray();
 for (int j = 0; j < 5 ; j++) {
 jsonArray1.put(units[j])
 }
 jsonArray.put(jsonArray);
 jsonObject.put("main",jsonArray);
 } catch (JSONException e) {
 e.printStackTrace();
 }
 }
answered May 18, 2017 at 5:23

Comments

0

Use this method

public static void newjson() {
 JSONObject json =new JSONObject();
 List<List<String>> listoflist = new ArrayList<List<String>>();
 List<String> list=new ArrayList<String>();
 list.add("one");
 list.add("one");
 list.add("one");
 listoflist.add(list);
 listoflist.add(list);
 try {
 json.put("main",listoflist);
 } catch (JSONException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 System.out.println(json);
 }
answered Mar 24, 2020 at 10:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.