0

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)

1 Answer 1

3

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);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanx, I changed materials.getJSONObject(j) to zoom.getJSONObject(j) still this not working, I getting the same error.
Whats the exception stack trace? Add it to your question.
just tested this solution fixed the problem
@Mr.Me - yes, I ran it with the JSON fragment the OP included and it works for me as well. Kudos for testing.
@user1891408 - I know that you modified your JSON before posting it as an example (I had to clean it up to get it too run). Which isn't a problem, but, it looks from your exception like you may be mapping the wrong attribute as an array. You may want to include a representation of the your full JSON in the question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.