JSON字符串无法转换为对象并获得它的价值

时间:2014-07-22 08:37:11

标签: android json

这是我的JSON字符串{"Success":0,"Message":"Something Went Wrong"}我试试这个

        if (jsonStr != null){
        try {

            JSONObject jsonObj = new JSONObject(jsonStr);

            JSON_RESPONSE_Success = jsonObj.getJSONObject(TAG_Success);
            JSON_RESPONSE_Message = jsonObj.getJSONObject(TAG_Message);

             Log.d("TAG_Success: ", "> " + JSON_RESPONSE_Success);
             Log.d("TAG_Message: ", "> " + JSON_RESPONSE_Message);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }`

但它不会帮助我。它是单个对象,没有数组,因此只需要将两个值从JSON解析为字符串。

2 个答案:

答案 0 :(得分:1)

您需要将getJSONObject替换为getString,如下所示:

if (jsonStr != null){
        try {

            JSONObject jsonObj = new JSONObject(jsonStr);

            JSON_RESPONSE_Success = jsonObj.getString(TAG_Success);
            JSON_RESPONSE_Message = jsonObj.getString(TAG_Message);

             Log.d("TAG_Success: ", "> " + JSON_RESPONSE_Success);
             Log.d("TAG_Message: ", "> " + JSON_RESPONSE_Message);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }

答案 1 :(得分:0)

String JSON_RESPONSE_Success = jsonObj.getString(TAG_Success);
String JSON_RESPONSE_Message = jsonObj.getString(TAG_Message);