如何使用动态响应密钥解析json

时间:2019-09-30 05:02:56

标签: android json gson retrofit2

我该如何解析以下json响应

{
  "MH46AF4149": [
    {
      "distance": 74,
      "date": "23-09-2019"
    },
    {
      "distance": 97,
      "date": "24-09-2019"
    },
    {
      "distance": 91,
      "date": "25-09-2019"
    },
    {
      "distance": 80,
      "date": "26-09-2019"
    },
    {
      "distance": 91,
      "date": "27-09-2019"
    },
    {
      "distance": 16,
      "date": "28-09-2019"
    }
  ]
}

MH46AF4149 dynamic value,它会随着每个新的响应而不断变化。

我正在将RetrofitClient与GsonConverterFactory一起使用。如何填充模型类。我应该使用哪个注释。 @Expose不起作用

4 个答案:

答案 0 :(得分:1)

您需要使用 Key Iterator 来获取此动态Key,就像这样:

首先存储您的Json响应到一个 JSONObject

JSONObject mainJSONObj=new JSONObject(responseString);

// get all keys from mainJSONObj
Iterator<String> iterator = mainJSONObj.keys();
  while (iterator.hasNext()) {
    String key = iterator.next();
    Log.e("Your Dynamic Key: ","-> "+key);
  }

您的 Retrofit2 呼叫应如下所示:

Call<String> call = mApiInterface.myApi(yourBodyParams);

call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {

                JSONObject mainJSONObj=new JSONObject(response);

                // get all keys from mainJSONObj
                Iterator<String> iterator = mainJSONObj.keys();
                while (iterator.hasNext()) {
                    String key = iterator.next();
                    Log.e("Your Dynamic Key: ","-> "+key);

                    JSONArray jsonArray = (JSONArray) mainJSONObj.getJSONArray(key);

                    if(jsonArray!=null){

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

                            String distance=jsonObject1.getString("distance");
                            String date=jsonObject1.getString("date");

                            Log.e("distance: ","-> "+distance);
                            Log.e("date: ","-> "+date);
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                t.printStackTrace();
            });

这对我有帮助;希望对您有帮助。

答案 1 :(得分:1)

按照下面的代码

JSONObject response = new JSONObject(responseString);
Iterator<?> keys = response.keys();
while( keys.hasNext() ) {
    String key = (String)keys.next();
    if ( response.get(key) instanceof JSONObject ) {
        JSONArray array = response.getJSONArray(key);
        for(int i = 0; i < array.length(); i++){
            JSONObject element = array.getJSONObject(i);
            //now use this array object to get strings
        }
    }
}

答案 2 :(得分:0)

您可以获得这样的钥匙

//if you have single key at that time use below code

 try {
           JSONObject jsonObject = new JSONObject(result.tostring);
            Iterator<String> keys = jsonObject.keys();
            String str_Name=keys.next();
            JSONArray jsonElements = (JSONArray) jsonObject.getJSONArray(str_Name);

//if you have multiple key you can get by for loop or while loop 

            while( keys.hasNext() ) {
                String key = (String)keys.next();
                JSONArray jsonElement = (JSONArray) jsonObject.getJSONArray(str_Name);
                for (int i = 0 ; i < jsonElement.length();i++){
                   JSONObject jsonObject1 = jsonElement.getJSONObject(i);
                }

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


答案 3 :(得分:0)

希望这对您有帮助。

public class Car {

        private String distance;
        private String date;

    }



Gson gson = new Gson();

    Type listType = new TypeToken<List<Car>>() {}.getType();
    List<Car> carList = null;
    try {
        jsonResponse = new JSONObject(response);

        Iterator<String> keys = jsonResponse.keys();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            if (jsonResponse.get(key) instanceof JSONArray) {
                JSONArray array = jsonResponse.getJSONArray(key);
               carList= gson.fromJson(array.toString(), listType);

                if (carList != null && carList.size() > 0) {

                }

            }
        }


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