簡體   English   中英

如何從Volley服務讀取[JSONArray [JSONArray]]響應

[英]How to read [JSONArray[JSONArray]] responses from Volley service

我有一個jsonArray響應。 我需要閱讀此響應,其中包含3個jsonArrays。

這是json響應。 這是一個GET請求,並通過Volley請求發送。

[  
  "0x9000",
  [
     [
       "D3521",
       "abc"
     ],
     [
       "D4212",
       "def"
     ],
     [ 
       "D2715",
       "hij ."
     ],
     [
       "D2366",
       "klm"
     ],
     [
       "D3660",
       "nopq"
     ]
  ]
]

這是我嘗試發送字符串請求的代碼。

    try{

    RequestQueue MyRequestQueue = Volley.newRequestQueue(this);

    final String endpoint = "getdoctors";
    final String url = SettingsConfig.IP + endpoint;
    StringRequest MyStringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            android.util.Log.d("print response",response);

            try {

                JSONArray jsonArray = new JSONArray(response);


                for (int i=0; i<jsonArray.length(); i++){

                    JSONArray dataArray = (JSONArray) jsonArray;

                    String code = dataArray.getString(i);

                }

            } catch (JSONException e) {
                e.printStackTrace();
                Log.e("Provider Inquiry","JSON error:" + e.getMessage());
                Intent intent = new Intent(EChannellingInfoActivity.this,MainMenuActivity.class);
                startActivity(intent);
                finish();
            }

        }
        }, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
        @Override
        public void onErrorResponse(VolleyError error) {


            android.util.Log.d("print error", error.toString());
            //This code is executed if there is an error.
        }
    });

    MyStringRequest.setRetryPolicy(new DefaultRetryPolicy(0, 0, DefaultRetryPolicy.DEFAULT_TIMEOUT_MS));
    MyRequestQueue.add(MyStringRequest);

    return true;
    } catch (Exception e) {
            e.printStackTrace();
            return false;
    }

此響應中有3個jsonArrays。 我如何一一分類/閱讀它們。

首先創建一個包含內部實例的類

   public class InnerObj
        {
         String id;
         String name;
        }

然后使用內部對象創建外部對象的類

 public class OuterObj
        {
         String size;
         InnerObj values[];
        }

現在,您可以使用OuterObj類作為responseType,將其映射到截擊的響應

我想解析以下JSON文件,但以[開頭是向我表示這是一個數組。

當以[開頭的JSON字符串表示字符串為JSONArray

如何修改解析器來解析此文件?

1.將JSON字符串轉換為JSONArray而不是JSONObject:

JSONArray jsonArray = new JSONArray(json);

2.getJSONFromUrl方法的返回類型從JSONObject更改為JSONArray

3.doInBackground從JSONArray中的getJSONFromUrl方法獲取響應:

 JSONArray jsonArray = jParser.getJSONFromUrl(URL);

這是更多答案LINK

這是您可以解析JSON數組

JSONArray jsonArray = new JSONArray(response);
                        String str = (String) jsonArray.get(0);
                        JSONArray arrayTwo = jsonArray.getJSONArray(1);
                        for (int i=0; i<arrayTwo.length(); i++){
                            JSONArray dataArray = arrayTwo.getJSONArray(i);
                            String code = dataArray.getString(i);
                        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM