How to retrieve the address from HOTEL JSON response ?

92 views
Skip to first unread message

Andi Hidajat

unread,
Feb 12, 2020, 3:49:56 AM2/12/20
to TripAdvisor API Developers
I'm learning android programming using android studio and now I'm trying to retrieve the hotel address from the JSON response to no avail.

my codes as follow:

json = new JSONObject(responseStr);
JSONArray jsonarr_1 = (JSONArray) json.get("data");
for(int i=0;i<jsonarr_1.length();i++)
    {
                        JSONObject result1 = jsonarr_1.getJSONObject( 0);
                        JSONObject geometry = result1.getJSONObject("result_object");
                        JSONObject locat = geometry.getJSONObject("address_obj");
                        alamat1 = locat.getString("street1");
                        alamat2 = locat.getString("street2");
                        kota = locat.getString("city");
                        negara = locat.getString("country");
                        kodepos = locat.getString("postalcode");


                        JSONObject c = jsonarr_1.getJSONObject(i);

                        JSONObject result_object = c.getJSONObject("result_object");

                        String namahotel1 = result_object.getString("name");
                        String lokasi     = result_object.getString("location_id");
                        String latitude   = result_object.getString("latitude");
                        String longitude  = result_object.getString("longitude");


                        hotelName = hotelName+ "Nama Hotel = "+ namahotel1 +"\n"+ "Lokasi = "+ lokasi + "\n"+
                                     "Latitude : "+ latitude+ "\n"+ "Longitude : "+ longitude  + "\n"+
                                    "Alamat : "+ alamat1+" "+ alamat2+"\n"+
                                    kota+"\n"+ negara+"\n"+ kodepos+"\n\n";

                    }
                    tvJSON.setText( hotelName);

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

When I run the application then the address of every hotel name will be the same. Then I try to change the following:
JSONObject result1 = jsonarr_1.getJSONObject( 0);

into :
JSONObject result1 = jsonarr_1.getJSONObject( i );

Then the application will show nohting.

Any help will be highly appreciated.

Regards,
Andi Hidajat
Reply all
Reply to author
Forward
0 new messages