2013-07-03 28 views
8

Jestem nowy JSON i otrzymuję wyjątek follwoing:org.json.JSONArray nie mogą być konwertowane do JSONObject

org.json.JSONArray cannot be converted to JSONObject w pierwszym wierszu samej sekcji try.

Proszę, pomóż mi to usunąć. Oto mój kod:

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

to json otrzymuję od mojego pliku 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

POST json pls. – Raghunandan

+2

Prześlij swój pełny ślad stosu – Triode

+0

dodaj swoje dane json –

Odpowiedz

22

Ten

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

należy

JSONArray jsonarray = new JSONArray(strResponse); 

[ oznacza json węzeł tablicy

{ oznacza json węzłowi

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

dzięki za wyjaśnienie, większość przykładów mieć obiekt z tablicy w nim, ale nie tylko tablicy na własną rękę, lecz to, co otworzyło dla mnie thanx – Manny265

+0

konstruktora JSONObject (int) jest niezdefiniowany? –

+0

To powinno być JSONObject jsonobject = jsonarray.getJSONObject (0); insted z JSONObject jsonobject = new JSONObject (i); –

5

Powinieneś raczej zainicjować json jako JSONArray:

JSONObject json = new JSONObject(strResponse); 

Następnie należy:

JSONArray json = new JSONArray(strResponse); 

jednak, że nie będzie działać z dwóch następujących operacji:

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

to byłoby w porządku, jeśli tylko zmienia swoją pętlę, aby uzyskać JSONObject z json zamiast (usuwając zależność kierunku .names():

JSONObject e = json.getJSONObject(i); 

Edit: Pełny kod

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

dziękuję Wykonałeś :) – DPK27

+0

Nie ma problemu @ user2545272!Nie zapomnij oznaczyć tej odpowiedzi jako rozwiązania problemu. :-) – ninetwozero

0

Problem:

JSONObject json = new JSONObject(strResponse); 

tutaj, strResponse może być w formacie JSONArray dzięki której otrzymujesz ten wyjątek, gdy przekształcenie go w JSONObject.

+0

strResponse jest String – DPK27

0

próbować ten jeden, Twój pierwszy blok jest json tablicy więc dostać pierwszej tablicy 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 jak u uzyskać wartości z tablicy strResponse url ........... – Amitsharma

0

jeśli to naprawdę jest json otrzymujesz należy wymienić cały ten:

JSONObject json = new JSONObject(strResponse); 

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

z

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

@lvo Witam mam taki sam problem .... u może mi powiedzieć jak u uzyskać wartości strResponse ......... – Amitsharma

Powiązane problemy