【问题标题】:Jsonexception - cannot be converted to JSONObject [duplicate]Jsonexception - 无法转换为 JSONObject [重复]
【发布时间】:2014-04-11 13:01:43
【问题描述】:

我知道这个异常之前被问过一百万次,我看到各种帖子说不同的建议,但对我没有任何帮助。

我需要使用 httpget 从 url 获取一些数据,并在我的 url 中提供特定的 id。 我的输出应该是这样的

{
"id": "35ZGXU7WQHFYY5BFTV6EACGEUS",
"createTime": "2014-04-11T12:52:26",
"updateTime": "2014-04-11T12:52:55",
"status": "Completed",
"transaction": {
    "amount": {
        "currencyCode": "GBP",
        "total": 7.47
    },
    "qrcode": "189e8dad99908745o7439f8ffabdfipp",
    "description": "This is the payment transaction description."
}
}

但由于某些原因出现此错误

04-11 18:24:14.655: E/STATUS_ERR(30067): org.json.JSONException: value com.mywallet.android.rest.MyWalletRestService$getStatus@41837fd0 of type java.lang.String 不能转换为 JSONObject

我的代码如下

 String line = null;
        try{
            BufferedReader in = null;


            HttpGet request = new HttpGet();
            URI website = new URI(url);
            request.setURI(website);
            request.setHeader("Accept", "application/json");
            request.setHeader(AppConstants.PAYMENT_HEADER1, BEARER);
            request.setHeader(AppConstants.content_type, AppConstants.application_json);
            response = httpClient.execute(request);


            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            while ((line = in.readLine()) != null)
            {
                sb.append(line + "\n");
            }

            result = sb.toString();
           JSONObject jObject = new JSONObject(toReturn);

            }

        if(jObject.has("error")){
            RES_STATUS      =   AppConstants.FAIL;
        }
        else{

             AppVariables.id                    =   jObject.getString(AppVariables.id_);
             AppVariables.createTime            =   jObject.getString(AppVariables.createTime_);
             AppVariables.updateTime            =   jObject.getString(AppVariables.updateTime_);
             AppVariables.status                =   jObject.getString(AppVariables.status_);             
             JSONObject oneObject               =   jObject.getJSONObject("transaction");
             JSONObject twoObject               =   oneObject.getJSONObject("amount");           
             AppVariables.total                 =   twoObject.getString("total");
             AppVariables.currencyCode          =   twoObject.getString("currencyCode");
             AppVariables.qrcode                =   oneObject.getString(AppVariables.qrcode_);
             AppVariables.description           =   oneObject.getString(AppVariables.description_);              

             MWLog.e("AppVariables.id", AppVariables.id);
             MWLog.e("AppVariables.createTime", AppVariables.createTime);
             MWLog.e("AppVariables.updateTime", AppVariables.updateTime);
             MWLog.e("AppVariables.status", AppVariables.status);
             MWLog.e("AppVariables.currencyCode", AppVariables.currencyCode);                
             MWLog.e("AppVariables.total", AppVariables.total);
             MWLog.e("AppVariables.qrcode", AppVariables.qrcode);
             MWLog.e("AppVariables.des", AppVariables.description);

             RES_STATUS                     =   AppConstants.SUCCESS;

             MWLog.e("BUYER", toReturn);
        }

【问题讨论】:

  • result的实际内容是什么?
  • 什么是返回?你得到了结果,但你没有在任何地方使用结果?
  • 同意以上评论。在这一行中JSONObject jObject = new JSONObject(toReturn); 不管toReturn 是不是一个有效的JSON 字符串。

标签: java android json http-get


【解决方案1】:

使用此函数获取正确的 json 响应

public String getResponseBody(final HttpEntity entity) throws IOException, ParseException {

    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }

    InputStream instream = entity.getContent();

    if (instream == null) {
        return "";
    }

    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException(

        "HTTP entity too large to be buffered in memory");
    }

    StringBuilder buffer = new StringBuilder();

    BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8));

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            buffer.append(line.trim());
        }

    } finally {
        instream.close();
        reader.close();
    }
    return buffer.toString().trim();

}

如何使用?

result= getResponseBody(response.getEntity());

【讨论】:

    【解决方案2】:
           result = sb.toString();
           JSONObject jObject = new JSONObject(toReturn);
    

    不应该是这样的

           result = sb.toString();
           JSONObject jObject = new JSONObject(result);
    

    您正在尝试将字符串转换为返回,该字符串包含无效的 JSON 数据。

    【讨论】:

      【解决方案3】:

      这里 Amount 是一个 JsonArray,您正在尝试将它投射到 json 对象中

      JSONArray JsonArray2 = jsonobject.getJSONArray("amount");
      

      试试这个方法

      【讨论】:

      • 这是一个嵌套的 Json 数组,你的父 Json 数组包含一个名为 tRansaction 和 transaction 的内部数组,还包含一个名为 Amount 的 json 数组
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      相关资源
      最近更新 更多