简体   繁体   English

在Java / Android中解析多个JSON数组

[英]Parsing Multiple JSON Arrays in Java/Android

If you pass multiple JSON arrays into Java (in a single stream) that looks like this: 如果将多个JSON数组(在单个流中)传递给Java,则如下所示:

[ <-----json objects---->] [ <-----json objects---->]  [ <-----json objects---->] 

How do you parse in Java? 您如何用Java解析? And is there a way to do it without rewriting the JSON data into a single array? 有没有一种方法可以在将JSON数据重写为单个数组的情况下做到这一点?

Essentialy, I want to get to a point where I can do this after the parsing. 本质上,我想分析一下之后就可以做到这一点。

item = json.getString("item");
total = json.getString("total");

items.add(item);
totals.add(total);

A key note is that the first array is items, the second array is totals. 重要说明是,第一个数组是项,第二个数组是总计。 The first json object in items corresponds to the first in totals. 项目中的第一个json对象对应于总计的第一个json对象。

I would say that you need first of all transform this content in a valid JSON, maybe you could first replace ]\\s*[ by ],[ and add [ at the beggining and ] at the end. 我要说的是,您首先需要以有效的JSON转换此内容,也许您可​​以先将]\\s*[替换为],[并在开始处添加[] This would be a valid JSON and could be parsed as an JSONArray that contains many JSONArray s. 这将是一个有效的JSON和可解析为JSONArray包含许多JSONArray秒。

Something like: 就像是:

String receivedJSON = "[{},{}] [{},{}] [{}]";
String normalized = "[" + receivedJSON.replaceAll("\\]\\s*\\[", "],[") + "]";
new JSONArray(normalized);

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

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