在Java中解析JSONArray的JSONArray

时间:2016-12-22 23:35:36

标签: java arrays json parsing

我很难解析看起来像这样的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
    ]
  }
}  

我试过这样的事,但没有用:

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);
}

请帮忙!

2 个答案:

答案 0 :(得分:0)

您正在使用的ObjectMapper是什么。以下是fastxml的示例。

String jsonInString = <your request>;
ObjectMapper mapper = new ObjectMapper();


//JSON from file to Object
User user = mapper.readValue(jsonInString, YourClassName.class);

答案 1 :(得分:0)

您可以使用google gson。 这会自动将您的JSON转换为嵌套列表列表的地图。

area.value=area.value.replace(/[^0-9,]/g,'');
area.value= area.value.replace(/,+/g,',');