AnnBnnCnnD 2013-05-29 07:16 采纳率: 0%
浏览 6797
已采纳

转换字符串到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条回答 默认 最新

  • 码密码密哄 2013-05-29 07:28
    关注

    错了,应该是

    JSONObject json = new JSONObject(DownloadText(url));
    JsonArray jsonArray  = json.getJsonArray("yourArrayKey");
    
    for() {
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!