Can't convert this string to JSON
我正在尝试将以下字符串转换为JSONArray。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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 +"\ "); } JSONArray JArray = new JSONArray(stringBuffer.toString()); for(int i = 0; i < JArray.length() + 1; i++) { JSONObject obj = JArray.getJSONObject(i); mWrite = mWrite + obj.toString() +","; } |
问题是我收到此错误:
1 | 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的根是对象,而不是数组。试试这个:
1 | JSONArray JArray = new JSONObject(stringBuffer.toString()).getJSONArray("friends"); |
我在Google Play中可用的查看器(JOE-JSON对象资源管理器)中打开了字符串。它显示了包含数组的根对象好友。如果您不熟悉JSON,JOE可能会帮助您更好地理解。