Possible Duplicate:
How do I parse JSON from a Java HTTPResponse?
How to parse json string in Android?
How can i parse this json string
{
"apiVersion": "2.1",
"data": {
"id": "pHuoDqcIyqk",
"uploaded": "2012-10-29T16:08:15.000Z",
"updated": "2012-11-02T08:48:28.000Z",
"uploader": "googlenexus",
"category": "Tech",
"title": "Nexus: Ask Me Anything",
"description": "The Best of Google, now in 3 sizes. Introducing Nexus 4, Nexus 7 and Nexus 10. The new smartphone and tablets from Google. Shop now at play.google.com/nexus",
"thumbnail": {
"sqDefault": "http://i.ytimg.com/vi/pHuoDqcIyqk/default.jpg",
"hqDefault": "http://i.ytimg.com/vi/pHuoDqcIyqk/hqdefault.jpg"
},
"player": {
"default": "http://www.youtube.com/watch?v=pHuoDqcIyqk&feature=youtube_gdata_player",
"mobile": "http://m.youtube.com/details?v=pHuoDqcIyqk"
},
"content": {
"5": "http://www.youtube.com/v/pHuoDqcIyqk?version=3&f=videos&app=youtube_gdata",
"1": "rtsp://v8.cache5.c.youtube.com/CiILENy73wIaGQmpyginDqh7pBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
"6": "rtsp://v7.cache4.c.youtube.com/CiILENy73wIaGQmpyginDqh7pBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"
},
"duration": 61,
"aspectRatio": "widescreen",
"rating": 4.8985643,
"likeCount": "5227",
"ratingCount": 5363,
"viewCount": 1038854,
"favoriteCount": 0,
"commentCount": 1442,
"accessControl": {
"comment": "allowed",
"commentVote": "allowed",
"videoRespond": "moderated",
"rate": "allowed",
"embed": "allowed",
"list": "allowed",
"autoPlay": "allowed",
"syndicate": "allowed"
}
}
}
Can anyone please tell me how to make a call to the above URL & get a JSON object, then parse it to get the desired info as a string?
-
i dont have any idea of making webcalls , so waiting for solution.Pls helpvaibvorld– vaibvorld2012年11月02日 09:18:55 +00:00Commented Nov 2, 2012 at 9:18
-
there are many links for json parsing ........try yourself first...one of the link is androidhive.info/2012/01/android-json-parsing-tutorialmaninder singh– maninder singh2012年11月02日 09:19:15 +00:00Commented Nov 2, 2012 at 9:19
-
learn how to parse json string from this site androidhive.info/2012/01/android-json-parsing-tutorialRam kiran Pachigolla– Ram kiran Pachigolla2012年11月02日 09:21:50 +00:00Commented Nov 2, 2012 at 9:21
-
I did homework for you: Android - JSON ParsingParesh Mayani– Paresh Mayani2012年11月02日 09:23:28 +00:00Commented Nov 2, 2012 at 9:23
-
please tell me way first to get desired json object , parsing is secondary thing but how to make call first to get json object?vaibvorld– vaibvorld2012年11月02日 09:23:34 +00:00Commented Nov 2, 2012 at 9:23
5 Answers 5
Well too broad a question you are asking..
1) Do a http get request on the URL. (Or using this method) and get the response String, which is JSON. Some examples here
2) See this answer to see how to parse JSON.
Comments
simple simple....
String str = "" // your JSON string
JSONObject json = new JSONObject(str);
String apiVersion = json.getString("apiVersion");
// continue as before for the rest of it...
Comments
you Can try below method. you can pass URL and get the Response as result String. public static String result;
public static boolean connect(String url) {
boolean flag = false;
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httppost = new HttpGet(url);
Log.e("url", url);
// Execute the request
HttpResponse response;
try {
// https://api.vkontakte.ru/method/audio.search?uid=163398985&q=akoncount=100&access_token=2a4db0e223f0f5ab23f0f5ab5f23da5680223f023f1f5a3c696b018be9b17b9
response = httpclient.execute(httppost);
// Examine the response status
Log.i("vkontake", response.getStatusLine().toString() + "\n"
+ response);
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
Log.d("Jsomn Activity", "---- is --- " + instream);
result = convertStreamToString(instream);
Log.d("Jsomn Activity", "---- Result --- " + result);
// now you have the string representation of the HTML request
instream.close();
} else {
Log.d("Jsomn Activity", "---- is --- null ");
}
flag = true;
net = false;
} catch (Exception e) {
Log.d("Jsomn Activity", "---- Catch --- " + e.toString());
flag = false;
e.printStackTrace();
} finally {
return flag;
}
}
Comments
There are a couple of ways to do this. One easy way is to define an object structure that matches your data and use http://code.google.com/p/google-gson/ to fill it:
final URL url = new URL("http://yourURL.com");
final InputStream openStream = url.openStream();
final InputStreamReader inputStreamReader = new InputStreamReader(openStream);
final SearchResult searchResult = new Gson().fromJson(inputStreamReader, SearchResult.class);
If your searchResult class containts a field e.g. apiVersion, it will be filled with the data from JSON. Then you have a data field with the other fields inside etc. This is a very easy way to populate your data, but you will have to mirror the structure of the JSON stream in your object model.
Comments
The JSON.org site contains no less than 20 different java implementations of JSON parsers.