简体   繁体   中英

JSON for Java, Encode a JSON array - Streaming

[I have some code to create a JSON array. In this code I am passing some values to x , y , z in a loop which looks like this.

   JSONArray list = new JSONArray();
   String jsonText = null;
   for (int j = 0; j < 4; j++) {
       list.add(new Integer(x));
       list.add(new Integer(y));
       list.add(new Integer(z));
       jsonText = list.toString();
  }
  System.out.print(jsonText);

This gives output as

[1234,245,10,312,234,122,1234,67788,345,235,001,332]

How can I get these values in a single array like this?

[[1234,245,10],[312,234,122],[1234,67788,345],[235,001,332]]
] I got the answer for this question needs answer for the below question.

I used one of the below solutions. Thanks for the response from you guys.

Now i got JSON formate nested Arrays which looks like this

 [ [[1234,245,10],[312,234,122],[1234,67788,345],[235,001,332]], [[1234,245,10],[312,234,122],[1234,67788,345],[235,001,332],[1234,67788,3450]], [[1234,245,10],[312,234,122],[1234,67788,345],[235,001,332],[1234,67788,34534]]] 

SO i have One big Array which contains three arrays(this can be 2 or more than three arrays sometimes) and each of these three array contains some arrays, in this above example

what is the reverse procedure ? i mean what if i want those values from these arrays. In the same way how i have did. using JSON JSONArray list = new JSONArray(); list.get() this get method will give me what i requies ? I used the org.json Java API.

Thanks friends for helping me till now.

Just put it in another JSONArray .

JSONArray array = new JSONArray();
array.add(list);
String jsonText = array.toString();

Update : as per your comment:

Sorry, the output is something like this

 [1234,245,10,312,234,122,1234,67788,345,235,001,332] 

Can you please tell how to get the above output like this

 [1234,245,10][312,234,122][1234,67788,345][235,001,332] 

Several ways:

  1. Concatenate to the jsonString .

     String jsonText = ""; // Start with empty string. for (int j = 0; j < 4; j++) { JSONArray list = new JSONArray(); // Create new on each iteration. list.add(new Integer(x)); list.add(new Integer(y)); list.add(new Integer(z)); jsonText += list.toString(); // += concatenates to existing string. } System.out.print(jsonText); 
  2. Since String concatenating using += in a loop is memory hogging and slow, rather use StringBuilder .

     StringBuilder jsonText = new StringBuilder(); for (int j = 0; j < 4; j++) { JSONArray list = new JSONArray(); list.add(new Integer(x)); list.add(new Integer(y)); list.add(new Integer(z)); jsonText.append(list.toString(); } System.out.print(jsonText.toString()); 

I suspect what you want is a syntactically correct JSON array of nested arrays of integers (original post requests invalid JSON). If not, go with @BalusC's answer .

To get an array containing sub-arrays, simply create the sub-arrays as int[] arrays, and add them directly to your main JSONArray .

public int[] getThreeValues() { // example
    Random r = new Random();
    return new int[] { r.nextInt(100), r.nextInt(100), r.nextInt(100) };
}

public void execute() {

    JSONArray master = new JSONArray();

    for (int j=0; j<4; j++) {
        master.put(getThreeValues());
    }

    System.out.println(master);
}

Result:

[[3,13,37],[24,4,64],[61,2,1],[97,13,86]]

Note: I'm not sure which JSON library your using, so I used the org.json Java API , which uses put(...) rather than add(...) . Also, that particular library supports adding int[] arrays directly to JSONArray - yours may not, in which case you'd need to build the nested JSONArrays and add them to your master.

You can always put the list inside a JSONArray

eg

   JSONArray list = new JSONArray();
   String jsonText = null;
   for (int j = 0; j < 4; j++) {
       list.add(new Integer(x));
       list.add(new Integer(y));
       list.add(new Integer(z));
  }

  JSONArray finalList = new JSONArray();
  finalList.put(0, list);
  jsonText = finaList.toString();
  System.out.print(jsonText);

Or ( not recommended at all ) hack your output,

eg

jsonText += "[" + jsonText + "]";
System.out.print(jsonText);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM