简体   繁体   English

JSON for Java,编码JSON数组-流

[英]JSON for Java, Encode a JSON array - Streaming

[I have some code to create a JSON array. [我有一些代码来创建JSON数组。 In this code I am passing some values to x , y , z in a loop which looks like this. 在这段代码中,我正在像这样的循环中将一些值传递给xyz

   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 现在我得到了JSON formate嵌套数组,看起来像这样

 [ [[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 所以我有一个大数组,其中包含三个数组(有时可以是2个或三个以上数组),并且在上述示例中,这三个数组中的每个数组都包含一些数组

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(); 使用JSON JSONArray list = new JSONArray(); list.get() this get method will give me what i requies ? list.get()这个get方法将给我我需要的东西? I used the org.json Java API. 我使用了org.json Java API。

Thanks friends for helping me till now. 谢谢朋友们对我的帮助。

Just put it in another JSONArray . 只需将其放在另一个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 . 连接到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

     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). 我怀疑您想要的是整数嵌套数组的语法正确的JSON数组(原始发布请求无效的JSON)。 If not, go with @BalusC's answer . 如果不是,请使用@BalusC的答案

To get an array containing sub-arrays, simply create the sub-arrays as int[] arrays, and add them directly to your main JSONArray . 要获得包含子数组的数组,只需将子数组创建为int[]数组,然后将它们直接添加到您的主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(...) . 注意:我不确定您使用哪个JSON库,因此我使用了org.json Java API ,该API使用put(...)而不是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. 此外,该特定库支持将int []数组直接添加到JSONArray-您可能不支持,在这种情况下,您需要构建嵌套的JSONArray并将其添加到主数据库中。

You can always put the list inside a JSONArray 您始终可以将列表放在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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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