1
   {
   "TABLE":[
      {
         "ROW":[
            {
               "COL":[
                  {
                     "DATA":"< OutBoundSMS PhoneId='3' PhoneNo='1111111111' MessageText='OutBound SMS Application Test' />"
                  }
               ]
            }
         ]
      }
   ]
}

How to parse json array like this format?
I am getting this error while parsing: "cannot be converted to json object".

3 Answers 3

6

Parse Current Json String as:

JSONObject json=new JSONObject("Your Json String");
JSONArray jsonarray = json.getJSONArray("TABLE");

for(int i=0;i<jsonarray.length();i++){

  JSONObject jsonnew=jsonarray.getJSONObject(i);
  JSONArray jsonarrayROW = jsonnew.getJSONArray("ROW");

   for(int j=0;j<jsonarrayROW.length();j++){
     JSONObject jsonnewtwo=jsonarrayROW.getJSONObject(j);
     JSONArray jsonarrayCOL = jsonnewtwo.getJSONArray("COL");

      for(int k=0;k<jsonarrayCOL.length();k++){

        JSONObject jsonnewthree=jsonarrayCOL.getJSONObject(k);

        //get DATA here

           String str_data=jsonnewthree.getString("DATA");
       }
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

its work but again i wont able to get the data in hashmap from OutBoundSMS tag so plz suggest
@NitinGupta : string you are getting in DATA is not a xml or not an other json string. so you will need to split it on the base of ' and use replace for removing unwanted characters
0

see here

String jsonStr = '{"menu": {' + 
        '"id": "file",' + 
        '"value": "File",' + 
        '"popup": {' + 
          '"menuitem": [' + 
            '{"value": "New", "onclick": "CreateNewDoc()"},' + 
            '{"value": "Open", "onclick": "OpenDoc()"},' + 
            '{"value": "Close", "onclick": "CloseDoc()"}' + 
          ']' + 
        '}' + 
      '}}'; 

here see good example JSON PARSING

1 Comment

but its array of array could u suugest again NagarjunaReddy.P
0

Its Working parse json data array inside array

http://maps.googleapis.com/maps/api/directions/json?origin=28.4759618,77.3140897&destination=28.3936072,77.3172154&sensor=false&mode=driving&alternatives=true

    {
       "geocoded_waypoints" : [
          {
             "geocoder_status" : "OK",
             "place_id" : "ChIJzfXobhTnDDkRJQ2M4AEkun8",
             "types" : [ "street_address" ]
          },
          {
             "geocoder_status" : "OK",
             "place_id" : "ChIJrz2dDk_cDDkRmnC23K0BpD4",
             "types" : [ "street_address" ]
          }
       ],
       "routes" : [
          {
             "bounds" : {
                "northeast" : {
                   "lat" : 28.4808871,
                   "lng" : 77.3195229
                },
                "southwest" : {
                   "lat" : 28.3932808,
                   "lng" : 77.30536649999999
                }
             },
             "copyrights" : "Map data ©2017 Google",
             "legs" : [
                {
                   "distance" : {
                      "text" : "11.9 km",
                      "value" : 11902
                   },
                   "duration" : {
                      "text" : "22 mins",
                      "value" : 1313
                   },
                   "end_address" : "384, Sector-16, Sector 15A, Faridabad, Haryana 121007, India",
                   "end_location" : {
                      "lat" : 28.3936165,
                      "lng" : 77.31753990000001
                   },
                   "start_address" : "A-105, Ashoka Enclave Part II, Sector 37, Faridabad, Haryana 121003, India",
                   "start_location" : {
                      "lat" : 28.4759614,
                      "lng" : 77.3139839
                   },
                   "steps" : [
                      {
                         "distance" : {
                            "text" : "14 m",
                            "value" : 14
                         },
                         "duration" : {
                            "text" : "1 min",
                            "value" : 3
                         },
                         "end_location" : {
                            "lat" : 28.4760899,
                            "lng" : 77.31398329999999
                         },
                         "html_instructions" : "Head \u003cb\u003enorth\u003c/b\u003e",
                         "polyline" : {
                            "points" : "wuxlDkk{vMY?"
                         },
                         "start_location" : {
                            "lat" : 28.4759614,
                            "lng" : 77.3139839
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "0.4 km",
                            "value" : 440
                         },
                         "duration" : {
                            "text" : "2 mins",
                            "value" : 116
                         },
                         "end_location" : {
                            "lat" : 28.4766301,
                            "lng" : 77.3097228
                         },
                         "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e toward \u003cb\u003eOld Sher Shah Suri Road\u003c/b\u003e/\u003cb\u003eSarai Khawaja Rd\u003c/b\u003e",
                         "maneuver" : "turn-left",
                         "polyline" : {
                            "points" : "qvxlDkk{vMApB?Z?jB?bACvDAx@?T@v@?@Dx@?H?FADABCBw@bAQRY`@"
                         },
                         "start_location" : {
                            "lat" : 28.4760899,
                            "lng" : 77.31398329999999
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "0.5 km",
                            "value" : 503
                         },
                         "duration" : {
                            "text" : "1 min",
                            "value" : 81
                         },
                         "end_location" : {
                            "lat" : 28.4808871,
                            "lng" : 77.30797579999999
                         },
                         "html_instructions" : "Slight \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eOld Sher Shah Suri Road\u003c/b\u003e/\u003cb\u003eSarai Khawaja Rd\u003c/b\u003e",
                         "maneuver" : "turn-slight-right",
                         "polyline" : {
                            "points" : "}yxlDwpzvMIDiF|AcBh@{Bp@eGhByBp@"
                         },
                         "start_location" : {
                            "lat" : 28.4766301,
                            "lng" : 77.3097228
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "0.3 km",
                            "value" : 255
                         },
                         "duration" : {
                            "text" : "1 min",
                            "value" : 50
                         },
                         "end_location" : {
                            "lat" : 28.4808239,
                            "lng" : 77.30536649999999
                         },
                         "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eMain Rd\u003c/b\u003e",
                         "maneuver" : "turn-left",
                         "polyline" : {
                            "points" : "qtylD{ezvMHnEBxH"
                         },
                         "start_location" : {
                            "lat" : 28.4808871,
                            "lng" : 77.30797579999999
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "1.0 km",
                            "value" : 1013
                         },
                         "duration" : {
                            "text" : "2 mins",
                            "value" : 125
                         },
                         "end_location" : {
                            "lat" : 28.4717998,
                            "lng" : 77.30594619999999
                         },
                         "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eNH19\u003c/b\u003e/\u003cb\u003eNH44\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Hotel Saffron Kiran (on the left in 1.0&nbsp;km)\u003c/div\u003e",
                         "maneuver" : "turn-left",
                         "polyline" : {
                            "points" : "ctylDquyvMhA?^An@?lAEfAGrEQzBMH?DALEJGzCw@TC`@C^AV?P?T@rALL?v@B\\?VAz@CjB@h@?X?fBG~@B^NrCKTA"
                         },
                         "start_location" : {
                            "lat" : 28.4808239,
                            "lng" : 77.30536649999999
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "7.5 km",
                            "value" : 7549
                         },
                         "duration" : {
                            "text" : "10 mins",
                            "value" : 624
                         },
                         "end_location" : {
                            "lat" : 28.4041209,
                            "lng" : 77.3116127
                         },
                         "html_instructions" : "Keep \u003cb\u003eright\u003c/b\u003e to stay on \u003cb\u003eNH19\u003c/b\u003e/\u003cb\u003eNH44\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Bank Of India (on the left in 2.4&nbsp;km)\u003c/div\u003e",
                         "maneuver" : "keep-right",
                         "polyline" : {
                            "points" : "w{wlDeyyvM|HU`DMrAEnQu@jASVCXEpGUfDM`BGl@ED?z@CrAGpAEdBI^AfF[~DSvLa@xJ_@lFSvEQlHQlIa@zHUnIa@~FWbBItBGfDIbDMrAP|DMnCMxBEl@?`BGtBMhAG`H[d@AnEQZAxBGbBGfFSxBIpBI`GSfEOlCIVATAh@Ch@AzFWtQu@rHWd@C~J_@`FQl@A`@Ct@AnAE`@Cr@EhCI^A\\Al@El@Ch@ApCOxBI\\AnDOvBG|BIdESzGU|DMvAG`@CdDOnAE~CIf@ElAClDM"
                         },
                         "start_location" : {
                            "lat" : 28.4717998,
                            "lng" : 77.30594619999999
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "11 m",
                            "value" : 11
                         },
                         "duration" : {
                            "text" : "1 min",
                            "value" : 4
                         },
                         "end_location" : {
                            "lat" : 28.404129,
                            "lng" : 77.3117281
                         },
                         "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eGovernment College Rd\u003c/b\u003e",
                         "maneuver" : "turn-left",
                         "polyline" : {
                            "points" : "wtjlDq|zvMAW"
                         },
                         "start_location" : {
                            "lat" : 28.4041209,
                            "lng" : 77.3116127
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "0.3 km",
                            "value" : 274
                         },
                         "duration" : {
                            "text" : "1 min",
                            "value" : 31
                         },
                         "end_location" : {
                            "lat" : 28.4016677,
                            "lng" : 77.31193089999999
                         },
                         "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e toward \u003cb\u003eLal Bahadur Shastri Marg\u003c/b\u003e",
                         "maneuver" : "turn-right",
                         "polyline" : {
                            "points" : "ytjlDi}zvMz@CzBMz@Ah@ClEO"
                         },
                         "start_location" : {
                            "lat" : 28.404129,
                            "lng" : 77.3117281
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "0.7 km",
                            "value" : 677
                         },
                         "duration" : {
                            "text" : "2 mins",
                            "value" : 100
                         },
                         "end_location" : {
                            "lat" : 28.4018048,
                            "lng" : 77.318851
                         },
                         "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eLal Bahadur Shastri Marg\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Sun Flag Hospital &amp; Research Centre (on the left in 650&nbsp;m)\u003c/div\u003e",
                         "maneuver" : "turn-left",
                         "polyline" : {
                            "points" : "mejlDq~zvMA]?_A?sD?gD?yC?_F?q@EiDIwCCcBC{A"
                         },
                         "start_location" : {
                            "lat" : 28.4016677,
                            "lng" : 77.31193089999999
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "0.9 km",
                            "value" : 936
                         },
                         "duration" : {
                            "text" : "2 mins",
                            "value" : 126
                         },
                         "end_location" : {
                            "lat" : 28.3934097,
                            "lng" : 77.3195229
                         },
                         "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e at \u003cb\u003eSector 16 Rd\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Gymkhana Club - I (on the right)\u003c/div\u003e",
                         "maneuver" : "turn-right",
                         "polyline" : {
                            "points" : "gfjlDyi|vMnAGrNg@nBGtBInIUlDKdJa@"
                         },
                         "start_location" : {
                            "lat" : 28.4018048,
                            "lng" : 77.318851
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "0.2 km",
                            "value" : 193
                         },
                         "duration" : {
                            "text" : "1 min",
                            "value" : 41
                         },
                         "end_location" : {
                            "lat" : 28.3932808,
                            "lng" : 77.3175523
                         },
                         "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e",
                         "maneuver" : "turn-right",
                         "polyline" : {
                            "points" : "yqhlD_n|vMPrE@nADdB"
                         },
                         "start_location" : {
                            "lat" : 28.3934097,
                            "lng" : 77.3195229
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "37 m",
                            "value" : 37
                         },
                         "duration" : {
                            "text" : "1 min",
                            "value" : 12
                         },
                         "end_location" : {
                            "lat" : 28.3936165,
                            "lng" : 77.31753990000001
                         },
                         "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDestination will be on the left\u003c/div\u003e",
                         "maneuver" : "turn-right",
                         "polyline" : {
                            "points" : "_qhlDua|vMcA@"
                         },
                         "start_location" : {
                            "lat" : 28.3932808,
                            "lng" : 77.3175523
                         },
                         "travel_mode" : "DRIVING"
                      }
                   ],
                   "traffic_speed_entry" : [],
                   "via_waypoint" : []
                }
             ],
             "overview_polyline" : {
                "points" : "wuxlDkk{vMY?ApB?fCEjIF|BGTiAvAY`@IDmIfCaKzCyBp@HnEBxHhA?nAAtCM~Ia@XMpD{@`AEh@?hBNdAB|ECbA?fBG~@B^NrCKrIWtFSnQu@jASp@IxLc@pEQjHYfLo@pXaAdMe@lHQlIa@zHUnIa@bJa@|GQbDMrAPlI[fDEvEUjJc@jK]fd@_BdJ_@h[mAdLc@fJYnHYlNk@jRu@rQo@tFUfEOzFQAWz@CvDOvFSAyL?yJE{EM{FC{AnAGbRo@dM_@lDKdJa@RbHDdBcA@"
             },
             "summary" : "NH19/NH44",
             "warnings" : [],
             "waypoint_order" : []
          }
       ],
       "status" : "OK"
    }

Handle

import android.app.Dialog;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    private Dialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        callApiArrayInsideArray();
    }


    private void callApiArrayInsideArray() {
        JSONObject jsonObject = new JSONObject();

        // TODO Send parameter using this in volley apis
        /*try {
            jsonObject.put("UserId", "abc");
        } catch (JSONException e) {
            e.printStackTrace();
        }*/
//        String url = Constants.GetData;
        String url = "http://maps.googleapis.com/maps/api/directions/json?origin=28.4759618,77.3140897&destination=28.3936072,77.3172154&sensor=false&mode=driving&alternatives=true";

        mDialog = ProgressDialog.show(MainActivity.this, "", "Please wait", true);
        mDialog.setCancelable(false);


        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url,
                jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.i("tag", "Volley response: " + response.toString());
                if (mDialog != null) {
                    mDialog.dismiss();
                }
                // TODO when Success Response  then set parse data in list view
                parseResponse(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (mDialog != null) {
                    mDialog.dismiss();
                }
                Log.i("tag", "Volley error: " + error.getMessage());
            }
        });
        Volley.newRequestQueue(this).add(jsonObjectRequest);
    }

    private void parseResponse(JSONObject response) {
        try {
            JSONArray jsonArray = response.getJSONArray("routes");

            Log.e("MainActivity", "jsonArray --> " + jsonArray.toString(2));
            if (jsonArray != null)
            {
                for (int i = 0; i < jsonArray.length(); i++)
                {
                    JSONObject jsonnew = jsonArray.getJSONObject(i);
                    JSONArray jsonarrayROW = jsonnew.getJSONArray("legs");

                    for (int j = 0; j < jsonarrayROW.length(); j++)
                    {
                        JSONObject jsonnewtwo = jsonarrayROW.getJSONObject(j);
                        JSONArray jsonarrayCOL = jsonnewtwo.getJSONArray("steps");

                        for (int k = 0; k < jsonarrayCOL.length(); k++)
                        {
                            JSONObject jsonnewthree = jsonarrayCOL.getJSONObject(k);
                            String str_data = jsonnewthree.getString("html_instructions");
                            Log.e("Keshav", "html_instructions -> " + str_data);
                        }
                    }
                    Log.e("Keshav", "GeoEvent List->");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

2 Comments

Add Dependency in build.gradle file
compile 'com.android.volley:volley:1.0.0'

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.