如何在Java中读取嵌套的JSON列表?

huangapple go评论79阅读模式
英文:

how to read nested json list in java?

问题

JSONArray jsonarray = new JSONArray(response);
System.out.println(String.format("JSONArray length => %d", jsonarray.length()));

for (int i = 0; i < jsonarray.length(); i++) {
    JSONObject obj1 = jsonarray.getJSONObject(i);
    String name = obj1.getString("name");
    int type = obj1.getInt("type");
    JSONObject networkUsage = obj1.getJSONObject("networkUsage");
    int downloadValue = networkUsage.getInt("download");
    System.out.println(name);
    System.out.println(type);
    System.out.println(downloadValue);
}
英文:

I need to get type value and networkUsage download value and list was dynamic

Json Response was

    {
  &quot;count&quot;: 2,
  &quot;devices&quot;: [
    {
      &quot;name&quot;: &quot;xyz&quot;,
      &quot;type&quot;: 1,
      &quot;subType&quot;: 0,
      &quot;signalStrength&quot;: -37,
      &quot;dataUsage&quot;: {
        &quot;download&quot;: 84,
        &quot;upload&quot;: 84
      },
      &quot;networkUsage&quot;: {
        &quot;download&quot;: 0,
        &quot;upload&quot;: 0
      },
      &quot;speed&quot;: {
        &quot;download&quot;: 0,
        &quot;upload&quot;: 0
      }
    },
    {
      &quot;name&quot;: &quot;abcd&quot;,
      &quot;type&quot;: 2,
      &quot;subType&quot;: 0,
      &quot;signalStrength&quot;: -37,
      &quot;dataUsage&quot;: {
        &quot;download&quot;: 84,
        &quot;upload&quot;: 84
      },
      &quot;networkUsage&quot;: {
        &quot;download&quot;: 0,
        &quot;upload&quot;: 0
      },
      &quot;speed&quot;: {
        &quot;download&quot;: 0,
        &quot;upload&quot;: 0
      }
    }
  ]
}

I tried this way and got errors.

JSONArray jsonarray = new JSONArray(response);
System.out.println(String.format(&quot;JSONArray length =&gt; %d&quot;, jsonarray.length()));

for (int i = 0; i &lt; jsonarray.length(); i++) {
        JSONObject obj1 = jsonarray.getJSONObject(i);
        String name = obj1.getString(&quot;name&quot;);
        int type = obj1.getInt(&quot;type&quot;);
        System.out.println(name);
        System.out.println(type);
}

> Exception in thread "main" org.json.JSONException: A JSONArray text must start with '[' at character 1 at org.json.JSONTokener.syntaxError(JSONTokener.java:496) at org.json.JSONArray.<init>(JSONArray.java:115) at org.json.JSONArray.<init>(JSONArray.java:159) at com.calix.pages.test.main(test.java:91)

答案1

得分: 0

JSONObject jsonobj = new JSONObject(response);
JSONArray jsonarray = jsonobj.getJSONArray("devices");
for (int i = 0; i < jsonobj.getInt("count"); i++) {
    JSONObject obj1 = jsonarray.getJSONObject(i);
    String name = obj1.getString("name");
    int type = obj1.getInt("type");
    System.out.println(name);
    System.out.println(type);
}
英文:
JSONObject jsonobj = new JSONObject(response);
JSONArray jsonarray = jsonobj.getJSONArray(&quot;devices&quot;);
for (int i = 0; i &lt; jsonobj.getInt(&quot;count&quot;); i++) {
        JSONObject obj1 = jsonarray.getJSONObject(i);
        String name = obj1.getString(&quot;name&quot;);
        int type = obj1.getInt(&quot;type&quot;);
        System.out.println(name);
        System.out.println(type);
}

huangapple
  • 本文由 发表于 2020年8月19日 00:48:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63473182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定