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"
 }

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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);

Comments

1
JSONObject jsonObject = new JSONObject(jsonString);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.