2013-07-03 53 views
8

我是新来JSON,我得到follwoing例外:org.json.JSONArray不能被转换为JSONObject的

在试段本身的第一线org.json.JSONArray cannot be converted to 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文件

[ 
{ 
    "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

发布JSON PLS JSON数组节点。 – Raghunandan

+2

发布你的完整堆栈跟踪 – Triode

+0

发布你的json数据 –

回答

22

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

应该是

JSONArray jsonarray = new JSONArray(strResponse); 

[表示

{表示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"); 
} 
+0

感谢您的澄清,大多数例子都有一个数组在其中的对象,但从来不仅仅是数组本身,但这已经清除了我的东西thanx – Manny265

+0

构造函数JSONObject(int)未定义? –

+0

这应该是JSONObject jsonobject = jsonarray.getJSONObject(0); JSONObject jsonobject = new JSONObject(i); –

5

得到JSON你或许应该初始化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 

这将是正常的,如果你只是改变你的循环中得到jsonJSONObject代替(从而消除对.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

谢谢你它完成:) – DPK27

+0

没有问题@ user2545272!如果它解决了您的问题,请不要忘记标记为答案。 :-) – ninetwozero

0

期:

JSONObject json = new JSONObject(strResponse); 

这里,strResponse可在JSONArray格式由于您收到此异常,而将其转化为JSONObject

+0

strResponse是一个字符串 – DPK27

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

@ sunil你如何从url数组中获得strResponse的值........... – Amitsharma

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); 
+0

@lvo你好我也有同样的问题....你能告诉我你怎么得到strResponse的值......... – Amitsharma

相关问题