2 annbnncnnd AnnBnnCnnD 于 2013.05.29 15:16 提问

转换字符串到json数组出错

在Android 日志:

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;
}

哪出错了呢?

2个回答

cj6585256
cj6585256   2013.05.29 15:28
已采纳

错了,应该是

JSONObject json = new JSONObject(DownloadText(url));
JsonArray jsonArray  = json.getJsonArray("yourArrayKey");

for() {

}
AnnBnnCnnD
AnnBnnCnnD 谢谢
4 年多之前 回复
Kill_it
Kill_it   2013.05.29 16:31
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");  
Csdn user default icon
上传中...
上传图片
插入图片