字符串无法转换为JSONArray Android
我试图从谷歌url api中提取长url。以下是代码片段,我正在尝试继续: `StringBuilder url=新的StringBuilder(url); append(shortUrl) 包含代码的函数返回一个JSON对象。在logcat中,我看到一个错误字符串无法转换为JSONArray Android,android,json,Android,Json,我试图从谷歌url api中提取长url。以下是代码片段,我正在尝试继续: `StringBuilder url=新的StringBuilder(url); append(shortUrl) 包含代码的函数返回一个JSON对象。在logcat中,我看到一个错误org.json.JSONException:Valuehttp://www.google.com/ 无法将java.lang.String类型的at longUrl转换为JSONArray 代码正在接收完整的url(),但我对JSONAr
org.json.JSONException:Valuehttp://www.google.com/ 无法将java.lang.String类型的at longUrl转换为JSONArray
代码正在接收完整的url(),但我对JSONArray做了一些错误。尝试使用jsonObj.getString(“longUrl”)而不是jsonObj.getJSONArray(“longUrl”),因为“longUrl”是字符串而不是数组,当然,将结果分配到字符串而不是JSONArray中。附带说明:这是来自Google API的响应:
{“种类”:“urlshortener#url”,“id”:http://goo.gl/fbsS,“longUrl”:http://www.google.com/“,”状态“:“确定”}
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
if(status == 200){
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONObject jsonObj = new JSONObject(data);
JSONArray ja = jsonObj.getJSONArray("longUrl");
JSONObject last = ja.getJSONObject(0);
return last;