Related
I excuted the followinf api request
https://api-v3.mbta.com/predictions?sort=arrival_time&filter%5Bstop%5D=70028%2C70029
and I am having the following parsing problem.
Failed to parse JSON
org.json.JSONException: Value [{"attributes":{"arrival_time":"2018-04-25T13:20:01-04:00","departure_time":"2018-04-25T13:20:01-04:00",
"direction_id":1,"schedule_relationship":null,"status":null,"stop_sequence":140,"track":null}
,"id":"prediction-36315805-70029-140","relationships":{"route":{"data":{"id":"Orange","type":"route"}},"stop":
{"data":{"id":"70029","type":"stop"}},"trip":{"data":{"id":"36315805","type":"trip"}}},"type":"prediction"},{"attributes"
:{"arrival_time":"2018-04-25T13:26:03-04:00","departure_time":"2018-04-25T13:26:03-04:00","direction_id":0,"schedule_relationship":null,
"status":null,"stop_sequence":50,"track":null},"id":"prediction-36315802-70028-50","relationships":{"route":{"data":{"id":"Orange","type":"route"}},"stop":{"data":{"id":"70028","type":"stop"}},"trip":{"data":{"id":"36315802","type":"trip"}}},"type":"prediction"},{"attributes":{"arrival_time":"2018-04-25T13:29:28-04:00","departure_time":"2018-04-25T13:29:28-04:00","direction_id":1,"schedule_relationship":null,"status":null,"stop_sequence":140,"track":null},"id":"prediction-36315806-70029-140","relationships":{"route":{"data":{"id":"Orange","type":"route"}},"stop":{"data":{"id":"70029","type":"stop"}},"trip":{"data":{"id":"36315806","type":"trip"}}},"type":"prediction"},{"attributes":{"arrival_time":"2018-04-25T13:33:00-04:00","departure_time":"2018-04-25T13:33:00-04:00","direction_id":0,"schedule_relationship":null,"status":null,"stop_sequence":50,"track":null},"id":"prediction-36315803-70028-50","relationships":{"route":{"data":{"id":"Orange","type":"route"}},"stop":{"data":{"id":"70028","type":"stop"}},"trip":{"data":{"id":"36315803","type":"trip"}}},"type":"prediction"},{"attributes":{"arrival_time":"2018-04-25T13:36:20-04:00","departure_time":"2018-04-25T13:36:20-04:00","direction_id":1,"schedule_relationship":null,"status":null,"stop_sequence":140,"track":null},"id":"prediction-36315807-70029-140","relationships":{"route":{"data":{"id":"Orange","type":"route"}},"stop":{"data":{"id":"70029","type":"stop"}},"trip":{"data":{"id":"36315807","type":"trip"}}},"type":"prediction"},{"attributes":{"arrival_time":"2018-04-25T13:44:44-04:00","departure_time":"2018-04-25T13:44:44-04:00","direction_id":0,"schedule_relationship":"ADDED","status":null,"stop_sequence":50,"track":null},"id":"prediction-ADDED-1524238424-70028-50","relationships":{"route":{"data":{"id":"Orange","type":"route"}},"stop":{"data":{"id":"70028","type":"stop"}},"trip":{"data":{"id":"ADDED-1524238424","type":"trip"}}},"type":"prediction"},{"attributes":{"arrival_time":"2018-04-25T13:52:00-04:00","departure_time":"2018-04-25T13:52:00-04:00","direction_id":1,"schedule_relationship":null,"status":null,"stop_sequence":140,"track":null},"id":"prediction-36315809-70029-140","relationships":{"route":{"data":{"id":"Orange","type":"route"}},"stop":{"data":{"id":"70029","type":"stop"}},"trip":{"data":{"id":"36315809","type":"trip"}}},"type":"prediction"},{"attributes":{"arrival_time":"2018-04-25T13:52:39-04:00","departure_time":"2018-04-25T13:52:39-04:00","direction_id":0,"schedule_relationship":null,"status":null,"stop_sequence":50,"track":null},"id":"prediction-36315757-70028-50","relationships":{"route":{"data":{"id":"Orange","type":"route"}},"stop":{"data":{"id":"70028","type":"stop"}},"trip":{"data":{"id":"36315757","type":"trip"}}},"type":"prediction"},{"attributes":{"arrival_time":"2018-04-25T14:00:20-04:00","departure_time":"2018-04-25T14:00:20-04:00","direction_id":0,"schedule_relationship":null,"status":null,"stop_sequence":50,"track":null},"id":"prediction-36315758-70028-50","relationships":{"route":{"data":{"id":"Orange","type":"route"}},"stop":{"data":{"id":"70028","type":"stop"}},"trip":{"data":{"id":"36315758","type":"trip"}}},"type":"prediction"},{"attributes":{"arrival_time":"2018-04-25T14:09:34-04:00","departure_time":"2018-04-25T14:09:34-04:00","direction_id":1,"schedule_relationship":null,"status":null,"stop_sequence":140,"track":null},"id":"prediction-36315810-70029-140","relationships":{"route":{"data":{"id":"
I am parsing the code like this
private void parseItems(List<GalleryItem> items, JSONObject jsonBody)
throws IOException, JSONException {
JSONObject photosJsonObject = jsonBody.getJSONObject("data");
JSONArray photoJsonArray = photosJsonObject.getJSONArray("attributes");
for (int i = 0; i < photoJsonArray.length(); i++) {
JSONObject photoJsonObject = photoJsonArray.getJSONObject(i);
GalleryItem item = new GalleryItem();
item.setId(photoJsonObject.getString("arrival_time"));
item.setCaption(photoJsonObject.getString("departure_time"));
item.setUrl(photoJsonObject.getString("departure_time"));
items.add(item);
}
}
could some one tell me where I am making a mistake when parsing the object.
thank you
Well, from your posted json data is an array and attributes is an object, you were trying to parse them the other way around. Try this:
JSONArray data = jsonBody.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
JSONObject singleData = data.getJSONObject(i);
JSONObject attributes = singleData.getJSONObject("attributes");
GalleryItem item = new GalleryItem();
item.setId(attributes.getString("arrival_time"));
item.setCaption(attributes.getString("departure_time"));
item.setUrl(attributes.getString("departure_time"));
items.add(item);
}
As data is a JSONArray, and your photosJsonObject should be JSONArray not object, which contains the objects. Rest is ok I think.
I have data on this string, but I'm having trouble to access the individual data.
This is my code:
Log.d("Detail Outputss", "" + response.toString());
And this is the string output:
{"futsal_id":"45","info":[{"futsal_id":"45","futsal_name":"Kathmandu Futsal","city":"Kathmandu","address":"Kathmandu","owner_name":"Hari Prasad","owner_address":"Kathmandu","email":"kathmandufutsal#gmail.com","password":"kathmandu","phone_no":"1111111","mobile_no":"9841112233","status":"1"}],"description":[{"futsal_id":"45","futsal_desc":"Futsal is the fever which never ends.\r\nKathmandu futsal is - a platform, a medium of communication for our fraternity. Not just somewhere to host challenges or seek venues, but a place where we can all share and spread the luv! Here, you can find just about anything, do just about anything, and see just about anything. Unbelievable? Believe it."}],"features":[{"futsal_id":"45","futsal_feat":"Free Wifi"},{"futsal_id":"45","futsal_feat":"High Quality Grass"},{"futsal_id":"45","futsal_feat":"Canteen Facility"},{"futsal_id":"45","futsal_feat":"Friendly Environment"}],"dimension":[{"futsal_id":"45","dimension":"40m X 20m"}],"no_of_futsal":[{"futsal_id":"45","number":"1"}],"opening_hrs":[{"futsal_id":"45","open_time_id":"1","open_time":"6am","close_time_id":"15","close_time":"9pm"}],"price_weekdays_price1":[{"futsal_id":"45","price_id":"1","start_time":"6am","end_time":"12pm","price":"1200"}],"price_weekdays_price2":[{"futsal_id":"45","price_id":"2","start_time":"12pm","end_time":"6pm","price":"1000"}],"price_weekdays_price3":[{"futsal_id":"45","price_id":"3","start_time":"6pm","end_time":"9pm","price":"1500"}],"price_weekend_price1":[{"futsal_id":"45","price_id":"1","start_time":"6pm","end_time":"12pm","price":"1500"}],"price_weekend_price2":[{"futsal_id":"45","price_id":"2","start_time":"12pm","end_time":"6pm","price":"1800"}],"price_weekend_price3":[{"futsal_id":"45","price_id":"3","start_time":"6pm","end_time":"9pm","price":"2000"}],"phone_number_address":[{"futsal_id":"45","futsal_name":"Kathmandu Futsal","city":"Kathmandu","address":"Kathmandu","owner_name":"Hari Prasad","owner_address":"Kathmandu","email":"kathmandufutsal#gmail.com","password":"kathmandu","phone_no":"1111111","mobile_no":"9841112233","status":"1"}],"news":[{"futsal_id":"45","news_id":"7","news_title":"asdfasdfasdfasdf","news_description":"asdfashdflahsdlfasdjf;oajsd;ofjaosdijfoaisdjofajsdfja;sjdf;ajsd;fja;ksdjf;kasjd;fkja;sdjf;asjd;fajsd;fjasdjf;asjd;fjas;djf;asdf"},{"futsal_id":"45","news_id":"6","news_title":"awefasdf","news_description":"sdjf;asdf;a;sdf;asjdf;oajsd;fja;sdjf;oajsd;oifjaosdf"},{"futsal_id":"45","news_id":"5","news_title":"asdfasdf","news_description":"sadfasdfasdfasdfa"}]}
I want to get the value of each object futsal_id, futsal_name, city and others, thanks in advance!!
It is JSONArray within a JSONObject
JSONObject json = new JSONObject(response);
JSONArray info = json.getJSONArray("info");
//getting the first value.. loop it if you have more than one
JSONObject infoObject = info.getJSONObject(0);
JSONArray description = json.getJSONArray("description");
JSONObject json = new JSONObject(response);
int fustal_id =Integer.parseInt(json.getString("futsal_id"));
JSONArray inf = json.getJSONArray("info");
JSONObject info = inf.getJSONObject(0);
...
JSONObject responseObject = new JSONObject(response);
JSONArray info = responseObject.getJSONArray("info");
for(int i = 0; i < info.length(); i++) {
JSONObject obj = info.getJSONObject(i);
String futsal_id = obj.getString("futsal_id");
String futsal_name = obj.getString("futsal_name");
//so on
}
Be sure to catch the exceptions.
In the below code, I can display the first returned JSON place name result from my Google Places URL. Rather than just the first item, I'd like to display the whole returned list of place names. What would be the easiest way of doing this? Can you point me to a tutorial, or give sample code?
HttpGet get = new HttpGet(bcURL.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
if(status == 200){
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
json = new JSONObject(data);
JSONArray timeline = json.getJSONArray("results");
JSONObject lastport = timeline.getJSONObject(0);
return lastport;
}else{
Toast.makeText(NewGPS3.this, "oops", Toast.LENGTH_SHORT);
return null;
}
The line JSONObject lastport = timeline.getJSONObject(0); shows the first name. I'd like to show a list rather than just the first item.
You are going to need to use a JSONArray object in order to get at the array. It should go something like this:
JSONObject entireObject = new JSONObject("your JSON string here");
JSONObject mainObject = entireObject.getJSONObject("photos"); // main object in entire object
JSONArray arrayObject = mainObject.getJSONArray("photo"); // array object in the main object
You can then iterate through this array just like any other:
for (int i = 0; i < arrayObject.length(); i++) {
JSONObject photoData = arrayObject.getJSONObject(i);
String yourString = photoData.getString("id");
}
I got some of this out of Pro Android Media. There is a really good JSON api call example in this book. You can probably get the source code for free, too.
http://www.amazon.com/Pro-Android-Media-Developing-Smartphones/dp/1430232676/ref=sr_1_1?ie=UTF8&qid=1343062690&sr=8-1&keywords=android+media
for(int i = 0; i < timeline.size(); i++){
JSONObject lastport = timeline.getJSONObject(i);
//Save the item to a ArrayList and use that list for your ListAdapter.
googlePlaces.add(lastPort);
}
I got the Json response. I am not able to get the values from the string.my string is
Json_response is
{"NameAllList":[{"Current":{"time":"2012-02-21T08:04:21","Name":"abcd"},
"Next":{"Name":"data1","StartTime":"2012-02-21T08:06:21"}},{"Current":{"time":"2012-02-21T08:14:21","Name":"defg"},
"Next":{"Name":"data2","StartTime":"2012-02-21T08:24:21"}},{"Current":{"time":"2012-02-21T08:28:21","Name":"ghij"},
"Next":{"Name":"data3","StartTime":"2012-02-21T08:34:21"}},{"Current":{"time":"2012-02-21T08:40:21","Name":"knmo"},
"Next":{"Name":"data4","StartTime":"2012-02-21T08:48:21"}}]}
and i tried this.
JSONObject jsonObj = new JSONObject(json_response);
JSONObject subObj = jsonObj.getJSONObject("Current");
String name_current =subObj.getString("Name");
but i am not able to get the value of "Name". what mistake i have done. provide the link to do the above parsing.
first of all, your JSON response is having NameAllList as a JSON Array of objects.
So you have to fetch JSON Array first, then you can fetch one-by-one object.
for example:
JSONObject jsonString = (new JSONObject(json_response_string));
JSONArray array = jsonString.getJSONArray("NameAllList");
for(int i=0; i<array.length(); i++)
{
// Retrieve Current object as such
JSONObject objCurrent = array.getJSONObject("Current");
// Retrieve Next object as such
JSONObject objNext = array.getJSONObject("Next");
}
You are not parsing json properly, so you are not able to fetch value of Name. Please note JSON Annotation [] represent JSONArray, and {} respresent JSONObject, so method to get current item's name is:
JSONObject jsonObj = new JSONObject(json_response_string);
JSONArray jsonArr=jsonObj.getJSONArray("NameAllList");
String Hora_name_current="";
for(int i=0;i<jsonArr.length();i++)
{
JSONObject obj=jsonArr.get(i);
try{
JSONObject subObj = obj.getJSONObject("Current");
Hora_name_current =subObj.getString("Name");
break;
}catch(JSONException ex)
{
}
}
looks like you're trying to use JSONObject when you should be using JSONArray for the second request. Try this:
JSONObject jsonString = (new JSONObject(json_response_string));
JSONArray array = jsonString.getJSONArray("NameAllList");
In your JSON return, "NameAllList is actually an array and needs to be handled as such. Once you set it to "array", you can then run a for loop and treat it like any other array in Java.
Let me know if that helps.
David
JSONObject jsonObj = new JSONObject(json_response_string);
JSONArray jsonArray = jsonObj.getJSONArrays("NameAllList");
[{"placeID":"p0001","placeName":"INTI International University","placeType":"Education","placeLat":"2.813997","placeLng":"101.758229","placePict":""},{"placeID":"p0002","placeName":"Nilai International College","placeType":"Education","placeLat":"2.814179","placeLng":"101.7700107","placePict":""}]
How do I decode the JSON sent from my PHP script on Android?
please try this
String s = "[{\"placeID\":\"p0001\",\"placeName\":\"INTI International University\",\"placeType\":\"Education\","
+ "\"placeLat\":\"2.813997\",\"placeLng\":\"101.758229\",\"placePict\":\"\"},"
+ "{\"placeID\":\"p0002\",\"placeName\":\"Nilai International College\",\"placeType\":\"Education\",\"placeLat\":\"2.814179\",\"placeLng\":\"101.7700107\",\"placePict\":\"\"}]";
ArrayList<String> arrplaceID = new ArrayList<String>();
ArrayList<String> arrplaceName = new ArrayList<String>();
try {
JSONArray arr = new JSONArray(s);
for (int i = 0; i < arr.length(); i++) {
JSONObject jsonObject = arr.getJSONObject(i);
arrplaceID.add(jsonObject.optString("placeID"));
arrplaceName.add(jsonObject.optString("placeName"));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < arrplaceID.size(); i++) {
Log.e("arr[" + i + "] place Name", arrplaceName.get(i));
}
What is the problem in this Please Read this tutorial for parsing JSON it might be helpful in future also.json parsing link
Follow below points.
1) it seems the response you are getting is Json Array. so create one json array by response string.
JSonArray jArray = new JsonArray(responseString);
2) now you have your response in jArray. now iterate a loop and take json object from JsonArray, in your case you have two json objects.
for(i,i<jArray.size,i++)
{
JsonObject obj=jArray.get(i);
// here you got your first entry in jsonObject.
// nor use this obj according to ur need. you can say obj.getString("placeID");
// and so on.
}
refer this to understand more on json link
use JSONArray class:
JSONArray jsonplaces = new JSONObject(stringPlaces);
then your able to iterate throught array by using for-loop:
for (int i = 0; i < jsonplaces.length(); i++) {
JSONObject jsonplace = (JSONObject) jsonplaces.get(i);
//read items, for example:
String placeName = jsonplace.getString("placeName");
}