0

My web service outputs a JSON string that I need to parse.

Could you please tell me how i can parse the given JSON string:

{
"access_token":"kfwsfdcscfnsdcfsdsdfsd6f7df9sd6f89sd6s",
 "expires_in":3600,
 "token_type":"bearer",
 "scope":null,
 "refresh_token":"5dfddf1d6dfd1fddgdgdg1dg56d1"
 }
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
Jeekiran
  • 473
  • 3
  • 10

4 Answers4

2
  JSONObject mainObject = new JSONObject(Your_Sring_data);
  JSONObject uniObject = mainObject.getJSONObject("university");
  String  uniName = uniObject.getJSONObject("name");
  String uniURL = uniObject.getJSONObject("url");

  JSONObject oneObject = mainObject.getJSONObject("1");
  String id = oneObject.getJSONObject("id");

refer this link:

http://stackoverflow.com/questions/8091051/how-to-parse-json-string-in-android
KarthikManoharan
  • 805
  • 5
  • 12
2
String jsonString = ""; //This'll contain the jsonString you mentioned above. 

JSONObject object = new JSONObject(jsonString);
String accessToken = object.getString("access_token");

Similarly fetch other values.

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
1
JSONObject jsonObject = new JSONObject(jsonString);
runor49
  • 3,870
  • 1
  • 21
  • 18
1
{  // represents json object node
"access_token":"kfwsfdcscfnsdcfsdsdfsd6f7df9sd6f89sd6s", // key value pair

To parse

 JSONObject jb = new JSONObject("jsonstring");
 String acess_token = jb.getString("acess_token"); // acess_token is the key
 // similarly others

You can also use Gson to convert json string to java object.

http://code.google.com/p/google-gson/

Using Gson

Download the jar add to the projects libs folder

Then

public class Response {

    String acess_token,expires_in,token_typerefresh_token;
}

Then

InputStreamReader isr = new InputStreamReader (is);
Gson gson = new Gson();         
Response lis = new Gson().fromJson(isr, Response.class);
Log.i("Acess Toekn is ",""+lis.expires_in);
Raghunandan
  • 132,755
  • 26
  • 225
  • 256