I'm using Android Volley to fetch JSON objects from an API. I'm going to use this URL as a reference: https://glosbe.com/gapi/translate?from=la&dest=eng&format=json&phrase=puer&pretty=true
Basically, I'm trying to convert between Latin and English using this API. I'm having an issue where, when I fetch the JSON in my app, and display it using toString(), it doesn't show some content that I was able to see if I pulled it up in my browser. Here's what it looked like in my browser:
{
"result" : "ok",
"tuc" : [ {
"phrase" : {
"text" : "boy",
"language" : "en"
},
"meanings" : [ {
"language" : "en",
"text" : "male servant"
}, {
"language" : "en",
"text" : "young male"
}, {
"language" : "en",
"text" : "youth (1)"
} ],
"meaningId" : 5190805061708394514,
"authors" : [ 89651 ]
}, {
"phrase" : {
"text" : "child",
"language" : "en"
},
"meanings" : [ {
"language" : "en",
"text" : "a minor"
} ],
"meaningId" : -7607542873189716522,
"authors" : [ 89651 ]
}, {
"phrase" : {
"text" : "lad",
"language" : "en"
},
"meaningId" : -4906708417289394649,
"authors" : [ 66257 ]
}, {
"phrase" : {
"text" : "young",
"language" : "en"
},
"meanings" : [ {
"language" : "en",
"text" : "in the early part of life or growth"
} ],
"meaningId" : -3502972425310277951,
"authors" : [ 83058 ]
}, {
"phrase" : {
"text" : "laddie",
"language" : "en"
},
"meaningId" : 8881496802632467440,
"authors" : [ 36 ]
}, {
"phrase" : {
"text" : "son",
"language" : "en"
},
"meaningId" : -5777550766434985278,
"authors" : [ 25115 ]
}, {
"phrase" : {
"text" : "teenager",
"language" : "en"
},
"meanings" : [ {
"language" : "en",
"text" : "person aged between thirteen and nineteen"
} ],
"meaningId" : 3903113562290975250,
"authors" : [ 83058 ]
}, {
"phrase" : {
"text" : "young man",
"language" : "en"
},
"meaningId" : 8100655201654223005,
"authors" : [ 1490 ]
}, {
"phrase" : {
"text" : "youth",
"language" : "en"
},
"meanings" : [ {
"language" : "en",
"text" : "youth (1)"
} ],
"meaningId" : 3228296648002997345,
"authors" : [ 92945 ]
}, {
"phrase" : {
"text" : "attendant",
"language" : "en"
},
"meaningId" : -215343938140795468,
"authors" : [ 66257 ]
}, {
"phrase" : {
"text" : "children",
"language" : "en"
},
"meaningId" : -7262823696521965327,
"authors" : [ 66257 ]
}, {
"phrase" : {
"text" : "kid",
"language" : "en"
},
"meaningId" : 667611545945379095,
"authors" : [ 13 ]
}, {
"phrase" : {
"text" : "little son",
"language" : "en"
},
"meaningId" : 7069478616059810263,
"authors" : [ 66257 ]
}, {
"phrase" : {
"text" : "male child",
"language" : "en"
},
"meaningId" : -6364965082259980533,
"authors" : [ 66257 ]
}, {
"phrase" : {
"text" : "servant",
"language" : "en"
},
"meaningId" : 3306664352577272579,
"authors" : [ 66257 ]
}, {
"phrase" : {
"text" : "slave",
"language" : "en"
},
"meaningId" : 3485776351359445593,
"authors" : [ 66257 ]
}, {
"meanings" : [ {
"language" : "en",
"text" : "(in the plural) children"
}, {
"language" : "en",
"text" : "boyhood (ex: <i>in puero</i>, "in his boyhood" or "as a boy")"
}, {
"language" : "en",
"text" : "a child"
}, {
"language" : "en",
"text" : "a boy"
}, {
"language" : "en",
"text" : "a male servant"
}, {
"language" : "en",
"text" : "a bachelor"
}, {
"language" : "en",
"text" : "vocative singular of puer"
} ],
"meaningId" : -1803685626071521178,
"authors" : [ 1 ]
} ],
"phrase" : "puer",
"from" : "la",
"dest" : "en",
"authors" : {
"66257" : {
"U" : "",
"id" : 66257,
"N" : "Charlton T. Lewis",
"url" : "https://glosbe.com/source/66257"
},
"92945" : {
"U" : "",
"id" : 92945,
"N" : "CLLD Concepticon",
"url" : "https://glosbe.com/source/92945"
},
"1" : {
"U" : "http://en.wiktionary.org",
"id" : 1,
"N" : "en.wiktionary.org",
"url" : "https://glosbe.com/source/1"
},
"89651" : {
"U" : "",
"id" : 89651,
"N" : "GlosbeWordalignmentRnD",
"url" : "https://glosbe.com/source/89651"
},
"1490" : {
"U" : "http://www.archives.nd.edu/cgi-bin/words.exe",
"id" : 1490,
"N" : "Piotr Szelma",
"url" : "https://glosbe.com/source/1490"
},
"83058" : {
"U" : "",
"id" : 83058,
"N" : "Dbnary: Wiktionary as Linguistic Linked Open Data",
"url" : "https://glosbe.com/source/83058"
},
"36" : {
"U" : "http://freedict.org",
"id" : 36,
"N" : "freedict.org",
"url" : "https://glosbe.com/source/36"
},
"25115" : {
"U" : "",
"id" : 25115,
"N" : "GlTrav3",
"url" : "https://glosbe.com/source/25115"
},
"13" : {
"U" : "http://ro.wiktionary.org",
"id" : 13,
"N" : "ro.wiktionary.org",
"url" : "https://glosbe.com/source/13"
}
}
}
When I pull it up in my app, it doesn't show the "phrase" portion. Here's the code that I used:
RequestQueue mRequestQueue;
Cache cache = new DiskBasedCache(getActivity().getCacheDir(), 1024 * 1024); // 1MB cap
Network network = new BasicNetwork(new HurlStack());
mRequestQueue = new RequestQueue(cache, network);
mRequestQueue.start();
String url = "https://glosbe.com/gapi/translate?from=la&dest=eng&format=json&phrase=" + e.getText().toString();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response)
{
Log.d("JSON", response.toString());
try
{
JSONArray arr = response.getJSONArray("tuc");
try
{
JSONObject phrase = response.getJSONObject("phrase");
}
catch(JSONException e)
{
Toast.makeText(getActivity(), "Phrase not found", Toast.LENGTH_LONG).show();
}
Log.d("JSON", arr.toString());
try
{
JSONArray arr2 = arr.getJSONObject(0).getJSONArray("meanings");
definition = arr2.getJSONObject(0).getString("text");
t.setText(definition);
Toast.makeText(getActivity(), "Got stuff sucessfully", Toast.LENGTH_LONG).show();
}
catch (JSONException e)
{
JSONObject phrase = arr.getJSONObject(0);
definition = phrase.getString("text");
t.setText(definition);
Toast.makeText(getActivity(), "Got stuff succesfully with exception", Toast.LENGTH_LONG).show();
}
}
catch(JSONException e)
{
Log.d("error", e.toString());
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error)
{
Log.d("Error", error.toString());
}
});
MySingleton.getInstance(getActivity()).addToRequestQueue(jsonObjectRequest);
The code you're using to parse it doesn't match the structure of the JSON.
"tuc" is an array, "phrase" and "meanings" are inside the objects in that array. Let's say you wanted to get just the first phrase:
JSONArray tuc = response.getJSONArray("tuc");
JSONObject phrase = tuc.getJSONObject(0).getJSONObject("phrase");
I think that's right but I don't have an Android project set up to test it at the moment.
Ok here is the code to parse your response in android:
try
{
JSONArray TUCarray = response.getJSONArray("tuc");
JsonObject phraseobject=TUCarray.getJSONObject(0).getJSONObject("phrase");
}
catch(JSONException e)
{
Toast.makeText(getActivity(), "Phrase not found",Toast.LENGTH_LONG).show();
}
Be careful when you are parsing the whole response because I could see that some objects of TUC array doesnt have sub object phrase eg:- Last object of the TUC array in the response of link you provided).
Related
I am trying to parse this file.
I am new to maps API so.
I am trying to parse this set into recyclerview
I trying to parse using String Request that works but I need json object to store data.
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "Ahmedabad",
"short_name" : "Ahmedabad",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Suradhara Society",
"short_name" : "Suradhara Society",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Khodiar Nagar",
"short_name" : "Khodiar Nagar",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Ahmedabad",
"short_name" : "Ahmedabad",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Gujarat",
"short_name" : "GJ",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
},
{
"long_name" : "382350",
"short_name" : "382350",
"types" : [ "postal_code" ]
}
],
"adr_address" : "3rd Floor, Sardar Patel Mall, Nikol Gam Road, \u003cspan class=\"street-address\"\u003eBapunagar\u003c/span\u003e, \u003cspan class=\"extended-address\"\u003eSuradhara Society, Khodiar Nagar\u003c/span\u003e, \u003cspan class=\"locality\"\u003eAhmedabad\u003c/span\u003e, \u003cspan class=\"region\"\u003eGujarat\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e382350\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eIndia\u003c/span\u003e",
"formatted_address" : "3rd Floor, Sardar Patel Mall, Nikol Gam Road, Bapunagar, Suradhara Society, Khodiar Nagar, Ahmedabad, Gujarat 382350, India",
"formatted_phone_number" : "085116 15040",
"geometry" : {
"location" : {
"lat" : 23.0379687,
"lng" : 72.6433222
},
"viewport" : {
"northeast" : {
"lat" : 23.0393176802915,
"lng" : 72.64467118029151
},
"southwest" : {
"lat" : 23.0366197197085,
"lng" : 72.64197321970849
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "7ddb8968d27481b07a700f6e34ac5442f528eaf2",
"international_phone_number" : "+91 85116 15040",
"name" : "Parsana Gym",
"photos" : [
{
"height" : 2322,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/104290859169763692879/photos\"\u003eAlpesh Patel\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAAgWV9QS54sRCALTitzLBc7LowfL3gKMw_jbNaTSmeFM9GqqG6iu1hx4HJwj332LJQHpfk44xeAPIgYg0mexUJaoWd8mKIUt4oTUj0f-DvbmnrOG67Q5BwAh9eP6VjcPz3EhAHDkHpn52M8rURjiAncwzrGhTLyHQZ5qHXowonHaOnDvld63wrXg",
"width" : 4128
}
],
"place_id" : "ChIJEbpNLyCHXjkRny4b5TptHsY",
"plus_code" : {
"compound_code" : "2JQV+58 Ahmedabad, Gujarat, India",
"global_code" : "7JMJ2JQV+58"
},
"rating" : 4.4,
"reference" : "ChIJEbpNLyCHXjkRny4b5TptHsY",
"reviews" : [
{
"author_name" : "Sumit Patel",
"author_url" : "https://www.google.com/maps/contrib/101190972481759472779/reviews",
"language" : "en",
"profile_photo_url" : "https://lh3.googleusercontent.com/-70hcgZha3k4/AAAAAAAAAAI/AAAAAAAAAAA/ABtNlbDZJQHYZ86bOPzJBQjG8VNA0_Sh6g/s128-c0x00000000-cc-rp-mo/photo.jpg",
"rating" : 5,
"relative_time_description" : "4 months ago",
"text" : "gym is good and trainer are also good.\nbut some new machine are require for excersize.",
"time" : 1527730807
},
{
"author_name" : "Mahesh Bhayani",
"author_url" : "https://www.google.com/maps/contrib/105571914899766322779/reviews",
"language" : "en",
"profile_photo_url" : "https://lh3.googleusercontent.com/-EKevbjOaZsw/AAAAAAAAAAI/AAAAAAAAA4o/k75Wr5thZvg/s128-c0x00000000-cc-rp-mo/photo.jpg",
"rating" : 5,
"relative_time_description" : "4 months ago",
"text" : "Good atmosfiyar for parsana gym. Good service provide good trinar.",
"time" : 1527820640
},
{
"author_name" : "Nadiya Dipak",
"author_url" : "https://www.google.com/maps/contrib/100514831030336220877/reviews",
"language" : "en",
"profile_photo_url" : "https://lh6.googleusercontent.com/-he9oXwcUqLI/AAAAAAAAAAI/AAAAAAAAAAA/ABtNlbBq3ow6IHVx5QNQktTbXQEDI2v2xw/s128-c0x00000000-cc-rp-mo/photo.jpg",
"rating" : 5,
"relative_time_description" : "4 months ago",
"text" : "Parsana gym is very good sarvice provide . Nice team work",
"time" : 1527738686
}
],
"scope" : "GOOGLE",
"types" : [ "gym", "health", "point_of_interest", "establishment" ],
"url" : "https://maps.google.com/?cid=14275967968530345631",
"utc_offset" : 330,
"vicinity" : "3rd Floor, Sardar Patel Mall, Nikol Gam Road, Bapunagar, Ahmedabad"
},
"status" : "OK"
}
I use volley that gives parse exception.
Here is my code
I am trying to parse JSON format using volley using below code.
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject object = response.getJSONObject(i);
JSONObject result = object.getJSONObject("result");
JSONArray array = result.getJSONArray("address_components");
Log.d("result", "" + array.length());
for (int adress = 0; adress < array.length(); adress++) {
JSONObject object1 = array.getJSONObject(i);
Log.d("result", object1.getString("long_name"));
Log.d("result", object1.getString("short_name"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
//Error lisnter for request
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("error", error.getMessage());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest );
Can Anyone help what I am missing in this?
here is exception.
Note-: org.json.JSONException: Value {"html_attributions":[],"result":{"address_components":[{"long_name":"Ahmedabad","short_name":"Ahmedabad","t.........}
Thanks in advance !!
Do this :
your response starts with jsonObject'{' not jsonArray'['
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONObject jsonObject = response.getJSONObject("result");
JSONArray jsonArray = jsonObject.getJSONArray("address_components");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
Log.d("result", object.getString("long_name"));
Log.d("result", object.getString("short_name"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
//Error lisnter for request
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("error", error.getMessage());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest );
i am getting some paring error while running this code but i can not tell what is the problem .there is no error in the android studio syntax but there is a problem in the json parsing or with my Json as i am not able to understand how to call json object inside another json object and json arrays
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.ImageView;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import org.json.JSONObject;
public class BookFragment extends Fragment {
OneRestaurantBean restaurentinfo;
String placid="ChIJO3JIy9iDGjkRIekztZhsmGg";
PhotosBean photolist;
ReviewBean reviewlist;
StringRequest stringRequest;
Spinner table_for;
PhotosBean photolist;
ReviewBean reviewlist;
private String format = "";
TextView date,time;
int day,month,year,hour,minute;
String url="https://maps.googleapis.com/maps/api/place/details/json?placeid="+placeid+"&key=AIzaSyDGtQbIzeOGRk0kGq3rCy3xAvo1xVNB304";
TextView txtaddress,txtphone,txttiming;
ImageView image1,image2,image3;
// Executes the Request
RequestQueue requestQueue;
ProgressDialog pd;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_book, container, false);
requestQueue = Volley.newRequestQueue(getActivity());
table_for=(Spinner)view.findViewById(R.id.spinner);
date=(TextView)view.findViewById(R.id.textViewdate) ;
// timePicker1=(TimePicker)view.findViewById(R.id.timePicker2);
time=(TextView)view.findViewById(R.id.textviewtime);
txtaddress=(TextView)view.findViewById(R.id.textViewaddress);
txtphone=(TextView)view.findViewById(R.id.textViewphone);
txttiming=(TextView)view.findViewById(R.id.textViewTiming);
image1=(ImageView)view.findViewById(R.id.imageView2);
image2=(ImageView)view.findViewById(R.id.imageView3);
image3=(ImageView)view.findViewById(R.id.imageView4);
init();
getRestaurentsinfo();
return view;
}
private void init() {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.Table_for, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
table_for.setAdapter(adapter);
}
private void getRestaurentsinfo() {
stringRequest = new StringRequest(com.android.volley.Request.Method.GET, url,
new com.android.volley.Response.Listener<String>() {
#Override
public void onResponse(String s) {
// pd.dismiss();
try {
JSONObject jsonObject = new JSONObject(s);
Gson gson = new Gson();
restaurentinfo = gson.fromJson(jsonObject.toString(), OneRestaurantBean.class);
Type listType = new TypeToken<List<PhotosBean>>(){}.getType();
photolist = (PhotosBean) gson.fromJson(jsonObject.toString(), listType);
Type listType2 = new TypeToken<List<ReviewBean>>(){}.getType();
reviewlist = (ReviewBean) gson.fromJson(jsonObject.toString(), listType2);
txtaddress.setText(restaurentinfo.getVicinity()); txttiming.setText(Arrays.toString(restaurentinfo.getOpenhour().getWeekday_text()).replaceAll("\\[|\\]", ""));
} catch (Exception e) {
Toast.makeText(getActivity(), "Some JSON Parsing Error", Toast.LENGTH_LONG).show();
}
}
},
// failure
new com.android.volley.Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(getActivity(), "Some Volley Error", Toast.LENGTH_LONG).show();
}
}
);
requestQueue.add(stringRequest);
}
my json file is as displayed below
{
"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" : false,
"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" : "CoQBdwAAAIhIrZLQ_Z3H9UUm8Hu96z7N5L4HdKPd_IJbGNEfzW_v2c6zNe3bSYoz3Ya2XkoBHM_9bXBmKbw72d8N_vmFPyT14uqWCHz_WuLWGp_K0Ss-u7VCghzyUpVZ1F5-asNOeTCNLOwjre0FdO8rQsgb4slyJhMGgJg8Okogx_jlShuLEhDdW69Zmuh-aB-SCdI0XdFDGhT4zjH4vKwmgNYkWdcnY53bwD4eaA",
"width" : 3264
},
{
"height" : 600,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/109327830988106108854/photos\"\u003eNirwal Karm\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAAPpyo99c8N-lt7F-l3YA7qSK7mZKY8zIyaMUoV3OqgOJ9DXVqspqpMQwIvu7KDycvrWcHpy6jaCpc2UyDm3SqtYhw2Fsr5IfgKjKbRTjDF-ST5a9w1vNs44O3SbHWwSLuKV6fhyax1xfqc2TEM7XqAE3K2XSOg5XCzgD8Nef3NJuEhAhTK3UI_yW-dim4bXP4ZK0GhQCO7kgf4E1f53t-_Tf4DEsjYF5xw",
"width" : 800
}
],
"place_id" : "ChIJAQAAwM2DGjkR6gVuwpeWwKk",
"price_level" : 1,
"rating" : 4.1,
"reference" : "CmRSAAAATiwel1VKVuyHEFkgl2LEMsL_Rxf9xV3EXj_N_o59Q0ZWGgy-XRjCTmFAPK6z1xRof9WpAKu9FTu-XaNyx6zxGlnzXVNRf-KUOjw-xBB5YdrdDN1VW7eN3E-qw6-pKdXREhCr9EeBMjBidk_KW74s9S9aGhS4NLG4UNMqWveymd0sBkpcieyybg",
"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" : 0,
"type" : "overall"
}
],
"author_name" : "Gurmeet Gujral",
"author_url" : "https://plus.google.com/113150303121450342768",
"language" : "en",
"profile_photo_url" : "//lh3.googleusercontent.com/-zzCCjI2B4HU/AAAAAAAAAAI/AAAAAAAAJ0w/JOBa4Aetq3E/photo.jpg",
"rating" : 1,
"text" : "Don't call them for home delivery. They will serve you cold",
"time" : 1439473587
}
],
"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"
}
I found error in json parsing,use some other parsing method such as
Retrofit . Volley JSON Parsing Sample Example
http://www.androidhive.info/2014/09/android-json-parsing-using-volley/
I am using the original retrofit to call the Instagram api. The call returns this reponse
{ "data" : [ { "attribution" : null,
"caption" : { "created_time" : "1460953831",
"from" : { "full_name" : "Edward ♕",
"id" : "282525924",
"profile_picture" : "https://scontent.cdninstagram.com/t51.2885-19/s150x150/12519400_165282517199271_1363863979_a.jpg",
"username" : "manlike_eddy"
},
"id" : "17847512245075242",
"text" : "Oh girl"
},
"comments" : { "count" : 1,
"data" : [ { "created_time" : "1460953885",
"from" : { "full_name" : "Edward ♕",
"id" : "282525924",
"profile_picture" : "https://scontent.cdninstagram.com/t51.2885-19/s150x150/12519400_165282517199271_1363863979_a.jpg",
"username" : "manlike_eddy"
},
"id" : "17847512257075242",
"text" : "#love #TagsForLikes #TagsForLikesApp #instagood #me #smile #follow #cute #photooftheday #tbt #followme #girl #beautiful #happy #picoftheday #instadaily #food #swag #amazing #TFLers #fashion #igers #fun #summer #instalike #bestoftheday #smile #like4like #friends #instamood"
} ]
},
"created_time" : "1460953831",
"filter" : "Normal",
"id" : "1230892414266144399_282525924",
"images" : { "low_resolution" : { "height" : 320,
"url" : "https://scontent.cdninstagram.com/t51.2885-15/s320x320/e35/12965764_932183320233898_1928407701_n.jpg?ig_cache_key=MTIzMDg5MjQxNDI2NjE0NDM5OQ%3D%3D.2.l",
"width" : 320
},
"standard_resolution" : { "height" : 640,
"url" : "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/12965764_932183320233898_1928407701_n.jpg?ig_cache_key=MTIzMDg5MjQxNDI2NjE0NDM5OQ%3D%3D.2.l",
"width" : 640
},
"thumbnail" : { "height" : 150,
"url" : "https://scontent.cdninstagram.com/t51.2885-15/s150x150/e35/c257.0.565.565/12950340_1566398773652655_1170974002_n.jpg?ig_cache_key=MTIzMDg5MjQxNDI2NjE0NDM5OQ%3D%3D.2.c",
"width" : 150
}
},
"likes" : { "count" : 0,
"data" : [ ]
},
"link" : "https://www.instagram.com/p/BEVAiEVDnaP/",
"location" : null,
"tags" : [ "beautiful",
"cute",
"fashion",
"love",
"summer",
"food",
"instalike",
"tbt",
"tagsforlikesapp",
"igers",
"follow",
"instadaily",
"instamood",
"friends",
"girl",
"me",
"swag",
"like4like",
"tflers",
"followme",
"instagood",
"tagsforlikes",
"amazing",
"bestoftheday",
"fun",
"smile",
"photooftheday",
"picoftheday",
"happy"
],
"type" : "image",
"user" : { "full_name" : "Edward ♕",
"id" : "282525924",
"profile_picture" : "https://scontent.cdninstagram.com/t51.2885-19/s150x150/12519400_165282517199271_1363863979_a.jpg",
"username" : "manlike_eddy"
},
"user_has_liked" : false,
"users_in_photo" : [ ]
} ],
}
}
As you can see some of the labels are using snakecase, such as standard_resolution. When I plug this data into Json Pogo it creates two classes for standard_resolution. One named StandardResolution and one StandardResolution. When I call .getStandardResolution() on the respective object null is returned, even though it should have valid data.
I'm a beginner in android development ... i want to know how to create a json structure, with using arraylists and hashmaps ... you can find the structure as follows ...
NOTE: I don't want to use JSONArray or JSONObject
[
[
{
"blog_owner" : "user",
"icalIssueEventID" : "",
"updated_date" : "2014-02-17 07:05:53 +0000",
"icalEventID" : "",
"blog_stage" : "Cancelled",
"blog_ID" : "GMpk20140206160708281",
"created_date" : "2014-02-06 10:37:38 +0000",
"emailFlag" : false
}
],
[
{
"blog_iNotes_ID" : "",
"blog_name" : "",
"blog_status" : "",
"blog_duration" : ""
},
{
"pid" : "0",
"isOpen" : true,
"id" : "R1",
"level" : 0,
"value" : [
{
"level" : 0,
"id" : "MA20140206181038936",
"type" : "meetingAgenda",
"isOpen" : true,
"value" : [
{
"pid" : "MA20140206181038936",
"level" : 1,
"id" : "CN20140207211127374",
"isOpen" : false,
"value" : [
],
"line_number" : 13,
"keyItem" : "Fgh",
"isCategorySelected" : false
},
{
"line_number" : 14,
"level" : 1,
"id" : "CN20140217123343769",
"isOpen" : true,
"value" : [
],
"keyItem" : "Fghh",
"isCategorySelected" : false,
"pid" : "MA20140206181038936"
},
{
"pid" : "MA20140206181038936",
"level" : 1,
"id" : "ISS20140217123542229",
"isOpen" : true,
"type" : "Issues",
"value" : [
],
"type_KeyItem" : {
"tfImpact" : "",
"tvIssueDescription" : "",
"tfActionRequired" : "Ffg",
"tfResponsible" : "",
"targetDate" : "Month DD YYYY"
},
"keyItem" : "Issues",
"isCategorySelected" : true,
"line_number" : 15
}
],
"type_KeyItem" : {
"tfAgendaTitle" : "New agenda for magic minutes",
"lblDuration" : "00:00",
"tfAgendaName" : ""
},
"line_number" : 11,
"isCategorySelected" : false,
"pid" : "R1"
}
],
"isCategorySelected" : false,
"keyItem" : "root",
"line_number" : 10
}
],
[
]
]
You can use google-gson library for converting JSON to Java objects and vice-versa.
Some other helpful links are :
1) http://www.javacreed.com/simple-gson-example/
2) http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
Hope this will help you.
I have a JSON file with lots of attributes. I've parsed JSON files before using the methods provided by the Android SDK. But I have absolutely no clue on how to handle this file because the attributes are in arrays.
Anyone has a guess?
{
"properties": {
"position" : { "valueType": "text" },
"image" : { "valueType": "text" },
"architekt" : { "valueType": "text" },
"baustil" : { "valueType": "text" },
"besuchbar" : { "valueType": "text" },
"bildbeschreibung" : { "valueType": "text" },
"bundesland" : { "valueType": "text" },
"eintrittspreis" : { "valueType": "text" },
"geo_lat" : { "valueType": "text" },
"geo_lon" : { "valueType": "text" },
"hoehe" : { "valueType": "text" },
"internetpraesenz" : { "valueType": "text" },
"land" : { "valueType": "text" },
"oeffnungszeiten" : { "valueType": "text" },
"ort" : { "valueType": "text" },
"parkplaetze" : { "valueType": "text" },
"plz" : { "valueType": "text" },
"sehenswuerdigkeit" : { "valueType": "text" },
"strasse" : { "valueType": "text" }
},
"items": [
{
"label": "Assf",
"position": ["Testroute::2","Rathausroute::2"],
"image": [" http:\/\/134.108.119.74\/mediawiki-1.18.2\/index.php\/Datei:Rathaus_Goeppingen.png"],
"bildbeschreibung": ["Vorderansicht des Rathaus G\u00f6ppingen"],
"sehenswuerdigkeit": ["Rathaus G\u00f6ppingen"]
},
{
"label": "Burg Hohenstaufen",
"position": ["Testroute::2","Rathausroute::13"],
"image": [" http:\/\/134.108.119.74\/mediawiki-1.18.2\/index.php\/Datei:Burg Hohenstaufen Fresko.jpg"],
"architekt": ["Friedrich I. von Schwaben"],
"baustil": ["H\u00f6henburg, Gipfellage"],
"besuchbar": ["wahr"],
"bildbeschreibung": ["\u00c4lteste Abbildung der Burg Hohenstaufen aus dem Jahre 1470"],
"bundesland": ["Baden-W\u00fcrttemberg"],
"eintrittspreis": ["unentgeltlich besuchbar"],
"geo_lat": ["48.743056"],
"geo_lon": ["9.716389"],
"hoehe": ["19 Meter"],
"internetpraesenz": ["http:\/\/www.awdfasdfa.de"],
"land": ["Germany"],
"oeffnungszeiten": ["Immer Ge\u00f6ffnet"],
"ort": ["G\u00f6ppingen Hohenstaufen"],
"parkplaetze": ["falsch"],
"plz": ["73037"],
"sehenswuerdigkeit": ["Burg Hohenstaufen"]
},
{
"label": "Test",
"position": ["Testroute::1","Rathausroute::23"],
"image": [" http:\/\/134.108.119.74\/mediawiki-1.18.2\/index.php\/Datei:Rathaus_Goeppingen.png"],
"architekt": ["Joseph Poelaert"],
"baustil": ["Barock"],
"besuchbar": ["wahr"],
"bildbeschreibung": ["Vorderansicht des Rathaus G\u00f6ppingens"],
"bundesland": ["Baden-W\u00fcrttemberg"],
"eintrittspreis": ["f\u00fcr Umme"],
"geo_lat": ["48.7025"],
"geo_lon": ["9.65277"],
"hoehe": ["19 Meter"],
"internetpraesenz": ["http:\/\/www.awdfasdfa.de"],
"land": ["Germany"],
"oeffnungszeiten": ["Mo: 9:30 bis 13:30 und 14:00 bis 18:30 <br \/> Sa&So: 10:00 bis 20:00"],
"ort": ["G\u00f6ppingen"],
"parkplaetze": ["wahr"],
"plz": ["70825"],
"sehenswuerdigkeit": ["Rathaus G\u00f6ppingen"],
"strasse": ["Robert-Bosch-str. 12"]
}
]
}
Its pretty easy
json = new JSONObject(textThatISJSON);
JSONArray properties = json.getJSONArray("properties");
JSONArray position = properties.getJSONArray("position");