android - 无法将此字符串转换为 JSON

标签 android arrays json

我正在尝试将以下字符串转换为 JSONArray。

 try {
        BufferedReader inputReader = new BufferedReader(new InputStreamReader(
                openFileInput(mFileName), "UTF-8"));
        String inputString;
        StringBuffer stringBuffer = new StringBuffer();
        while ((inputString = inputReader.readLine()) != null) {
            stringBuffer.append(inputString + "\n");
        }

        JSONArray JArray = new JSONArray(stringBuffer.toString());
        for(int i = 0; i < JArray.length() + 1; i++) {
            JSONObject obj = JArray.getJSONObject(i);
            mWrite = mWrite + obj.toString() + ",";

        }

问题是我得到这个错误:

org.json.JSONException: Value {"friends":[{"name":"paul","level":"56.73","focus_level":"53.55","focus_points":32129,"max_streak":8760,"total_days_coded":780,"total_flow_states":1081,"time_spent":2109716,"programming_now":true,"current_language":"RegExp","streaking_now":false,"platforms":{"Linux":{"percent_work":0.88,"points":22,"time":6932},"Windows 7":{"percent_work":3.26,"points":82,"time":17326},"linux":{"percent_work":48.74,"points":1237,"time":1196665},"windows":{"percent_work":47.12,"points":1196,"time":890146}},"languages":{"ANY":{"level":"0.36","points":5},"ActionScript":{"level":"0.00","points":0},"ApacheConf":{"level":"0.09","points":1},"ApacheConfig":{"level":"0.00","points":0},"Bash":{"level":"0.00","points":0},"CSS":{"level":"1.23","points":30},"CoffeeScript":{"level":"0.73","points":14},"Diff":{"level":"0.00","points":0},"ECMA Script Level 4":{"level":"0.00","points":0},"GenericSQL":{"level":"0.00","points":0},"Git Commit Message":{"level":"0.09","points":1},"GitHub Markdown":{"level":"0.00","points":0},"Groovy":{"level":"0.00","points":0},"HTML":{"level":"4.32","points":256},"HTML (Django)":{"level":"0.05","points":0},"HTML (Rails)":{"level":"0.09","points":1},"HTML 5":{"level":"1.27","points":32},"Handlebars":{"level":"0.91","points":19},"Host Config":{"level":"0.00","points":0},"JAVA":{"level":"0.00","points":0},"JSON":{"level":"0.91","points":20},"Java":{"level":"0.27","points":4},"JavaScript":{"level":"8.18","points":833},"JavaScript 1":{"level":"0.00","points":0},"Language: JAVA":{"level":"0.05","points":0},"Lisp":{"level":"0.09","points":1},"Markdown":{"level":"1.59","points":46},"MySQL":{"level":"0.00","points":0},"Null Grammar":{"level":"0.05","points":1},"PHP":{"level":"11.36","points":1550},"Plain":{"level":"7.41","points":692},"Plain Text":{"level":"2.59","points":104},"PlainTasks":{"level":"1.91","points":61},"Python":{"level":"3.32","points":158},"R":{"level":"0.00","points":0},"RegExp":{"level":"0.00","points":0},"Ruby":{"level":"0.41","points":6},"SCSS":{"level":"0.00","points":0},"SQL":{"level":"0.32","points":5},"SVN Edit":{"level":"0.23","points":3},"Shell":{"level":"0.14","points":1},"Shell-Unix-Generic":{"level":"0.36","points":6},"Syntaxes":{"level":"0.00","points":0},"TEXT":{"level":"0.00","points":0},"TypeScript":{"level":"0.36","points":5},"XML":{"level":"0.86","points":19},"XSL":{"level":"0.00","points":0},"YAML":{"level":"0.14","points":1},"color":{"level":"1.09","points":25},"laravel-blade":{"level":"0.00","points":0},"regex":{"level":"0.00","points":0},"syntax":{"level":"0.23","points":3},"yaml":{"level":"0.00","points":0}}}]} of type org.json.JSONObject cannot be converted to JSONArray

有人知道这里会发生什么吗?我是 JSON 的新手。

最佳答案

JSON 的根是一个对象,而不是数组。试试这个:

JSONArray JArray = new JSONObject(stringBuffer.toString()).getJSONArray("friends");

关于android - 无法将此字符串转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35689532/

相关文章:

android - 如何在不知道当前 Activity 的情况下在 Android 应用程序中显示对话框

android - 对话框 fragment 中的 onActivityResult

javascript - 为什么javascript程序不运行

android - Xamarin Android如何使用手机默认打印机功能打印从WebView打开的PDF内容

android.content.res.Resources$NotFoundException : no such label

javascript - 将属性添加到对象数组

java - 从嵌套数组中提取值

c# - 为什么 JsonSerializer 给空?

json - 如何将 getJSON 数据绑定(bind)到 ReactJS 状态

java - 提取 Facebook 页面的链接