What i am doing::I have a Json Structure I want to extract Bangalore having key adminArea5
{
"results": [
{
"locations": [
{
"latLng": {
"lng": 77.620949,
"lat": 12.931088
},
"adminArea4": "Bangalore Urban",
"adminArea5Type": "City",
"adminArea4Type": "County",
"adminArea5": "Bangalore",
"street": "Sarjapur Road",
"adminArea1": "IN",
"adminArea3": "Karnataka",
"type": "s",
"displayLatLng": {
"lng": 77.617502,
"lat": 12.928883
},
"linkId": 0,
"postalCode": "560095",
"sideOfStreet": "N",
"dragPoint": false,
"adminArea1Type": "Country",
"geocodeQuality": "ADDRESS",
"geocodeQualityCode": "L1AAA",
"mapUrl": "http://open.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luur2d0821,8s=o5-9arwha&type=map&size=225,160&pois=purple-1,12.931088,77.620949,0,0|¢er=12.931088,77.620949&zoom=15&rand=981886032",
"adminArea3Type": "State"
}
],
"providedLocation": {
"latLng": {
"lng": 77.620949,
"lat": 12.931088
}
}
}
],
"options": {
"ignoreLatLngInput": false,
"maxResults": -1,
"thumbMaps": true
},
"info": {
"copyright": {
"text": "© 2014 MapQuest, Inc.",
"imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
"imageAltText": "© 2014 MapQuest, Inc."
},
"statuscode": 0,
"messages": []
}
}
What i have done so far::
HttpGet httpget = new HttpGet(Const.URL_JSON_OBJECT);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
Content = Client.execute(httpget, responseHandler);
jsonobject = new JSONObject(Content);
Log.d("Content", Content.toString());
myString=jsonobject.getString("results").toString();
With the code above :: I have the JSON Structure
[
{
"providedLocation": {
"latLng": {
"lat": 12.931088,
"lng": 77.620949
}
},
"locations": [
{
"latLng": {
"lat": 12.931088,
"lng": 77.620949
},
"adminArea5Type": "City",
"adminArea4": "Bangalore Urban",
"adminArea5": "Bangalore",
"adminArea4Type": "County",
"street": "Sarjapur Road",
"adminArea1": "IN",
"adminArea3": "Karnataka",
"type": "s",
"displayLatLng": {
"lat": 12.928883,
"lng": 77.617502
},
"linkId": 0,
"postalCode": "560095",
"dragPoint": false,
"sideOfStreet": "N",
"adminArea1Type": "Country",
"geocodeQuality": "ADDRESS",
"geocodeQualityCode": "L1AAA",
"adminArea3Type": "State",
"mapUrl": "http://open.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luur2d0821,8s=o5-9arwha&type=map&size=225,160&pois=purple-1,12.931088,77.620949,0,0|¢er=12.931088,77.620949&zoom=15&rand=-1939275403"
}
]
}
]
Question:: How to get Bangalore having key adminArea5
It looks like that you've an JSONArray() within your JSONObject.
Try to get the JSONOBject result, pick out the
JSONArray jsonArray = new JSONArray(jsonObject.get("results")).
In your JSONArray you can parse out your required data.
Related
I'm trying to read date from JSON using a URL , but it throws an exception. and the response from header is 301 Moved Permanently
this basically means the there is something wrong with link, but I cannot figure out what it is exactly since the API provider did not mention anything in regard to changes to their API
Any help is appreciated.
here is my code
private static String readUrl(String urlString) throws Exception {
BufferedReader reader = null;
try {
URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1)
buffer.append(chars, 0, read);
return buffer.toString();
} finally {
if (reader != null)
reader.close();
}
}
String url = "http://webservice.fanart.tv/v3/movies/" + movie.id + "?api_key=" + apikey;
List<String> enClearLogos = new ArrayList<>();
try {
JSONObject json = new JSONObject(readUrl(url));
JSONArray jsonArray = json.getJSONArray("hdmovielogo");
for(int i = 0; i < jsonArray.length(); i++){
JSONObject movieObject = jsonArray.getJSONObject(i);
if (movieObject.getString("lang").equalsIgnoreCase("en"))
enClearLogos.add(movieObject.getString("url"));
}
} catch (Exception e) {
e.printStackTrace();
}
here example
{
"name": "The Bad Guys",
"tmdb_id": "629542",
"imdb_id": "tt8115900",
"hdmovieclearart": [
{
"id": "362695",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovieclearart/the-bad-guys-6231fd4379a87.png",
"lang": "en",
"likes": "6"
},
{
"id": "362696",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovieclearart/the-bad-guys-6231fd53d3a96.png",
"lang": "ru",
"likes": "3"
},
{
"id": "367992",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovieclearart/the-bad-guys-6280fb431126b.png",
"lang": "cz",
"likes": "2"
}
],
"hdmovielogo": [
{
"id": "369396",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-629dc8e4da93a.png",
"lang": "fr",
"likes": "3"
},
{
"id": "368375",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-62899cfba2c00.png",
"lang": "en",
"likes": "3"
},
{
"id": "371110",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-62bdec1d38353.png",
"lang": "es",
"likes": "2"
},
{
"id": "355025",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-61ba168a41d9b.png",
"lang": "ru",
"likes": "2"
},
{
"id": "355031",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-61ba1f851cd32.png",
"lang": "en",
"likes": "2"
},
{
"id": "355032",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-61ba1f96a497e.png",
"lang": "en",
"likes": "1"
},
{
"id": "366828",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-626984d807f4d.png",
"lang": "cz",
"likes": "1"
},
{
"id": "360922",
"url": "https://assets.fanart.tv/fanart/movies/629542/hdmovielogo/the-bad-guys-6217a7c5ba60d.png",
"lang": "it",
"likes": "0"
}
],
"movieposter": [
{
"id": "367357",
"url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-6274ea20dcc86.jpg",
"lang": "en",
"likes": "2"
},
{
"id": "371114",
"url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-62bdecfbd6715.jpg",
"lang": "es",
"likes": "2"
},
{
"id": "368000",
"url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-628108a944560.jpg",
"lang": "pt",
"likes": "2"
},
{
"id": "367358",
"url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-6274ea33e2ed5.jpg",
"lang": "00",
"likes": "2"
},
{
"id": "355026",
"url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-61ba169a2b57e.jpg",
"lang": "en",
"likes": "2"
},
{
"id": "358210",
"url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-61ee6f9a14706.jpg",
"lang": "00",
"likes": "1"
},
{
"id": "367991",
"url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-6280fb200c5bf.jpg",
"lang": "cz",
"likes": "0"
},
{
"id": "367990",
"url": "https://assets.fanart.tv/fanart/movies/629542/movieposter/the-bad-guys-6280fb1ba6d39.jpg",
"lang": "cz",
"likes": "0"
}
],
"moviethumb": [
{
"id": "371117",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviethumb/the-bad-guys-62bdee5332ad2.jpg",
"lang": "es",
"likes": "2"
},
{
"id": "367994",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviethumb/the-bad-guys-6280fb7b224fd.jpg",
"lang": "cz",
"likes": "1"
},
{
"id": "370972",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviethumb/the-bad-guys-62bbbc9385121.jpg",
"lang": "en",
"likes": "0"
},
{
"id": "367260",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviethumb/the-bad-guys-6272bfb851ca5.jpg",
"lang": "en",
"likes": "0"
}
],
"moviebackground": [
{
"id": "355027",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviebackground/the-bad-guys-61ba16ab7df92.jpg",
"lang": "",
"likes": "2"
},
{
"id": "367177",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviebackground/the-bad-guys-6271f6e99b835.jpg",
"lang": "",
"likes": "0"
}
],
"moviedisc": [
{
"id": "367993",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviedisc/the-bad-guys-6280fb602b271.png",
"lang": "cz",
"likes": "1",
"disc": "1",
"disc_type": "bluray"
},
{
"id": "358213",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviedisc/the-bad-guys-61ee85436574f.png",
"lang": "en",
"likes": "0",
"disc": "1",
"disc_type": "bluray"
}
],
"moviebanner": [
{
"id": "358216",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviebanner/the-bad-guys-61ee93348cb62.jpg",
"lang": "en",
"likes": "1"
},
{
"id": "370973",
"url": "https://assets.fanart.tv/fanart/movies/629542/moviebanner/the-bad-guys-62bbbd7215d83.jpg",
"lang": "en",
"likes": "0"
}
]
}
exception message
W/System.err: org.json.JSONException: Value <html> of type java.lang.String cannot be converted to JSONObject
W/System.err: at org.json.JSON.typeMismatch(JSON.java:112)
W/System.err: at org.json.JSONObject.<init>(JSONObject.java:168)
W/System.err: at org.json.JSONObject.<init>(JSONObject.java:181)
The http:// causes url redirection and throws this message in the header response (301 Moved Permanently) so I had to use https:// instead now everything works nicely.
more info here Response 301 Moved Permanently
{
"status": "1",
"message": "",
"result": {
"info": {
"tax": "0",
"discount": "0",
"minimum_spend": "300",
"delivery_charges": "0",
"last_updated": "1 week 15 hours ago"
},
"items": [
{
"name": "Eat At Home",
"menu_item_id": "12345",
"menu_cat_id": "4321",
"menu_cat_sku": "",
"nutritions": "",
"price": "1000",
"currency": "USD",
"desc": "Desription",
"category": "Promotion",
"image": "https://static.google.com/media/images/thumbs/343e8f41b18325a6058adc3773ed4d53.png",
"large_image": "https://static.google.com/media/images/343e8f41b18325a6058adc3773ed4d53.png",
"options": [],
"discount": "",
"weight": "",
"sku": "",
"status": "0",
"brand": []
},
{
"name": "Lunch Bundle",
"menu_item_id": "4321",
"menu_cat_id": "4321",
"menu_cat_sku": "",
"nutritions": "",
"price": "1500",
"currency": "USD",
"desc": "Description",
"category": "Promotion",
"image": "https://static.google.com/media/images/thumbs/62cdde279bbc3e45b8456f040d649b32.png",
"large_image": "https://static.google.com/media/images/62cdde279bbc3e45b8456f040d649b32.png",
"options": [],
"discount": "",
"weight": "",
"sku": "",
"status": "0",
"brand": []
},
My code
MenuResponse menuResponse = JsonParser.getInstance().parseMenuResponse(response);
public MenuResponse parseMenuResponse(String serverResponse) throws Exception {
MenuResponse response = null;
if (serverResponse != null) {
try {
response = gson.fromJson(serverResponse, MenuResponse.class);
} catch (JsonSyntaxException jse) {
throw new Exception(ERROR_MESSAGE);
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
try this.
JSONObject json = new JSONObject(response);
String status= json.getString("status");
JSONArray itemsArray= json.getJSONArray("items");
for (int i = 0; i < itemsArray.length(); i++) {
JSONObject c = itemsArray.getJSONObject(i);
String name= c.getString("name");
//remaining data you will
}
Or else better you can use Retrofit.
I'm trying to insert an object to a nested array through the java api, but I get the following error
MapperParsingException[object mapping for [X] tried to parse field [null] as object, but found a concrete value]
doing it through Kibana as shown below the same script works.
Any ideas on how to fix this?
The java code is the following
HashMap<String, Object> params = new HashMap<>();
params.put("object", objectAsString);
Script script = new Script(ScriptType.INLINE, "painless", "ctx._source.media.add(params.object)", params);
UpdateResponse result = elasticClient.prepareUpdate(indexName, "Type", documentId).setScript(script).execute().actionGet();
Trhough Kibana
POST index/document/id/_update
{
"script": {
"lang": "painless",
"inline": "ctx._source.media.add(params.object)",
"params": {
"object": {
"description" : "A second image",
"height" : 5,
"weight": 5,
"name" : "Test",
"orientation" : "Vertical",
"url" : "htttp://newurl.jpg",
"tags":["first","second"],
"type":"image"
}
}
}
}
The template mapping is the following:
"mappings": {
"FOLDER_MODULE": {
"properties": {
"name": {
"type": "keyword"
},
"publisherId": {
"type": "keyword"
},
"username": {
"type": "keyword"
},
"media": {
"type": "nested",
"properties": {
"id": {
"type": "text"
},
"name": {
"type": "text"
},
"publisherId": {
"type": "short"
},
"tags": {
"type": "text"
},
"description": {
"type": "text"
},
"url": {
"type": "keyword"
},
"createdDate": {
"format": "strict_date_optional_time||epoch_millis",
"type": "date"
},
"height": {
"type": "float"
},
"width": {
"type": "float"
},
"weight": {
"type": "float"
},
"orientation": {
"type": "keyword"
},
"status": {
"type": "keyword"
},
"type": {
"type": "keyword"
},
"username": {
"type": "keyword"
}
}
}
}
}
}
I'm trying to retrieve the "Maxspeed" string from the JSON below.
It iterates through the array, but when it reaches the "tag" object and tries to retrieve the "maxspeed" value, it returns an exception(below).
Would anyone know why this is happening?
Any help would be appreciated.
Thanks.
Exception:
org.json.JSONException: No value for maxspeed
Java:
JSONObject parentObject = new JSONObject(result);
JSONArray speedJSON = parentObject.getJSONArray("elements");
for (int i = 0; i < speedJSON.length(); i++) {
JSONObject element = (JSONObject) speedJSON.get(i);
if (!element.isNull("tags")) {
//JSONObject tags = (JSONObject) speedJSON.getJSONObject(i).get("tags");
String maxspeed = element.getString("maxspeed");
txtSpeed.setText(maxspeed+" here");
} else {
//Your error handling here...
}
}
//txtSpeed.setText(""+);
this.progressDialog.dismiss();
JSON:
{
"version": 0.6,
"generator": "Overpass API",
"osm3s": {
"timestamp_osm_base": "2015-03-16T20:10:02Z",
"copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL."
},
"elements": [
{
"type": "node",
"id": 768053039,
"lat": 54.9526671,
"lon": -7.7273348
},
{
"type": "node",
"id": 768053040,
"lat": 54.9498094,
"lon": -7.7176056
},
{
"type": "node",
"id": 768053041,
"lat": 54.9497066,
"lon": -7.7173174
},
{
"type": "node",
"id": 768053043,
"lat": 54.9495658,
"lon": -7.7170937
},
{
"type": "node",
"id": 768053044,
"lat": 54.9495035,
"lon": -7.7169816
},
{
"type": "node",
"id": 791492493,
"lat": 54.9494183,
"lon": -7.7168205
},
{
"type": "node",
"id": 795319854,
"lat": 54.9510427,
"lon": -7.7218262
},
{
"type": "node",
"id": 795320324,
"lat": 54.9509153,
"lon": -7.7213706
},
{
"type": "node",
"id": 1922546572,
"lat": 54.9502165,
"lon": -7.7190169
},
{
"type": "node",
"id": 1922546679,
"lat": 54.9504739,
"lon": -7.7199078
},
{
"type": "node",
"id": 1922546692,
"lat": 54.9500860,
"lon": -7.7185174
},
{
"type": "node",
"id": 1922602861,
"lat": 54.9517250,
"lon": -7.7241644
},
{
"type": "node",
"id": 1922622063,
"lat": 54.9514357,
"lon": -7.7231690
},
{
"type": "node",
"id": 2673934802,
"lat": 54.9498543,
"lon": -7.7177617
},
{
"type": "way",
"id": 64273241,
"nodes": [
768053039,
1922602861,
1922622063,
795319854,
795320324
],
"tags": {
"highway": "secondary",
"maxspeed": "60",
"name": "Port Road",
"oneway": "no",
"ref": "R229"
}
},
{
"type": "way",
"id": 64887990,
"nodes": [
795320324,
1922546679,
1922546572,
1922546692,
2673934802,
768053040,
768053041,
768053043,
768053044,
791492493
],
"tags": {
"highway": "secondary",
"maxspeed": "60",
"name": "Port Road",
"oneway": "no",
"ref": "R229"
}
}
]
}
You are trying to get maxspeed from the element, but it's not there. It is inside tags object in the element.
You need something like this
if (!element.isNull("tags")) {
JSONObject tags = element.getJSONObject("tags");
String maxspeed = tags.getString("maxspeed");
txtSpeed.setText(maxspeed+" here");
} else {
//Your error handling here...
}
String maxspeed = element.getString("maxspeed");
to
String maxspeed = element.getJSONObject("tags").getString("maxspeed");
Here I have a response from the facebook server that returns the list of albums of a selected user. I want to create an array for all the album names labeled "name" as well as the "link" and "cover_photo" to be ready to processed into a listview
{
"data": [
{
"id": "664462156031",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "Mobile Uploads",
"link": "https://www.facebook.com/album.php?fbid=664462156031&id=68004222&aid=2081375",
"cover_photo": "754991180141",
"count": 88,
"type": "mobile",
"created_time": "2012-05-12T00:38:14+0000",
"updated_time": "2013-11-04T20:45:08+0000",
"can_upload": false
},
{
"id": "813288706431",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "Bitstrips Photos",
"link": "https://www.facebook.com/album.php?fbid=813288706431&id=68004222&aid=1073741826",
"cover_photo": "813288781281",
"count": 1,
"type": "app",
"created_time": "2013-10-31T21:53:11+0000",
"updated_time": "2013-10-31T21:53:15+0000",
"can_upload": false
},
{
"id": "757384214481",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "Galveston Vacation 2013",
"description": "Our trip before joining Air Force",
"link": "https://www.facebook.com/album.php?fbid=757384214481&id=68004222&aid=1073741825",
"cover_photo": "757221350861",
"count": 8,
"type": "normal",
"created_time": "2013-05-15T18:37:19+0000",
"updated_time": "2013-05-15T22:12:52+0000",
"can_upload": false,
"likes": {
"data": [
{
"id": "100002572634186",
"name": "Misty O'Quain"
},
{
"id": "100000582072776",
"name": "Clifford Joyce"
},
{
"id": "1045514613",
"name": "Caity Ellender"
}
],
"paging": {
"cursors": {
"after": "MTA0NTUxNDYxMw==",
"before": "MTAwMDAyNTcyNjM0MTg2"
}
}
}
},
{
"id": "542202136091",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "Profile Pictures",
"link": "https://www.facebook.com/album.php?fbid=542202136091&id=68004222&aid=2054735",
"cover_photo": "749743202131",
"count": 20,
"type": "profile",
"created_time": "2010-09-02T04:43:32+0000",
"updated_time": "2013-04-13T13:46:14+0000",
"can_upload": false
},
{
"id": "646032913381",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "Cover Photos",
"link": "https://www.facebook.com/album.php?fbid=646032913381&id=68004222&aid=2079483",
"cover_photo": "681058471881",
"count": 2,
"type": "cover",
"created_time": "2012-03-22T00:25:50+0000",
"updated_time": "2012-07-17T15:26:24+0000",
"can_upload": false
},
{
"id": "599115930391",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "Timeline Photos",
"link": "https://www.facebook.com/album.php?fbid=599115930391&id=68004222&aid=2073818",
"cover_photo": "599115935381",
"count": 1,
"type": "wall",
"created_time": "2011-10-20T14:25:23+0000",
"updated_time": "2011-10-20T14:25:23+0000",
"can_upload": false
},
{
"id": "551798524851",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "My Year - 2010",
"link": "https://www.facebook.com/album.php?fbid=551798524851&id=68004222&aid=2060899",
"cover_photo": "551798544811",
"count": 1,
"type": "normal",
"created_time": "2010-12-19T05:11:51+0000",
"updated_time": "2010-12-19T05:12:14+0000",
"can_upload": false
},
{
"id": "522841060841",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "Night Lightnin",
"description": "Always wanted to do this, because lightning is way cooler at night. Perhaps next time I'll be able to refine my methods a little. Read as \"bring tripod so you don't have to prop cam up on roof of car\"",
"location": "Just north of Sulphur",
"link": "https://www.facebook.com/album.php?fbid=522841060841&id=68004222&aid=2036447",
"cover_photo": "522841195571",
"count": 15,
"type": "normal",
"created_time": "2009-09-16T04:38:43+0000",
"updated_time": "2009-09-16T04:42:32+0000",
"can_upload": false
},
{
"id": "513270939441",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "The Stuff That Doesn't Make Another Album",
"description": "See title.",
"location": "Good question",
"link": "https://www.facebook.com/album.php?fbid=513270939441&id=68004222&aid=2029083",
"cover_photo": "513271029261",
"count": 9,
"type": "normal",
"created_time": "2009-03-03T06:43:09+0000",
"updated_time": "2009-04-01T02:20:22+0000",
"can_upload": false,
"comments": {
"data": [
{
"id": "513270939441_3444",
"from": {
"name": "Misty Bylsma Royal",
"id": "592607110"
},
"message": "I love how the beautiful lady pile is in the same album as the bull pics. Nice.",
"can_remove": false,
"created_time": "2009-03-03T22:37:18+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "513270939441_3451",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"message": "Only because they are both things that i only had a few pictures of.",
"can_remove": false,
"created_time": "2009-03-04T02:46:11+0000",
"like_count": 0,
"user_likes": false
}
],
"paging": {
"cursors": {
"after": "Mg==",
"before": "MQ=="
}
}
}
},
{
"id": "511605661671",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "What the...?",
"description": "What's wrong with the rain? It's flaky and piles up on stuff... Did the plants leak something?\n",
"location": "Outside",
"link": "https://www.facebook.com/album.php?fbid=511605661671&id=68004222&aid=2026641",
"cover_photo": "511605681631",
"count": 54,
"type": "normal",
"created_time": "2008-12-11T11:59:27+0000",
"updated_time": "2008-12-13T18:06:00+0000",
"can_upload": false
},
{
"id": "511565886381",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "The Christmas Tree Farm",
"description": "An Ellender Tradition",
"location": "Grant",
"link": "https://www.facebook.com/album.php?fbid=511565886381&id=68004222&aid=2026590",
"cover_photo": "511566170811",
"count": 19,
"type": "normal",
"created_time": "2008-12-09T02:38:54+0000",
"updated_time": "2008-12-09T02:55:21+0000",
"can_upload": false
}
],
"paging": {
"cursors": {
"after": "NTExNTY1ODg2Mzgx",
"before": "NjY0NDYyMTU2MDMx"
}
}
}
Also, here is where the json data is returned (response). I will need to start parsing the data under response.
Session.NewPermissionsRequest np = new Session.NewPermissionsRequest(this, "friends_photos");
Session.getActiveSession().requestNewReadPermissions(np);
Request rq = new Request(Session.getActiveSession(), userID + "/albums", null, HttpMethod.GET, new Request.Callback() {
#Override
public void onCompleted(Response response) {
}
});
rq.executeAsync();
Any example would be helpful. I would like to study the code to get a better understanding on parsing json data.
You can get a good example of how to do here: http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
Basically you have to use JSONObject and JSONArray, that you can get from getJSONObject() and getJSONArray().
Once you're on the good spot, use getJSONString() to get the desired value.
In your case it will be something like this:
JSONObject json = new JSONObject(response)
JSONArray jarray = json.getJSONArray("data");
for(int i = 0; i < jarray.length(); i++){
JSONObject oneAlbum = jarray.getJSONObject(i);
//get your values
oneAlbum.getJSONString("name"); // this will return you the album's name.
}
Hope this will help you.
Try this
Session.NewPermissionsRequest np = new Session.NewPermissionsRequest(this, "friends_photos");
Session.getActiveSession().requestNewReadPermissions(np);
Request rq = new Request(Session.getActiveSession(), userID + "/albums", null, HttpMethod.GET, new Request.Callback() {
#Override
public void onCompleted(Response response) {
JSONArray albumArr = response.getGraphObject().getInnerJSONObject().getJSONArray("data");
for (int i = 0; i < albumArr.length(); i++) {
JSONObject item = albumArr.getJSONObject(i);
System.out.println("id : " + item.getString("id"));
System.out.println("fromName : " + item.getJSONObject("from").getString("name"));
System.out.println("fromid : " + item.getJSONObject("from").getString("id"));
System.out.println("link : " + item.getString("link"));
System.out.println("cover_photo : " + item.getString("cover_photo"));
System.out.println("count : " + item.getString("count"));
System.out.println("created_time : " + item.getString("created_time"));
System.out.println("updated_time : " + item.getString("updated_time"));
System.out.println("can_upload : " + item.getString("can_upload"));
}
}
});
rq.executeAsync();