JSON for Java,对 JSON 数组进行编码 - 流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3700107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON for Java, Encode a JSON array - Streaming
提问by jimmy
[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.
[我有一些代码来创建一个 JSON 数组。在这段代码中我传递一些值x
,y
,z
在一个循环中,看起来像这样。
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 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 格式的嵌套数组,它看起来像这样
[
[[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(); list.get() this get method will give me what i requies ? I used the org.json Java API.
什么是相反的程序?我的意思是如果我想要这些数组中的值怎么办。就像我所做的一样。使用 JSON JSONArray list = new JSONArray(); list.get() 这个 get 方法会给我什么我想要的?我使用了 org.json Java API。
Thanks friends for helping me till now.
感谢朋友们帮助我到现在。
采纳答案by Chadwick
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.
我怀疑你想要的是一个语法正确的 JSON 数组嵌套数组(原始帖子请求无效的 JSON)。如果没有,请使用@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[]
数组,然后将它们直接添加到 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.
注意:我不确定您使用的是哪个 JSON 库,所以我使用了org.json Java API,它使用put(...)
而不是add(...)
. 此外,该特定库支持将 int[] 数组直接添加到 JSONArray - 您的可能不支持,在这种情况下,您需要构建嵌套的 JSONArrays 并将它们添加到您的 master 中。
回答by BalusC
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]
对不起,输出是这样的
[1234,245,10,312,234,122,1234,67788,345,235,001,332]
你能告诉我如何像这样得到上面的输出吗
[1234,245,10][312,234,122][1234,67788,345][235,001,332]
Several ways:
几种方式:
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);
Since String concatenating using
+=
in a loop is memory hogging and slow, rather useStringBuilder
.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());
连接到
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);
由于
+=
在循环中使用字符串连接会占用内存并且速度很慢,因此使用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());
回答by Buhake Sindi
You can always put the list inside a JSONArray
您始终可以将列表放在 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);