简体   繁体   English

我如何从这样的 JSON 响应中获取值

[英]How do I get a value from such a JSON Response

This is What i get as a response from the PlacesApi after making a request.这是我在提出请求后从 PlacesApi 得到的响应。 But the issue is that i cant get the value of "photo_reference".但问题是我无法获得“photo_reference”的值。 The issue is also the Objects being in Arrays and all , the whole response looks confusing.问题还在于对象在 Arrays 和 all 中,整个响应看起来令人困惑。

Below is what i have tried in android studio Java以下是我在 android studio Java 中尝试过的

private void getUserLocationImage(String mLocationName) {
    String url = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input="+mLocationName+"&inputtype=textquery&fields=photos&key="+R.string.google_api;

    // prepare the Activities Request
    JsonObjectRequest getWeatherRequest = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray dataArray = response.getJSONArray("candidates");
                        JSONObject photosObj = dataArray.getJSONObject(0);
                        JSONArray photosArray = photosObj.getJSONArray("photos");
                        JSONObject photoRefObj = photosArray.getJSONObject(0);
                        String imageRef = photoRefObj.get("photo_reference").toString();


                        Toast.makeText(HomeLandingPageActivity.this, ""+imageRef, Toast.LENGTH_SHORT).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("Error.Response", error.toString());
        }
    });

    // add it to the RequestQueue
    queue.add(getWeatherRequest);
}

This is the Error这是错误

org.json.JSONException: Index 0 out of range [0..0)

This is what the response is in the Web这就是 Web 中的响应

{
   "candidates" : [
      {
         "photos" : [
            {
               "height" : 4160,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/111684034547030396888\"\u003eCaroline Wood\u003c/a\u003e"
               ],
               "photo_reference" : "CmRaAAAAQkMptoZgWJHING5qIR5_abXvnxjhHHEOHmDRH3ZpXUrar5PfpN5tQhhPoPwYmTDjpdVmXeT3T9klnrdK4xMvuudPm309UxMcx_ddbiu6E4shWYaPFn4gO4Diq4mOM46EEhCoo3TLpUbrWhInjelgVtYZGhSDJPyoRefWJ8WIcDs8Bk8VXAwHyQ",
               "width" : 3120
            }
         ]
      }
   ],
   "status" : "OK"
}

Try to use Gson library for deserilization your response.尝试使用 Gson 库对您的响应进行反序列化。 http://tutorials.jenkov.com/java-json/gson.html http://tutorials.jenkov.com/java-json/gson.html

It's very simple way to get any values from response这是从响应中获取任何值的非常简单的方法

At first need create class with response model首先需要使用响应模型创建类

import java.util.List;

public class ResponseBody {

    public List<Candidates> candidates;

    public static class Candidates {
        public List<Photos> photos;

        public static class Photos {
            public int height;
            public int width;
            public String photo_reference;

            public List<String> html_attributions;
        }
    }

}

Then just get your response as String and deserilize it:然后将您的响应作为字符串并将其反序列化:

String json = "{\n" +
                "   \"candidates\" : [\n" +
                "      {\n" +
                "         \"photos\" : [\n" +
                "            {\n" +
                "               \"height\" : 4160,\n" +
                "               \"html_attributions\" : [\n" +
                "                  \"\\u003ca href=\\\"https://maps.google.com/maps/contrib/111684034547030396888\\\"\\u003eCaroline Wood\\u003c/a\\u003e\"\n" +
                "               ],\n" +
                "               \"photo_reference\" : \"CmRaAAAAQkMptoZgWJHING5qIR5_abXvnxjhHHEOHmDRH3ZpXUrar5PfpN5tQhhPoPwYmTDjpdVmXeT3T9klnrdK4xMvuudPm309UxMcx_ddbiu6E4shWYaPFn4gO4Diq4mOM46EEhCoo3TLpUbrWhInjelgVtYZGhSDJPyoRefWJ8WIcDs8Bk8VXAwHyQ\",\n" +
                "               \"width\" : 3120\n" +
                "            }\n" +
                "         ]\n" +
                "      }\n" +
                "   ],\n" +
                "   \"status\" : \"OK\"\n" +
                "}";

        Gson gson = new Gson();

        ResponseBody responseBody = gson.fromJson(json, ResponseBody.class);

        System.out.println("responseBody = " + responseBody.candidates.get(0).photos.get(0).photo_reference);

I got this:我得到了这个:

responseBody = CmRaAAAAQkMptoZgWJHING5qIR5_abXvnxjhHHEOHmDRH3ZpXUrar5PfpN5tQhhPoPwYmTDjpdVmXeT3T9klnrdK4xMvuudPm309UxMcx_ddbiu6E4shWYaPFn4gO4Diq4mOM46EEhCoo3TLpUbrWhInjelgVtYZGhSDJPyoRefWJ8WIcDs8Bk8VXAwHyQ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM