Java Jsonexception-无法转换为JSONObject
我知道这个例外之前被问过一百万次,我看到各种各样的帖子说不同的建议,但对我来说还没有任何效果 我需要使用httpget从url获取一些数据,我在url中提供了一个特定的id。 我的输出应该是这样的Java Jsonexception-无法转换为JSONObject,java,android,json,http-get,Java,Android,Json,Http Get,我知道这个例外之前被问过一百万次,我看到各种各样的帖子说不同的建议,但对我来说还没有任何效果 我需要使用httpget从url获取一些数据,我在url中提供了一个特定的id。 我的输出应该是这样的 { "id": "35ZGXU7WQHFYY5BFTV6EACGEUS", "createTime": "2014-04-11T12:52:26", "updateTime": "2014-04-11T12:52:55", "status": "Completed", "transaction": {
{
"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无法将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);
}
使用此函数可以获得正确的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());
难道不是吗
result = sb.toString();
JSONObject jObject = new JSONObject(result);
您正在尝试将字符串转换为返回,该字符串包含无效的JSON数据。此处的
Amount
是一个JsonArray,您正在尝试将其强制转换为JSON对象
JSONArray JsonArray2 = jsonobject.getJSONArray("amount");
试着这样做结果的实际内容是什么?您得到了结果,但您没有在任何地方使用结果?同意上述评论。在这一行中,
JSONObject jObject=newjsonobject(toReturn)
无论toreern
是什么,它显然不是有效的JSON字符串。这是一个嵌套的JSON数组父JSON数组包含一个名为tRansaction的内部数组,tRansaction还包含一个名为Amount的JSON数组
JSONArray JsonArray2 = jsonobject.getJSONArray("amount");