0

i have this json : {"id":1,"name":"john smith"}

How i can parse it? i know i have to do this with this function:

public static String parseJSONResponse(String jsonResponse) {
    String name = "";
    JSONObject json;
    try {
        json = new JSONObject(jsonResponse);
        JSONObject result = json.getJSONObject("**********");
        name = result.getString("**********");

    } catch (JSONException e) {

        e.printStackTrace();
    }

    return name;
}

but i dont know what can i put in the areas incated with "****". please help me

i only want to fetch id and name values.

0

2 Answers 2

1

parse current json String as:

json = new JSONObject(jsonResponse);
// get name here
name = json.getString("name");

// get id here
id = json.getString("id");

because current json string contain only one jsonObject

Sign up to request clarification or add additional context in comments.

2 Comments

Correct ! What you put between these quotes is just the key to get a value from your JSON String
It was suggested from an edit that json.getInt("id") should be used to get the id.
0

use this method to parse your JSON String

public static String parseJSONResponse(String jsonResponse) {

    try {

         JSONObject  json = new JSONObject(jsonResponse);

           // get name & id here
         String  name = json.getString("name");
         int id =  json.getInt("id"); 

    } catch (JSONException e) {

        e.printStackTrace();
    }

    return name;
}

For more Refer this Link

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.