在android中解析没有title对象的json

时间:2013-01-26 13:12:41

标签: android xml json api rest

我有这个json:{"id":1,"name":"john smith"}

我怎么解析它?我知道我必须用这个功能做到这一点:

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

但我不知道我能用“ * *** ”加入的区域。请帮帮我

我只想获取idname值。

2 个答案:

答案 0 :(得分:1)

将当前json String解析为:

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

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

因为当前的json字符串只包含一个jsonObject

答案 1 :(得分:0)

使用此方法解析您的JSON字符串

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

有关详情,请参阅此Link