android–JSONArray无法转换为JSONObject错误

android–JSONArray无法转换为JSONObject错误,第1张

概述在获取json数据时,我收到错误:JSONArraycannotbeconvertedtoJSONObject生成json的代码:JSONObjectparent=newJSONObject();DatabaseHandlerdbh=newDatabaseHandler(getApplicationContext());for(inti=0;i<allEds.size();i++){

在获取Json数据时,我收到错误:

JSONArray cannot be converted to JsONObject

@H_301_7@

生成Json的代码:

JsONObject parent = new JsONObject();DatabaseHandler dbh = new DatabaseHandler(getApplicationContext());              for(int i=0; i < allEds.size(); i++){                String edsText = allEds.get(i).getText().toString();                                                          //spinner = allSpns.get(i);               String spinSelected=allSpns.get(i).getSelectedItem().toString();                                 try                {                   JsONObject Json = new JsONObject();                             Json.put("ID", i);                   Json.put("FIEldname", edsText);                   Json.put("FIEldType",spinSelected);                   parent.accumulate("data", Json);                }                catch (JsONException e)                {                        // Todo auto-generated catch block                        e.printstacktrace();                }                               }Generated Json is               {"data":[{"FIEldType":"Account Number","FIEldname":"r","ID":0},  {"FIEldType":"Net      Banking ID","FIEldname":"tt","ID":1} ]}code for Json read------------------JsONObject JsonObj = new JsONObject(folderStructure);        JsONObject data = JsonObj.getJsONObject("data");         String ID = data.getString("ID");         String value = data.getString("FIEldname");         Log.d("Item name: ", value);    

在阅读上面的Json时遇到错误
代码有什么问题吗?

解决方法:

更改

JsONObject data = JsonObj.getJsONObject("data"); 

JsONArray data = JsonObj.getJsONArray("data");

由于数据的值是JsonArray而不是JsONObject.

要获取单个ID和字段名称,您应该遍历此JsONArray,如下所示:

for(int i=0; i<data.length(); i++){     JsONObject obj=data.getJsONObject(i);     String ID = obj.getString("ID");         String value = obj.getString("FIEldname");         Log.d("Item name: ", value);}
总结

以上是内存溢出为你收集整理的android – JSONArray无法转换为JSONObject错误全部内容,希望文章能够帮你解决android – JSONArray无法转换为JSONObject错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1119928.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存