[英]Parsing JSONArray of JSONArray in Java
I am having a hard time parsing a JSONArray that looks like this: 我很难解析如下所示的JSONArray:
{
"informations": {
"rows_nb": 4, //max is 4
"report": [
[ [ "BEACH", 59.35 ], [ "OCEAN", 40.65 ] ],//row1 , can be more than 2 arrays
[ [ "OCEAN", 70.2 ], [ "BEACH", 29.8 ] ],//row2, can be more than 2 arrays
[ "OCEAN", "BEACH" ],//row3
[ "OCEAN" ]//row4
]
}
}
I tried something like this, but didn't work : 我尝试了类似的方法,但没有成功:
JSONObject JSONresult = new JSONObject(result);
JSONObject extras = JSONresult.getJSONObject("extras");
JSONArray report = extras.getJSONArray("report");
List<ArrayList<Object[]>> report_list = new ArrayList<ArrayList<Object[]>>();
for (int i = 0; i < report.length(); i++) {
JSONArray tmp = report.getJSONArray(i);
ArrayList<Object[]> row = new ArrayList<Object[]>();
for (int j = 0; i < tmp.length(); i++) {
Object[] array = {tmp.getJSONArray(i).getString(0), tmp.getJSONArray(i).getDouble(1)};
row.add(array);
}
report_list.add(row);
}
Please help! 请帮忙!
What is the ObjectMapper you are using . 您正在使用什么ObjectMapper。 Below is the sample for fasterxml.
下面是fastxml的示例。
String jsonInString = <your request>;
ObjectMapper mapper = new ObjectMapper();
//JSON from file to Object
User user = mapper.readValue(jsonInString, YourClassName.class);
You can use google gson . 您可以使用google gson 。 This would automatically convert your JSON to a Map of List of nested list's.
这将自动将JSON转换为嵌套列表的列表列表。
String json = "{report: [ [ [ BEACH, 59.35 ], [ OCEAN, 40.65 ] ], [ [ OCEAN, 70.2 ], [ BEACH, 29.8 ] ], [ OCEAN, BEACH], [ OCEAN ] ]}";
Gson gson = new GsonBuilder().create();
Map map = gson.fromJson(json, Map.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.