org.json.JSONArray 无法转换为 JSONObject

Posted

技术标签:

【中文标题】org.json.JSONArray 无法转换为 JSONObject【英文标题】:org.json.JSONArray cannot be converted to JSONObject 【发布时间】:2013-06-30 17:52:42 【问题描述】:

我是 JSON 的新手,我遇到了以下异常:

org.json.JSONArray cannot be converted to JSONObject 在 try 部分本身的第一行。

请帮我删除它。这是我的代码:

try    
    JSONObject json = new JSONObject(strResponse);

    //Get the element that holds the internship ( JSONArray )
    JSONArray name = json.names();
    JSONArray  internships = json.toJSONArray(name);

    //Loop the Array
    for(int i=0;i < internships.length();i++)      
        Log.e("Message","loop");
        HashMap<String, String> map = new HashMap<String, String>();
        JSONObject e = internships.getJSONObject(i);
        map.put("id",  String.valueOf("id"));
        map.put("title", "Title :" + e.getString("title"));
        map.put("company", "Company : " +  e.getString("company"));
        map.put("category", "Category : " +  e.getString("category"));
        mylist.add(map);
     
 catch(JSONException e) 
    Log.e("log_tag", "Error parsing data "+e.toString());

这是我从我的 php 文件中得到的 json

[
 
    "id": "31",
    "title": "Business Development - Executive",
    "company": "Indidelights",
    "category": "Sales and Business Development"
 ,
 
    "id": "40",
    "title": "Business Development - Ecommerce MH",
    "company": "Ram Gopal & Co",
    "category": "Sales and Business Development"
 ,
 
    "id": "41",
    "title": "Sales and Business development intern",
    "company": "Esanchalak",
    "category": "Sales and Business Development"
 ,
 
    "id": "42",
    "title": "Purchase Executive",
    "company": "Winni.in",
    "category": "Marketing"
 ,
 
    "id": "43",
    "title": "Marketing Intern",
    "company": "Walkover Web Solutions Pvt. Ltd.",
    "category": "Marketing"
 ,
 
    "id": "44",
    "title": "Marketing Intern",
    "company": "SkillKindle Learning Pvt Ltd",
    "category": "Marketing"
 ,
 
    "id": "45",
    "title": "Graphic Designer",
    "company": "Stylopa",
    "category": "Graphic Design / Art Work"
 ,
 
    "id": "46",
    "title": "Graphic Designer",
    "company": "LycondonFX",
    "category": "Graphic Design / Art Work"
 ,
 
    "id": "47",
    "title": "Web Designer",
    "company": "Xapify LLC",
    "category": "Software"
 ,
 
    "id": "48",
    "title": "Web Designer (Frontend)",
    "company": "gotrademark.in",
    "category": "Web Design and Development"
 ,
 
    "id": "49",
    "title": "Content Writing Intern",
    "company": "National Entrepreneurship Network",
    "category": "Content Writing / Journalism"
 ,
 
    "id": "50",
    "title": "Content Writing Intern",
    "company": "Pragmatum Training Pvt Ltd",
    "category": "Content Writing / Journalism"
 ,
 
    "id": "51",
    "title": "HR Intern",
    "company": "GATI Kintetsu Express Pvt Ltd",
    "category": "HR / Recruitment"
 ,
 
    "id": "52",
    "title": "Pharma Intern",
    "company": "Qlinics Health Care Pvt Ltd",
    "category": "BioTechnology / Pharma"
 ,
 
    "id": "53",
    "title": "android Developer",
    "company": "InoXapps Mobile Solutions Pvt Ltd",
    "category": "Mobile App Development"
 ,
 
    "id": "54",
    "title": "Mobile App developer",
    "company": "RV Media Inc",
    "category": "Mobile App Development"
 ,
 
    "id": "55",
    "title": "Electronics Intern",
    "company": "GA SOFTWARE TECHNOLOGIES PVT LTD",
    "category": "Electronics Engineering"
 
 ]

【问题讨论】:

发布您的完整堆栈跟踪 【参考方案1】:

这个

JSONObject json = new JSONObject(strResponse);
// your strResponse is a json array 

应该是

JSONArray jsonarray = new JSONArray(strResponse);

[代表json数组节点

代表json对象节点

for(int i=0; i < jsonarray.length(); i++) 
    JSONObject jsonobject = jsonarray.getJSONObject(i);
    String id       = jsonobject.getString("id");
    String title    = jsonobject.getString("title");
    String company  = jsonobject.getString("company");
    String category = jsonobject.getString("category");

【讨论】:

感谢您的澄清,大多数示例都有一个包含数组的对象,但绝不是单独的数组,但这对我来说已经清除了事情thanx 构造函数 JSONObject(int) 未定义? 这应该是 JSONObject jsonobject = jsonarray.getJSONObject(0);插入 JSONObject jsonobject = new JSONObject(i); @TarunVarshney 是的,这是一个典型的错误。现已改正。 我的按钮好了【参考方案2】:

您可能应该将json 初始化为JSONArray

JSONObject json = new JSONObject(strResponse);

那么应该是:

JSONArray json = new JSONArray(strResponse);

但是,这不适用于以下两个操作:

JSONArray name = json.names(); //.names() doesn't exist in JSONArray
JSONArray  internships = json.toJSONArray(name); // Is instead to be seen as

如果您只是更改循环以从 json 获取 JSONObject 就可以了(从而消除对 .names() 的依赖:

JSONObject e = json.getJSONObject(i);

编辑:完整代码

try    
    JSONArray internships = new JSONArray(strResponse);

    //Loop the Array
    for(int i=0;i < internships.length();i++)      
        Log.e("Message","loop");
        HashMap<String, String> map = new HashMap<String, String>();
        JSONObject e = internships.getJSONObject(i);
        map.put("id",  String.valueOf("id"));
        map.put("title", "Title :" + e.getString("title"));
        map.put("company", "Company : " +  e.getString("company"));
        map.put("category", "Category : " +  e.getString("category"));
        mylist.add(map);
     
 catch(JSONException e) 
    Log.e("log_tag", "Error parsing data "+e.toString());

【讨论】:

没问题@user2545272!如果它解决了您的问题,请不要忘记将其标记为答案。 :-)【参考方案3】:

问题:

 JSONObject json = new JSONObject(strResponse);

这里,strResponse 的格式可能为 JSONArray,因此您在将其转换为 JSONObject 时遇到此异常。

【讨论】:

【参考方案4】:

试试这个,你的第一个块是 json 数组所以得到第一个 json 数组

JSONArray jsonarray = new JSONArray(strResponse);

    for(int i=0;i < jsonarray .length();i++) 
    JSONObject jsonobj = new JSONObject(i);
            map.put("id",   jsonobj .getString("id"));
            map.put("title",  jsonobj .getString("title"));
            map.put("company",  jsonobj .getString("company"));
            map.put("category",  jsonobj .getString("category"));
            mylist.add(map);

         

【讨论】:

【参考方案5】:

如果那真的是你收到的 json,你应该替换整个这个:

JSONObject json = new JSONObject(strResponse);

//Get the element that holds the internship ( JSONArray )
JSONArray name = json.names();
JSONArray  internships = json.toJSONArray(name);

JSONArray  internships = json.toJSONArray(strResponse);

【讨论】:

@lvo 嗨,我有同样的问题....你能告诉我你是如何获得 strResponse 的值的......

以上是关于org.json.JSONArray 无法转换为 JSONObject的主要内容,如果未能解决你的问题,请参考以下文章

org.json.JSONArray 类型的无法转换为 JSONObject

org.json.JSONArray 类型的值无法转换为 JSONObject

react-native android中出现此问题“无法转换类型为org.json.JSONArray的参数”的原因是啥?

Android Kotlin Volley 如何发送 JSON 数据

org.json.JSONObject & org.json.JSONArray

不同json如何使用jsonArray以及ajax如何取,实现读取新闻