naveed abbas naveed abbas - 3 months ago 12
JSON Question

json data without array name

This is my array which I retrieve from a url


[{"ash_id":"1","asg_code":"1226","ash_name":"hello","ash_cell":"123","ash_nic":"123","ash_long":"34.015","ash_lat":"71.5805","zm_id":null,"created_by":"0","created_date":"0000-00-00
00:00:00","last_updated":"2016-08-29 07:52:35"}]


I have array without array name, I can read data if there is name of jason array but I am unable to do that with this type of array.

any suggestion my code for json with array name


String json = serviceClient.makeServiceCall(URL_ITEMS, ServiceHandler.GET);
// print the json response in the log
Log.d("Get match fixture resps", "> " + json);
if (json != null) {
try {
Log.d("try", "in the try");
JSONObject jsonObj = new JSONObject(json);
Log.d("jsonObject", "new json Object");
// Getting JSON Array node
matchFixture = jsonObj.getJSONArray(TAG_FIXTURE);
Log.d("json aray", "user point array");
int len = matchFixture.length();
Log.d("len", "get array length");
for (int i = 0; i < matchFixture.length(); i++) {
JSONObject c = matchFixture.getJSONObject(i);
Double matchId = Double.parseDouble(c.getString(TAG_MATCHID));
Log.d("matchId", String.valueOf(matchId));
Double teamA = Double.valueOf(c.getString(TAG_TEAMA));
Log.d("teamA", String.valueOf(teamA));
String teamB = c.getString(TAG_TEAMB);
Log.d("teamB", teamB);`

Answer
List<Double> listMatch = new ArrayList<Double>();
List<Double> listA = new ArrayList<Double>();
List<String> listB = new ArrayList<String>();

Create three arrayList and add data in that list

if(json!=null)

    {
        try {
            Log.d("try", "in the try");
            JSONArray jsonArray = new JSONArray(json);

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject c = jsonArray.getJSONObject(i);

                listMatch.add(Double.parseDouble(c.getString(TAG_MATCHID)));
                Log.d("matchId", String.valueOf(matchId));
                listA.add(Double.valueOf(c.getString(TAG_TEAMA));
                Log.d("teamA", String.valueOf(teamA));
                listB.add(c.getString(TAG_TEAMB);
                Log.d("teamB", teamB);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }