tags:

views:

142

answers:

3

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

+4  A: 

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());
    
BalusC
@BalusC i got both the answers. I have two different approaches one with concatenating arrays and one with nested arrays. So i got you solution and aslo @chadwick's Solution. Thanks for the responces you guys are genius. thanks for the help.
salman
It's a matter of using human logical thinking powers. It's however sad that your initial question was completely wrong.
BalusC
@BalusC Yes i agree with you, But as i told i have to use for both the answers urs and @Chadwick's. Buts it my foult that i should have ask for both the aproches. Your answer helps me with String of names which am looking for.
salman
A: 

You can always put the list inside a JSONArray

e.g.

   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,

e.g.

jsonText += "[" + jsonText + "]";
System.out.print(jsonText);
The Elite Gentleman
+1  A: 

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.

Chadwick