程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> array-轉換字符串到json數組出錯

array-轉換字符串到json數組出錯

編輯:編程綜合問答
轉換字符串到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;
}

哪出錯了呢?

最佳回答:


錯了,應該是

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

for() {

}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved