如何从Google Place Api中提取内部数组数据

时间:2016-11-19 09:34:16

标签: android arrays json gson google-places-api

我很难从这个文件中提取数据。我正在使用Volley发送请求和JSON来为我的Android应用程序提取这些材料。我能够获得第一个JSON数组,即name,id,place_id,phone等。但我无法从JSON数组中获取数据作为每个人的评论,weekday_text,照片等。所以请帮助我告诉我如何获得所需的信息。谢谢。

 {
  "html_attributions" : [],
    "result" : {
       "address_components" : [
     {
        "long_name" : "Ludhiana",
        "short_name" : "Ludhiana",
        "types" : [ "locality", "political" ]
     },
     {
        "long_name" : "Sarabha Nagar",
        "short_name" : "Sarabha Nagar",
        "types" : [ "sublocality_level_1", "sublocality", "political" ]
     },
     {
        "long_name" : "Ludhiana",
        "short_name" : "Ludhiana",
        "types" : [ "administrative_area_level_2", "political" ]
     },
     {
        "long_name" : "Punjab",
        "short_name" : "PB",
        "types" : [ "administrative_area_level_1", "political" ]
     },
     {
        "long_name" : "India",
        "short_name" : "IN",
        "types" : [ "country", "political" ]
     },
     {
        "long_name" : "141003",
        "short_name" : "141003",
        "types" : [ "postal_code" ]
     }
  ],
  "adr_address" : "SCF-21, Sarabha Nagar Market, \u003cspan class=\"extended-address\"\u003eSarabha Nagar\u003c/span\u003e, \u003cspan class=\"locality\"\u003eLudhiana\u003c/span\u003e, \u003cspan class=\"region\"\u003ePunjab\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e141003\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eIndia\u003c/span\u003e",
  "formatted_address" : "SCF-21, Sarabha Nagar Market, Sarabha Nagar, Ludhiana, Punjab 141003, India",
  "formatted_phone_number" : "0161 245 2810",
  "geometry" : {
     "location" : {
        "lat" : 30.8927238,
        "lng" : 75.8218431
     }
  },
  "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
  "id" : "17865244baa6a88165cbe83f21d2713656bcece0",
  "international_phone_number" : "+91 161 245 2810",
  "name" : "Domino's Pizza",
  "opening_hours" : {
     "open_now" : true,
     "periods" : [
        {
           "close" : {
              "day" : 0,
              "time" : "2300"
           },
           "open" : {
              "day" : 0,
              "time" : "1100"
           }
        },
        {
           "close" : {
              "day" : 1,
              "time" : "2300"
           },
           "open" : {
              "day" : 1,
              "time" : "1100"
           }
        },
        {
           "close" : {
              "day" : 2,
              "time" : "2300"
           },
           "open" : {
              "day" : 2,
              "time" : "1100"
           }
        },
        {
           "close" : {
              "day" : 3,
              "time" : "2300"
           },
           "open" : {
              "day" : 3,
              "time" : "1100"
           }
        },
        {
           "close" : {
              "day" : 4,
              "time" : "2300"
           },
           "open" : {
              "day" : 4,
              "time" : "1100"
           }
        },
        {
           "close" : {
              "day" : 5,
              "time" : "2300"
           },
           "open" : {
              "day" : 5,
              "time" : "1100"
           }
        },
        {
           "close" : {
              "day" : 6,
              "time" : "2300"
           },
           "open" : {
              "day" : 6,
              "time" : "1100"
           }
        }
     ],
     "weekday_text" : [
        "Monday: 11:00 AM – 11:00 PM",
        "Tuesday: 11:00 AM – 11:00 PM",
        "Wednesday: 11:00 AM – 11:00 PM",
        "Thursday: 11:00 AM – 11:00 PM",
        "Friday: 11:00 AM – 11:00 PM",
        "Saturday: 11:00 AM – 11:00 PM",
        "Sunday: 11:00 AM – 11:00 PM"
     ]
  },
  "photos" : [
     {
        "height" : 1836,
        "html_attributions" : [
           "\u003ca href=\"https://maps.google.com/maps/contrib/102359458524790058006/photos\"\u003eKAMALJEET SINGH RANDHAWA\u003c/a\u003e"
        ],
        "photo_reference" : "CoQBdwAAAOv9Qu7EPWdGvskFjVRJmw_unwmcxcIAbWz6E3CeAnK0gOkM09iAnIPIosfeF6Yu8Q028XVsFR1zkkty_OWaqSUrLOMHB642vaJbVp2KC4kbmR1n9fDXYbIVqFDAe6_kr0xJ-vwUlhnLqmFdKyoKBrnUs8yBrcTOM5g8rKD3axXoEhDayDn9FMAjUoSrS9w5hZfqGhSaKbtP3Uwb1VyqtAEbT1fUhP28gA",
        "width" : 3264
     },
     {
        "height" : 600,
        "html_attributions" : [
           "\u003ca href=\"https://maps.google.com/maps/contrib/109327830988106108854/photos\"\u003eNirwal Karm\u003c/a\u003e"
        ],
        "photo_reference" : "CoQBdwAAAFrrvfzgLXjusY8jtQ13hA1NR8jxlE3Uhc8jKbYtqS-d0hL1qvBpU83GAKrHiHBaUySyZNKZLg5zkZnPpAnKZtaRaJwSdaXBLQvRZLFpEx2xLbErcsk430vkwVrJGPkppnRy6cIQ-EyEutc1Py88r0xnMFQMzGj2Kh4zJX5XFMO3EhAEj4uscqfbaTOb-RQ6CHpZGhRawMIRnMnrd5nOEIO5pLOMz-seUQ",
        "width" : 800
     }
  ],
  "place_id" : "ChIJAQAAwM2DGjkR6gVuwpeWwKk",
  "price_level" : 1,
  "rating" : 4.1,
  "reference" : "CmRSAAAA1iZluSdwF3H2ljd9mSNSR8HwSxSPI-rGGfeqdUcy1HvFqaDx36qPL0N1SNCDrmDVMuhaOWUm7dmNMdEAEw2RR8oewRev5HmjJwh1Mj_uPzCFueZ-Edm8SMpuvLoy4vq9EhA6RGK9EpUF_pbAmMLTvojnGhTox_nOXHZ4v8QByb6e7-ahGm1B3w",
  "reviews" : [
     {
        "aspects" : [
           {
              "rating" : 2,
              "type" : "overall"
           }
        ],
        "author_name" : "Vardaan Sharma",
        "author_url" : "https://plus.google.com/116622040002550992578",
        "language" : "en",
        "profile_photo_url" : "//lh4.googleusercontent.com/-oc3UC8zHbqE/AAAAAAAAAAI/AAAAAAAACDE/eD2qok-jrMQ/photo.jpg",
        "rating" : 4,
        "text" : "Love The Garlic Bread!! Awesome Taste for Decent Prices!!!",
        "time" : 1465754538
     },
     {
        "aspects" : [
           {
              "rating" : 0,
              "type" : "overall"
           }
        ],
        "author_name" : "Darleen Grewal",
        "author_url" : "https://plus.google.com/102174339060680368258",
        "language" : "en",
        "rating" : 1,
        "text" : "Pathetic delivery service , orders delayed by one and a half hour and staff is uneducated and non Cooperating",
        "time" : 1476727634
     },
     {
        "aspects" : [
           {
              "rating" : 2,
              "type" : "overall"
           }
        ],
        "author_name" : "Abhishek Attri",
        "author_url" : "https://plus.google.com/103724862705815258945",
        "language" : "en",
        "profile_photo_url" : "//lh6.googleusercontent.com/-8XVYso4qYxc/AAAAAAAAAAI/AAAAAAAAHIA/63UVRnCpc8M/photo.jpg",
        "rating" : 4,
        "text" : "Nice place.. ",
        "time" : 1478198571
     },
     {
        "aspects" : [
           {
              "rating" : 0,
              "type" : "overall"
           }
        ],
        "author_name" : "Bhavesh Bansal",
        "author_url" : "https://plus.google.com/107902290149968294793",
        "language" : "en",
        "profile_photo_url" : "//lh5.googleusercontent.com/-0GAn_nkPa9g/AAAAAAAAAAI/AAAAAAAAAro/1Z2WU1Tup7g/photo.jpg",
        "rating" : 1,
        "text" : "Poor service. Last 2 orders have been delayed by almost 2 hours. All their contact numbers are unreachable or busy, when you try to contact them.\nSeems like they dont care anymore about customer",
        "time" : 1463848060
     },
     {
        "aspects" : [
           {
              "rating" : 1,
              "type" : "overall"
           }
        ],
        "author_name" : "Pavail Sra",
        "author_url" : "https://plus.google.com/103436119468313848756",
        "language" : "en",
        "profile_photo_url" : "//lh4.googleusercontent.com/-KlR-ExeI5RI/AAAAAAAAAAI/AAAAAAAAAk0/oXAeytdPdmU/photo.jpg",
        "rating" : 3,
        "text" : "Crowded and slow service",
        "time" : 1476069515
     }
  ],
  "scope" : "GOOGLE",
  "types" : [
     "meal_delivery",
     "meal_takeaway",
     "restaurant",
     "food",
     "point_of_interest",
     "establishment"
  ],
  "url" : "https://maps.google.com/?cid=12231942166484485610",
  "utc_offset" : 330,
  "vicinity" : "SCF-21, Sarabha Nagar Market, Ludhiana",
  "website" : "http://www.dominos.co.in/"
    },
    "status" : "OK"
 }

2 个答案:

答案 0 :(得分:1)

您可以使用JsonParser反序列化。我在下面写一个样本。但是我建议你用这种方式https://stackoverflow.com/a/2865580/1562723来让Gson对json进行deseriliaze。

JsonElement jelement = new JsonParser().parse(jsonLine);
JsonObject  jobject = jelement.getAsJsonObject();
jobject = jobject.getAsJsonObject("result");

/*Review*/
JsonArray jarray = jobject.getAsJsonArray("reviews");
/*For inside*/
JsonObject jreview = jarray.get(0).getAsJsonObject();
String authorName = jreview.get("author_name").toString();

/*Opening hours*/
JsonObject opObject = jobject.getAsJsonObject("opening_hours");
JsonArray jWeekDayArray = jobject.getAsJsonArray("weekday_text");
String weekdayText = jWeekDayArray.get(0).toString();

<强>更新

如果您不想使用反斜杠或想要使用getAsString方法获取解析数据,

jreview.get("author_name").getAsString();

答案 1 :(得分:0)

你可能正在使用JSONrequest,我建议在Volley中使用 StringRequest 。从这里你可以得到你的响应字符串然后你可以把它当作JSONArray,JSONObject或任何你需要的。

注意:检查响应是否为JSON数组。我认为(如响应中所示)它是JSONObject。所以StringRequest在这里会更好。