我有这个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;
}
但我不知道我能用“ * *** ”加入的区域。请帮帮我
我只想获取id
和name
值。
答案 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