org.json.JSONArray 无法转换为 JSONObject。

23

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

org.json.JSONArray无法在try部分的第一行转换为JSONObject

请帮助我解决这个问题。以下是我的代码:

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"
 }
 ]

3
请发布完整的堆栈跟踪信息。 - Triode
5个回答

60

这个

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");
}

感谢澄清,大多数示例都有一个包含数组的对象,但从未仅有数组本身,但是这为我澄清了事情,谢谢。 - Manny265
构造函数 JSONObject(int) 未定义? - T_V
应该使用 JSONObject jsonobject = jsonarray.getJSONObject(0); 而不是 JSONObject jsonobject = new JSONObject(i); - T_V
@TarunVarshney 是的,那是一个打字错误。现在已经更正了。 - Raghunandan
1
好家伙,这真是让我大吃一惊。 - Techgration

6

您应该将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());
}

0

问题:

 JSONObject json = new JSONObject(strResponse);

在这里,strResponse 可能是 JSONArray 格式的,因此在将其转换为 JSONObject 时会出现异常。


0

试试这个,你的第一个块是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);

         }

0
如果你实际接收到的确实是这个 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的值吗? - Amitsharma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接