【问题标题】:How to fetch json data without the json array name in android from django rest api ?如何从 django rest api 获取 android 中没有 json 数组名称的 json 数据?
【发布时间】:2018-09-03 21:54:44
【问题描述】:
private void jsonParse() {

String url = "http://127.0.0.1:8000/products/products/";

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArray = response.getJSONArray("");

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject o = jsonArray.getJSONObject(i);
                        int id = o.getInt("id");
                        String title = o.getString("title");

                        String description = o.getString("description");
                        String price = o.getString("price");
                        mTextViewResult.append(title + ", " + String.valueOf(id) + ", "+price +"," + description + "\n\n");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
    }
});

mQueue.add(request);}

这是我的安卓代码

 [  {
"id": 1,
"title": "t-shirt",
"description": "this is a good t-shirt",
"price": "39.99"

},

{
"id": 2,
"title": "Jeans",
"description": "this is a jean",
"price": "89.99"}]

这是我的 json 数据

如何获取没有 json 数组名称的 json 数据。 在我的 android 中,我需要有 json 数组名称来解析 json 数据。但是我的 django rest api 生成没有 json 数组名称的 json 数据。

【问题讨论】:

    标签: android json django


    【解决方案1】:

    响应是一个 json 列表。你需要一个JSONArrayRequest 对象来获取JSONArray

    示例

    String url = "http://127.0.0.1:8000/products/products/";
    
    JsonArrayRequest request = new JsonArrayRequest(url,
        new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                try {
                    // `response` is the JSONArray that you need to iterate
                    for (int i = 0; i < response.length(); i++) {
                        JSONObject o = response.getJSONObject(i);
                        int id = o.getInt("id");
                        String title = o.getString("title");
    
                        String description = o.getString("description");
                        String price = o.getString("price");
                        mTextViewResult.append(title + ", " + String.valueOf(id) + ", "+price +"," + description + "\n\n");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
          }
    });
    

    【讨论】:

    • [ JSONObject o = jsonArray.getJSONObject(i); ]这部分我应该写什么?
    • 但它无法获取json数据并在android中显示。当我单击按钮时,它不显示任何内容。
    • 很抱歉打扰您,但我是 android 新手,所以我无法获得它。这是 logcat 显示-\ 03-26 14:54:09.785 2532-2636/com.example.android.ecom D/OpenGLRenderer:交换行为 1 03-26 14:54:09.963 2532-2636/com.example.android。 ecom E/EGL_emulation: tid 2636: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH) 03-26 14:54:09.963 2532-2636/com.example.android.ecom W/OpenGLRenderer: 无法在表面 0x99843280 上设置 EGL_SWAP_BEHAVIOR,错误=EGL_BAD_MATCH
    • 很抱歉打扰您,但我是 android 新手,所以我无法获得它。这是 logcat 显示 - 03-26 14:54:09.785 2532-2636/com.example.android.ecom D/OpenGLRenderer:交换行为 1 03-26 14:54:09.963 2532-2636/com.example.android.ecom E/EGL_emulation: tid 2636: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH) 03-26 14:54:09.963 2532-2636/com.example.android.ecom W/OpenGLRenderer: 无法在表面 0x99843280 上设置 EGL_SWAP_BEHAVIOR,错误 = EGL_BAD_MATCH
    猜你喜欢
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多