【问题标题】:Parse these Json data with arrays in Android在 Android 中用数组解析这些 Json 数据
【发布时间】:2018-07-25 18:26:55
【问题描述】:
please tell me I am new to use JSON data. Thanks in Advance.

https://api.myjson.com/bins/waw4y

how to parse this data in android. I am adding the two client details. In that 
First, I have added an object for the whole content and after I have added the different client names and age, car with arrays. so please make it as easy. 
Thanks in advance 

JSON 代表 JavaScript 对象表示法。它是一种独立的数据交换格式,是 XML 的最佳替代品。本章介绍如何解析 JSON 文件并从中提取必要的信息。

Android 提供了四种不同的类来操作 JSON 数据。这些类是 JSONArray、JSONObject、JSONStringer 和 JSONTokenizer。

The first step is to identify the fields in the JSON data in which you are interested in. For example. In the JSON given below we interested in getting 
temperature only.

【问题讨论】:

  • stackoverflow.com/questions/2591098/how-to-parse-json-in-java 的可能重复项。请看看它是否可以帮助您解决问题。
  • 不,这只是在多个数组中
  • GSON 是来自 Google 的库,它可以帮助您解决您的情况:using GSON
  • 数组的数量与解无关。该问题涵盖了将字符串转换为 JSON 的内容。接受的答案告诉您如何从 JSONArray 和 JSONObject 获取值。
  • 我检查了你的json,并不复杂。我在 YouTube 视频上使用 github api 做了一个 JSOn 示例。希望这真的对你有帮助。 youtu.be/Pl4dG5FkDfQ

标签: java android json


【解决方案1】:

这段代码对于JSON的动态实现是不可行的,只是为了展示解析过程。

JSONObject client = null;
    try {
        client = new JSONObject(response);
        JSONArray clientChild1 = client.getJSONArray("client1");
        JSONArray clientChild2 = client.getJSONArray("client2");
        for (int j = 0; j < clientChild1.length(); j++) {
            JSONObject objectChild = clientChild1.getJSONObject(j);
            Log.e("NAME", "onCreate: " + objectChild.getString("name"));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 2013-01-11
    相关资源
    最近更新 更多