【问题标题】:android-parsing nested json Objectsandroid-解析嵌套的 json 对象
【发布时间】:2014-10-30 12:38:19
【问题描述】:

我必须在我的应用程序中实现 json 数据。但我不知道如何从服务器获取数据。我正在使用 ION 库。

{
"search": [{
    "id": "5454003",
    "description": "Larger Than Life",
    "url": "http://audiojungle.net/item/larger-than-life/5454003",
    "type": "item",
    "sales": "1469",
    "rating": "5",
    "item_info": {
        "id": "5454003",
        "item": "Larger Than Life",
        "url": "http://audiojungle.net/item/larger-than-life/5454003",
        "user": "pinkzebra",
        "thumbnail": "https://0.s3.envato.com/files/67162834/upbeatsongwinner2.jpg",
        "sales": "1469",
        "rating": "5",
        "rating_decimal": "4.96",
        "cost": "18.00",
        "preview_type": "audio",
        "preview_url": "https://0.s3.envato.com/files/94250640/preview.mp3",
        "length": "2:35"
    }
} ] }

这是我的 json 数据。我想在我的应用程序数据中显示“成本”、“用户”等。但我没明白。离子代码来了,

Ion.with(this)
            .load("http://marketplace.envato.com/api/edge/search:audiojungle,,happy.json")
            .asJsonObject().setCallback(new FutureCallback<JsonObject>() {

                @Override
                public void onCompleted(Exception arg0, JsonObject data) {
                    // TODO Auto-generated method stub

                }
            });

所以如果有人知道,请帮助我。提前谢谢你。

【问题讨论】:

  • 从对象数据中获取数组“search”,然后获取其元素之一(或迭代数组),然后获取元素“item_info”,然后获取元素“cost”或“user”...为什么它得到+1?这个问题是典型的“how to parse json with X library”在这里回答了很多次
  • 但我真的对此一无所知。请给我任何参考。
  • 您的 JSON 数据无效 Fotomat SyntaxError: JSON.parse: expected ',' or ']' after array element at line 23 column 3 JSON data
  • ok:检查 ION 使用了哪个 json 库,然后找到该库的文档,阅读它(或 google 获取示例),编写代码......足够简单吗?
  • 请任何人帮助我。

标签: android json nested android-ion


【解决方案1】:

编辑:你发布的第一个信息有一些误导性信息,我检查了你发布的答案,我有一个工作代码给你,

代码:

Ion.with(this)
        .load("http://marketplace.envato.com/api/edge/search:audiojungle,,happy.json")
        .asString().setCallback(new FutureCallback<String>() {
    @Override
    public void onCompleted(Exception e, String result) {
        try {
            parseJson(new JSONObject(result));
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
    }
});

解析响应数据:

private void parseJson(JSONObject result) {

    try {
        JSONArray jsonArray = result.getJSONArray("search");
        for(int a=0;a<jsonArray.length();a++){

            System.out.println(jsonArray.getJSONObject(a).getString("id"));
            System.out.println(jsonArray.getJSONObject(a).getString("description"));
            System.out.println(jsonArray.getJSONObject(a).getString("url"));
            System.out.println(jsonArray.getJSONObject(a).getString("type"));
            System.out.println(jsonArray.getJSONObject(a).getString("sales"));
            System.out.println(jsonArray.getJSONObject(a).getString("rating"));

            // JSON data with in JSONObject "item_info"
            System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("id"));
            System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("item"));
            System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("url"));
            System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("user"));
            System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("thumbnail"));
            System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("sales"));
            System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("rating"));
            System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("rating_decimal"));
            System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("cost"));
            System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("preview_type"));
            System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("preview_url"));
            System.out.println(jsonArray.getJSONObject(a).getJSONObject("item_info").getString("length"));

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

}
  • 记得使用 JSONArray(org.json.JSONArray) 和 JSONObject(org.json.JSONObject) 形成 org.json 库,而不是 JsonArray(com.google.gson.JsonArray;) 或 JsonObject(com.google.gson. JsonObject) 来自 com.google.gson 库,它把事情搞砸了;
  • 为方便起见,在此处粘贴回复http://json.parser.online.fr/ 以查看哪个是 jsonobject 或数组

【讨论】:

  • @NikPastagia “为单个对象获取多个值”是什么意思
  • 我的意思是说 item_info 对象在每个数组索引值中重复...所以我想获取数组的每个索引值...
  • @NikPastagia 形成我的理解,你只需要每个数组中的 item_info 数据,而不是在循环中使用 jsonArray.getJSONObject(a).getJSONObject("item_info") 。您将获得每个数组的完整 item_info 对象。
  • @NikPastagia gilbert92sarip@gmail.com
  • 您好,这是否意味着我可以在 for 循环中设置我的 setter 以绑定到我的 Recyclerview ?,如何将 Json 数据绑定到我的 RecyclerView?
【解决方案2】:

流程:

JsonArray searchArray = getJsonArray("search");
JsonObject index_0_Object= searchArray.getJsonObject("index_0");
JsonObject infoObject = index_0_Object.getJsonObject("item_info");
String id = infoObject.getString("id");

System.out.pritnln(id);

已编辑

首先:你必须找到“搜索”数组,在它使用索引来获取它的对象之后,所以现在你有了 JsonObject,从中获取“item_info”对象,从那里你可以找到“id”对象。

【讨论】:

  • 对我也不起作用...您的答案是针对 GSON ..每次我使用它都没有得到任何价值...简单地使用 JSONObject 或 JSONArray 不使用 JsonArray ..
  • 哥们,我已经把它作为一个进程添加了,尝试根据这个答案解析。
  • 这里,没有像 getJsonArray("search") 这样的方法。
【解决方案3】:
> yes i got it...

    Ion.with(this)
                .load("http://marketplace.envato.com/api/edge/search:audiojungle,,happy.json")
                .asString().setCallback(new FutureCallback<String>() {

                    @Override
                    public void onCompleted(Exception arg0, String data) {
                        // TODO Auto-generated method stub

                        try {
                            JSONObject jObject = new JSONObject(data);
                            JSONArray jArray = jObject.getJSONArray("search");

                            for ( int i = 0 ; i < jArray.size() ; i++ )
                            {
                                JSONObject jObject_0 = jArray.getJSONObject(i);
                                JSONObject jObj = jObject_0.getJSONObject("id");
                                Log.e("id : ",id+"");
                            } 
                            //JSONObject jObject_0 = jArray.getJSONObject(1);
                            //JSONObject jObj = jObject_0.getJSONObject("item_info");
                            //cost.setText(jObj.getString("cost"));
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }
                });

> but this returns only one value. i want to multiple value according to array list.

检查您的日志。

【讨论】:

  • 使用 jArray.size() ,并使用 forloop 。
  • 每次它给我一个单一的价值。
  • 您的 json 中只有一个对象。
猜你喜欢
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 2020-07-06
  • 1970-01-01
  • 2015-04-23
  • 1970-01-01
  • 2023-04-02
相关资源
最近更新 更多