【问题标题】:Parsing JSON with column key in first array使用第一个数组中的列键解析 JSON
【发布时间】:2016-08-23 14:43:44
【问题描述】:

我想在java中解析一个JSON字符串,并从下面提到的文本中获取以下值:

{
  "columns": [
    "id",
    "name"
  ],
  "rows": [
    [
      "124",
      "LOREM"
    ],
    [
      "125",
      "DOLOR"
    ],
    [
      "126",
      "FUGIAT"
    ],
    [
      "127",
      "IPSUM"
    ]
 ]
}

从每个元素中,我想从标记为行的数组中获取值。我如何在 Jackson JSON for android 中做到这一点?

【问题讨论】:

  • 请描述您到目前为止所做的尝试。然后我们可以提供帮助

标签: java android json jackson


【解决方案1】:

类似这样的:

 List<String> rowsValues = new ArrayList<>(); // to store row String values in List
    try {
        JSONObject json = new JSONObject(data); //data is your json in String format
        JSONArray array =  json.getJSONArray("rows");

        for (int index = 0 ; index < array.length(); index++) {
            JSONArray currentArray = array.getJSONArray(index);
            for (int i = 0 ; i < currentArray.length(); i++) {
                rowsValues.add(array.getString(i));
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    相关资源
    最近更新 更多