Dim
Dim

Reputation: 4837

Android reading JSONArray from JSONObject

I am trying to read from json file, the file have JSONArray inside JSONObject, it is reading the first JSONObject value but not reading from inside inside array called "zoom", can you please help me?

    {
   "@file_name": "materials",
   "materials": [
        {
            "@site_name": "N/A",
            "@site_icon_iPad_xPosition": "150",
            "@site_icon_iPad_yPosition": "150",
            "@site_icon_Android_xPosition": "215",
            "@site_icon_Android_yPosition": "206",
            "zoom": 
                {
                    "@zoom_name": "Main Reservoir",
                    "@zoom_number": "zoom1"

                }

        },
        {
            "@site_name": "Building Applications",
            "@site_icon_iPad_xPosition": "636",
            "@site_icon_iPad_yPosition": "313",
            "@site_icon_Android_xPosition": "215",
            "@site_icon_Android_yPosition": "206",
            "zoom": [
                {
                    "@zoom_name": "Rooftop Reservoir & Pump Station",
                    "@zoom_number": "zoom10-1"

                },
                {
                    "@zoom_name": "Compact Zone PRV System",
                    "@zoom_number": "zoom10-2"

                },
                {
                    "@zoom_name": "Basement Reservoir & Pump Room",
                    "@zoom_number": "zoom10-3"

                }
            ]           
        }
   ]
}

My code:

 JSONParser jParser = new JSONParser();
         JSONObject json = jParser.getJSONFromUrl(Environment.getExternalStorageDirectory().getPath()+"/Materials.json");

            try {


                JSONArray materials = json.getJSONArray("materials");
                for(int i = 0; i < materials.length(); i++){

                    JSONObject c = materials.getJSONObject(i);

                    // Storing each json item in variable
                    String a1= c.getString("@site_name");
                    String a2 = c.getString("@site_icon_Android_xPosition");
                    String a3 = c.getString("@site_icon_Android_yPosition");

                    Log.d(TAG, a1+" "+a2+" "+a3);


                    JSONArray zoom = c.getJSONArray("zoom");
                    Log.d(TAG, ""+zoom.length());
                    for(int j = 0; j < zoom.length(); j++){

                        JSONObject d = zoom.getJSONObject(j);
                         String b1 = d.getString("@zoom_name");
                         String b2 = d.getString("@zoom_number");
                         Log.d(TAG, b1+" "+b2);
                    }

                   Log.d(TAG, "--------------------------------");


                }
            } catch (JSONException e) {
                 Log.d(TAG, "exeption");
                e.printStackTrace();
            }

My output:

Applications 215 206
exeption

Exception:

03-24 11:29:01.188: W/System.err(17952): org.json.JSONException: Value {"@zoom_number":"zoom1","@zoom_name":"Main Reservoir"} at zoom of type org.json.JSONObject cannot be converted to JSONArray
03-24 11:29:01.198: W/System.err(17952):    at org.json.JSON.typeMismatch(JSON.java:100)
03-24 11:29:01.198: W/System.err(17952):    at org.json.JSONObject.getJSONArray(JSONObject.java:548)
03-24 11:29:01.198: W/System.err(17952):    at com.weterworks.MainActivity.onCreate(MainActivity.java:120)
03-24 11:29:01.198: W/System.err(17952):    at android.app.Activity.performCreate(Activity.java:4465)
03-24 11:29:01.198: W/System.err(17952):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-24 11:29:01.198: W/System.err(17952):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-24 11:29:01.198: W/System.err(17952):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-24 11:29:01.198: W/System.err(17952):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-24 11:29:01.198: W/System.err(17952):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-24 11:29:01.198: W/System.err(17952):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 11:29:01.198: W/System.err(17952):    at android.os.Looper.loop(Looper.java:137)
03-24 11:29:01.198: W/System.err(17952):    at android.app.ActivityThread.main(ActivityThread.java:4424)
03-24 11:29:01.198: W/System.err(17952):    at java.lang.reflect.Method.invokeNative(Native Method)
03-24 11:29:01.198: W/System.err(17952):    at java.lang.reflect.Method.invoke(Method.java:511)
03-24 11:29:01.198: W/System.err(17952):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-24 11:29:01.198: W/System.err(17952):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-24 11:29:01.198: W/System.err(17952):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 366

Answers (1)

Perception
Perception

Reputation: 80633

You have a small error in your code. In your inner loop that processes the zoom property, you are referring to the wrong array object.

for (int j = 0; j < zoom.length(); j++) {

    JSONObject d = materials.getJSONObject(j); // <-- Should refer to zoom, not materials
    String b1 = d.getString("@zoom_name");
    String b2 = d.getString("@zoom_number");
    Log.d(TAG, b1 + " " + b2);
}

Upvotes: 3

Related Questions