2013-07-03 31 views
8

Tôi mới vào JSON và tôi nhận được ngoại lệ follwoing:org.json.JSONArray không thể được chuyển đổi sang JSONObject

org.json.JSONArray cannot be converted to JSONObject trong dòng đầu tiên của phần thử chính nó.

Vui lòng giúp tôi xóa mục này. Dưới đây là mã của tôi:

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

đây là json Tôi nhận được từ file php của tôi

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

đăng json pls. – Raghunandan

+2

Đăng dấu vết ngăn xếp đầy đủ của bạn – Triode

+0

đăng dữ liệu json của bạn –

Trả lời

22

này

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

nên

JSONArray jsonarray = new JSONArray(strResponse); 

[ đại diện cho nút mảng json

{ đại diện json nút đối tượng

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

nhờ làm rõ, hầu hết các ví dụ có một đối tượng với một mảng trong nó nhưng không bao giờ chỉ là mảng riêng của nó, nhưng điều này đã xóa mọi thứ đối với tôi thanx – Manny265

+0

Constructor JSONObject (int) là không xác định? –

+0

Điều này có thể là JSONObject jsonobject = jsonarray.getJSONObject (0); insted của JSONObject jsonobject = new JSONObject (i); –

5

Bạn có lẽ nên khởi json như một JSONArray:

JSONObject json = new JSONObject(strResponse); 

nên sau đó là:

JSONArray json = new JSONArray(strResponse); 

Tuy nhiên, điều đó sẽ không làm việc với hai hoạt động sau đây:

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

Điều đó sẽ không sao nếu bạn chỉ cần thay đổi vòng lặp của bạn để có được những JSONObject từ json thay vì (do đó loại bỏ sự phụ thuộc đối với .names():

JSONObject e = json.getJSONObject(i); 

chỉnh sửa: toàn đang

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

cảm ơn bạn Đã hoàn tất :) – DPK27

+0

Không có vấn đề gì @ user2545272!Đừng quên đánh dấu đây là câu trả lời nếu nó giải quyết được vấn đề của bạn. :-) – ninetwozero

0

Vấn đề:

JSONObject json = new JSONObject(strResponse); 

đây, strResponse có thể nằm trong định dạng của JSONArray do mà bạn đang nhận được ngoại lệ này trong khi chuyển đổi nó thành JSONObject.

+0

strResponse là một chuỗi – DPK27

0

thử thế này, khối đầu tiên của bạn là mảng json do đó, có mảng json đầu tiên

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 làm thế nào u nhận được các giá trị của strResponse từ mảng url ........... – Amitsharma

0

nếu đó thực sự là json bạn nhận được, bạn nên thay thế toàn bộ này:

JSONObject json = new JSONObject(strResponse); 

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

với

JSONArray internships = json.toJSONArray(strResponse); 
+0

@ lvo hi i có cùng một vấn đề .... u có thể cho tôi biết làm thế nào u lấy giá trị của strResponse ......... – Amitsharma

Các vấn đề liên quan