19

我是新手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"
 }
 ]
4

5 回答 5

54

这个

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");
}
于 2013-07-03T06:48:36.643 回答
6

您可能应该初始化jsonJSONArray

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

如果您只是更改循环以获取JSONObjectfrom ,那json就没问题(从而消除对 的依赖.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());
}
于 2013-07-03T06:38:59.560 回答
0

问题:

 JSONObject json = new JSONObject(strResponse);

在这里,strResponse可能是JSONArray由于您在将其转换为JSONObject.

于 2013-07-03T06:39:06.640 回答
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);

         }
于 2013-07-03T06:46:40.690 回答
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);
于 2013-07-03T06:47:18.737 回答