-1

I have JSONArray having following structure:

{

    "People":[
        {
            "006MST21IND":{
                "desc":"MST21 BAL. PR. THERMOSTATIC STEAM TRAP",
                "attribute":"1,b_“F”ELEMENT / c_`G' ELEMENT;2,b_“F”ELEMENT / c_`G' ELEMENT;3, b_“F”ELEMENT / c_`G' ELEMENT"
            }
        },
        {
            "006MST22IND":{
                "desc":"MST21 BAL. PR. THERMOSTATIC STEAM TRAP",
                "attribute":"1,b_“F”ELEMENT / c_`G' ELEMENT;2,b_“F”ELEMENT / c_`G' ELEMENT;3, b_“F”ELEMENT / c_`G' ELEMENT"
            }
        }
    ]

}

I am trying, but it is giving following exception,

org.json.JSONException: Value [{"006MST21IND":{"attribute":"1,b_�F�ELEMENT \/ c_`G' ELEMENT;2,b_�F�ELEMENT \/ c_`G' ELEMENT;3, b_�F�ELEMENT \/ c_`G' ELEMENT","desc":"MST21 BAL. PR. THERMOSTATIC STEAM TRAP"}},{"006MST22IND":{"attribute":"1,b_�F�ELEMENT \/ c_`G' ELEMENT;2,b_�F�ELEMENT \/ c_`G' ELEMENT;3, b_�F�ELEMENT \/ c_`G' ELEMENT","desc":"MST21 BAL. PR. THERMOSTATIC STEAM TRAP"}}] 
at People of type org.json.JSONArray cannot be converted to JSONObject

code is :

I am saving json data in file and taking from it as,

File root = Environment.getExternalStorageDirectory();
File jsonFile = new File(root, "jsonFile.txt");
FileInputStream stream;
String jsonStr = null;
stream = new FileInputStream(jsonFile);



FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

jsonStr = Charset.defaultCharset().decode(bb).toString();


stream.close();

then by using that jsonStr, i am using following logic...

JSONObject jsonObj = new JSONObject(jsonStr);

JSONObject jsonObj1 = jsonObj.getJSONObject("People");
JSONArray myJson=jsonObj1.getJSONArray("006MST21IND");

if (myJson!=null && myJson.length()!=0) {


    for(int i=0;i<myJson.length();i++)
    {
        JSONObject obj2 = myJson.getJSONObject(i);

        desc = obj2.getString("desc");
        attribute = obj2.getString("attribute");

        Log.e("Desc:", desc);
        Log.e("Attribute:", attribute);
    }

}

I was trying but failed.

Sufian
  • 6,405
  • 16
  • 66
  • 120
  • 1
    hard to read edit your question – Raghunandan Jul 09 '14 at 06:05
  • Use try {} catch ().... and try to debug .... on which line you get an error .. see the error .. you will easily understand that the keyword you are parsing is "JSONArray" or "JSONObject" – Akarsh M Jul 09 '14 at 06:07
  • i think you are getting jsonarray as response but you are trying to convert it into jsonobject..please check the response – asiya Jul 09 '14 at 06:10
  • 1
    It clearly states `People of type org.json.JSONArray cannot be converted to JSONObject`. – Marius Jul 09 '14 at 06:11
  • As @Marius said, the error is clear. You might like to use http://json.parser.online.fr/ to assist parsing the JSON. There are some cool features like "Show JS Types" in "options"). – Sufian Aug 05 '16 at 12:37
  • Possible duplicate of [org.json.jsonarray cannot be converted to jsonobject error](http://stackoverflow.com/questions/21129318/org-json-jsonarray-cannot-be-converted-to-jsonobject-error) – Sufian Aug 05 '16 at 12:38

2 Answers2

0

pepole is not object it is array
JSONArray jsonarr = jsonObj.getJSONArray ("People");

0

You trying to fetch JSONobject as JSONarray and fetch JSONarray as JSONobject.Try following :

 try {
        JSONObject jsonObj = new JSONObject(jsonStr);

        JSONarray arry = jsonObj.getJSONarray("People")

        if (arry.length()!=0) {

        for(int i=0;i<arry.length();i++)
        {
           JSONObject obj2 = arry.getJSONObject(i);

           desc = obj2.getString("desc");
           attribute = obj2.getString("attribute");

           Log.e("Desc:", desc);
           Log.e("Attribute:", attribute);
        }
     }

   } catch(Exception e){
       //Catch Error Here
 }
Nikhil
  • 312
  • 2
  • 13