将字符串转换为JSON数组时出错

时间:2012-04-24 21:19:33

标签: android arrays string json

我在android LogCat

中收到此错误消息
04-24 21:14:02.603: W/System.err(831): org.json.JSONException: Value {"id":"602414132","first_name":"Adham","username":"adham.enaya","locale":"en_GB","link":"http:\/\/www.facebook.com\/adham.enaya","name":"Adham Enaya","last_name":"Enaya","gender":"male"} of type org.json.JSONObject cannot be converted to JSONArray
04-24 21:14:02.603: W/System.err(831):  at org.json.JSON.typeMismatch(JSON.java:107)
04-24 21:14:02.603: W/System.err(831):  at org.json.JSONArray.<init>(JSONArray.java:91)
04-24 21:14:02.603: W/System.err(831):  at org.json.JSONArray.<init>(JSONArray.java:103)
04-24 21:14:02.615: W/System.err(831):  at pit.opensource.readjson.D0ReadJSONWebServiceActivity.pasreJSON(D0ReadJSONWebServiceActivity.java:85)
04-24 21:14:02.623: W/System.err(831):  at pit.opensource.readjson.D0ReadJSONWebServiceActivity.access$0(D0ReadJSONWebServiceActivity.java:78)
04-24 21:14:02.623: W/System.err(831):  at pit.opensource.readjson.D0ReadJSONWebServiceActivity$DownloadFBUser.doInBackground(D0ReadJSONWebServiceActivity.java:108)
04-24 21:14:02.623: W/System.err(831):  at pit.opensource.readjson.D0ReadJSONWebServiceActivity$DownloadFBUser.doInBackground(D0ReadJSONWebServiceActivity.java:1)
04-24 21:14:02.623: W/System.err(831):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-24 21:14:02.633: W/System.err(831):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-24 21:14:02.633: W/System.err(831):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-24 21:14:02.633: W/System.err(831):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-24 21:14:02.633: W/System.err(831):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-24 21:14:02.633: W/System.err(831):  at java.lang.Thread.run(Thread.java:1019)

问题是什么,我认为是字符串编码?!

更新

代码在这里

//Download text -------------------------------------------------------------
private String DownloadText(String urlString){
    InputStream in = null;
    StringBuilder sb = new StringBuilder();
    try{
        in = OpenHttpConnection(urlString);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line="";
        while((line = br.readLine())!= null){
            sb.append(line);
        }
    }catch(IOException ex){

    }
    return sb.toString();
}

//Parse JSON text -----------------------------------------------------------
private FBUser pasreJSON(String url){
    FBUser user = null;
    JSONArray json;
    Log.d("JSON----------------",DownloadText(url));
    try {
        user = new FBUser();
        json = new JSONArray(DownloadText(url));
        for(int i=0;i<json.length();i++){
            JSONObject object = json.getJSONObject(i);
            user.id = object.getString("id");
            user.name = object.getString("name");
            user.first_name = object.getString("first_name");
            user.last_name = object.getString("last_name");
            user.link = object.getString("link");
            user.username = object.getString("username");
            user.gender = object.getString("gender");
            user.locale = object.getString("locale");
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return user;
}

4 个答案:

答案 0 :(得分:1)

错误是这不是JSONArray,而是JSONObject。数组用方括号括起来,而对象用大括号括起来。

查看官方网站json.org

答案 1 :(得分:1)

在这一行:

json = new JSONArray(DownloadText(url));

将其更改为:

json = new JSONObject(DownloadText(url));

显然,将json更改为JSONOBject

答案 2 :(得分:1)

尝试

JSONObject object = new JSONObject (DownloadText(url));
                     user.id = object.getString("id");   
               user.name = object.getString("name");  
                user.first_name = object.getString("first_name"); 
                 user.last_name = object.getString("last_name"); 
                 user.link = object.getString("link");         
         user.username = object.getString("username");        
          user.gender = object.getString("gender");            
      user.locale = object.getString("locale");  

答案 3 :(得分:0)

看起来pit.opensource.readjson.D0ReadJSONWebServiceActivity.pasreJSON正在尝试将Facebook用户信息响应解码为JSONArray类型。

您应该将其存储在JSONObject中。