0

Here, i am getting all the child object of innings(parent) object using names() method and store into JSONArray but everytime it's changing the order.

Here, is my ServiceHandler.java

package com.domore.jsonpardingdemo;

import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by MY WORLD on 12/23/2015.
 */
public class ServiceHandler {
    static String response=null;
    public final static int GET = 1;
    public final static int POST = 2;

    public ServiceHandler() {

    }

    /**
     * Making service call
     * @url - url to make request
     * @method - http request method
     * */
    public String makeServiceCall(String url, int method) {
        return this.makeServiceCall(url, method, null);
    }
    public String makeServiceCall(String url, int method,
                                  List<NameValuePair> params) {
        Log.e("URL111",url);
        try {
            // http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            // Checking http request method type
            if (method == POST) {
                HttpPost httpPost = new HttpPost(url);
                Log.e("INSIDE POST","DONE");
                // adding post params
                if (params != null) {
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }

                httpResponse = httpClient.execute(httpPost);

            } else if (method == GET) {
                // appending params to url
                Log.e("INSIDE GET","DONE");
                if (params != null) {
                    String paramString = URLEncodedUtils
                            .format(params, "utf-8");
                    url += "?" + paramString;
                    Log.e("URL",url);
                }
                HttpGet httpGet = new HttpGet(url);

                httpResponse = httpClient.execute(httpGet);

            }
            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);
            Log.e("RESPONSE",""+response);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;

    }

}

Here, is my MainActivity.java

package com.domore.jsonpardingdemo;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends ListActivity {

    private ProgressDialog pDialog;

    // URL to get contacts JSON
    private static String url = "http://10.0.2.2/JSON/fullLitz.php";

    // JSON Node names
    private static final String TAG_CONTACTS = "contacts";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_ADDRESS = "address";
    private static final String TAG_GENDER = "gender";
    private static final String TAG_PHONE = "phone";
    private static final String TAG_PHONE_MOBILE = "mobile";
    private static final String TAG_PHONE_HOME = "home";
    private static final String TAG_PHONE_OFFICE = "office";

    ArrayList<String> innings_list=null;
    // contacts JSONArray
    JSONArray contacts = null;

    ListView lv;
    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList;
    List<NameValuePair> params;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        contactList = new ArrayList<HashMap<String, String>>();

        //lv=(ListView)findViewById(R.id.list);
        lv = getListView();
        new GetContacts().execute();
    }
    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            params=new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("access_token","ACCESSTOKEN"));
            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.POST);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    JSONObject data=jsonObj.getJSONObject("data");
                    JSONObject card=data.getJSONObject("card");

                    JSONObject innings=card.getJSONObject("innings");
                    JSONArray arr=innings.names();

                    innings_list=new ArrayList<>();//Here is the code to get all innings object and stored into JSONArray.
                    for(int i=0;i<arr.length();i++){
                        innings_list.add(arr.getString(i));
                    }
                    for(String s:innings_list){
                        Log.e("INNINGS",s);
                    }
                }
                catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            if (pDialog.isShowing())
                pDialog.dismiss();

//            ListAdapter adapter = new SimpleAdapter(
//                    MainActivity.this, contactList,
//                    R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL,
//                    TAG_PHONE_MOBILE }, new int[] { R.id.name,
//                    R.id.email, R.id.mobile });
//
//            setListAdapter(adapter);
        }

    }
}

Here, is my json file which is incomplete. I post only what i need.

{
"status": true,
"version": "2.0.3",
"status_code": 200,
"expires": "1427298827.0",
"Etag": "1427287936.0",
"cache_key": "match|iplt20_2013_g30|full_card",
"data": {
"card_type": "full_card",
"card": {
"related_name": "30th Match",
"inactive_balls": [ ],
"msgs": {
"info": "CSK won by 5 wickets.",
"completed": "CSK won by 5 wickets.",
"others": [ ]
},
"batting_order": [
[
"b",
"1"
],
[
"a",
"1"
]
],
"toss": {
"decision": "bat",
"won": "b",
"str": "Rajasthan Royals won the toss and chose to bat first"
},
"winner_team": "a",
"long_description": "CSK won by 5 wickets. Chennai Super Kings scored 186/5 in 19.5 and Rajasthan Royals scored 185/4 in 20.0. Michael Hussey played good and he is man of the match. - Chennai Super Kings vs Rajasthan Royals (International Cricket Match) - 30th Match - IPL 2013 - IPL T20 2013. On 22 April 2013 in MA Chidambaram Stadium, Chepauk, Chennai.",
"innings": {
"b_1": {
"batting_order": [
"s_watson",
"a_rahane",
"d_yagnik",
"r_dravid",
"s_binny",
"b_hodge"
],
"runs": 185,
"balls": 120,
"fall_of_wickets": [
"AM Rahane at 71 runs, in 7.2 over",
"DH Yagnik at 84 runs, in 9.3 over",
"RS Dravid at 113 runs, in 13.2 over",
"SR Watson at 159 runs, in 17.3 over"
],
"wide": 0,
"run_rate": "9.25",
"fours": 12,
"run_str": "185/4 in 20.0",
"wickets": 4,
"wicket_order": [
"a_rahane",
"d_yagnik",
"r_dravid",
"s_watson"
],
"extras": 10,
"bowling_order": [
"a_chandila",
"r_shukla",
"j_faulkner",
"k_cooper",
"s_trivedi",
"s_watson",
"s_binny"
],
"key": "b_1",
"noball": 0,
"sixes": 7,
"legbye": 0,
"bye": 0,
"overs": "20.0",
"dotballs": 30,
"partnerships": [
{
"player_a_six": 3,
"player_a_four": 5,
"first_ball": "bfee9a98-76a9-4959-b1cb-638989c96bff",
"overs_balls": "7.1",
"four": 6,
"index": 1,
"six": 3,
"dismissed": false,
"player_b_four": 1,
"start_over": "0.1",
"end_over": "7.1",
"innings": "1",
"player_b_runs": 16,
"player_b_six": 0,
"player_b_balls": 14,
"runs": 71,
"balls": 43,
"player_b": "a_rahane",
"player_a": "s_watson",
"player_a_balls": 29,
"last_ball": "4825aad2-25d8-434c-86bb-11f8eedbdc4f",
"player_a_runs": 54,
"match": "iplt20_2013_g30",
"team": "b",
"run_rate": "9.91"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "c7351d98-9012-4361-ba2a-f5cd8665587d",
"overs_balls": "2.1",
"four": 1,
"index": 2,
"six": 0,
"dismissed": true,
"player_b_four": 0,
"start_over": "7.2",
"end_over": "9.2",
"innings": "1",
"player_b_runs": 4,
"player_b_six": 0,
"player_b_balls": 5,
"runs": 13,
"balls": 13,
"player_b": "s_watson",
"player_a": "a_rahane",
"player_a_balls": 1,
"last_ball": "c4134225-b057-4a43-9e0f-cd227b377540",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "b",
"run_rate": "6.00"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "17d6fc7f-d37e-45ee-9b0a-aaf15221d8c4",
"overs_balls": "3.5",
"four": 1,
"index": 3,
"six": 1,
"dismissed": true,
"player_b_four": 1,
"start_over": "9.3",
"end_over": "13.1",
"innings": "1",
"player_b_runs": 21,
"player_b_six": 1,
"player_b_balls": 14,
"runs": 29,
"balls": 23,
"player_b": "s_watson",
"player_a": "d_yagnik",
"player_a_balls": 1,
"last_ball": "cf9137bb-9fd7-49c1-9fdc-3e5c8492ee3b",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "b",
"run_rate": "7.57"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "0ea39992-7647-4916-bfcc-b2beba9dbf00",
"overs_balls": "4.1",
"four": 2,
"index": 4,
"six": 2,
"dismissed": true,
"player_b_four": 0,
"start_over": "13.2",
"end_over": "17.2",
"innings": "1",
"player_b_runs": 22,
"player_b_six": 2,
"player_b_balls": 12,
"runs": 46,
"balls": 25,
"player_b": "s_watson",
"player_a": "r_dravid",
"player_a_balls": 1,
"last_ball": "2eaaa026-485f-493c-9a70-551abc2bb83c",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "b",
"run_rate": "11.04"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "4720b7ff-e963-4bc8-9138-6124c2b261fc",
"overs_balls": "2.4",
"four": 2,
"index": 5,
"six": 1,
"dismissed": true,
"player_b_four": 1,
"start_over": "17.3",
"end_over": "19.6",
"innings": "1",
"player_b_runs": 16,
"player_b_six": 1,
"player_b_balls": 9,
"runs": 26,
"balls": 16,
"player_b": "s_binny",
"player_a": "s_watson",
"player_a_balls": 1,
"last_ball": "76481b09-e7e4-4097-9e88-b6884f069082",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "b",
"run_rate": "9.75"
}
]
},
"a_1": {
"batting_order": [
"m_vijay",
"m_hussey",
"s_raina",
"ms_dhoni",
"r_jadeja",
"d_bravo",
"c_morris"
],
"runs": 186,
"balls": 119,
"fall_of_wickets": [
"M Vijay at 22 runs, in 2.3 over",
"SK Raina at 112 runs, in 12.4 over",
"MEK Hussey at 154 runs, in 16.1 over",
"RA Jadeja at 154 runs, in 16.3 over",
"MS Dhoni at 175 runs, in 18.6 over"
],
"wide": 0,
"run_rate": "9.38",
"fours": 18,
"run_str": "186/5 in 19.5",
"wickets": 5,
"wicket_order": [
"m_vijay",
"s_raina",
"m_hussey",
"r_jadeja",
"ms_dhoni"
],
"extras": 7,
"bowling_order": [
"m_sharma",
"j_holder",
"c_morris",
"r_ashwin",
"r_jadeja",
"d_bravo"
],
"key": "a_1",
"noball": 0,
"sixes": 4,
"legbye": 0,
"bye": 0,
"overs": "19.5",
"dotballs": 21,
"partnerships": [
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "1dd3e8b3-b7a6-4417-9b2f-28b3b17353d5",
"overs_balls": "2.2",
"four": 2,
"index": 1,
"six": 1,
"dismissed": false,
"player_b_four": 2,
"start_over": "0.1",
"end_over": "2.2",
"innings": "1",
"player_b_runs": 19,
"player_b_six": 1,
"player_b_balls": 10,
"runs": 22,
"balls": 14,
"player_b": "m_hussey",
"player_a": "m_vijay",
"player_a_balls": 4,
"last_ball": "29fdf8bd-57dd-4ed6-982b-033662310923",
"player_a_runs": 3,
"match": "iplt20_2013_g30",
"team": "a",
"run_rate": "9.43"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "777c4f5e-a00d-4ca8-9827-351305db096e",
"overs_balls": "10.1",
"four": 9,
"index": 2,
"six": 2,
"dismissed": true,
"player_b_four": 5,
"start_over": "2.3",
"end_over": "12.3",
"innings": "1",
"player_b_runs": 36,
"player_b_six": 0,
"player_b_balls": 26,
"runs": 90,
"balls": 61,
"player_b": "m_hussey",
"player_a": "m_vijay",
"player_a_balls": 1,
"last_ball": "e826e804-5b8c-4fd2-8771-d2257348f976",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "a",
"run_rate": "8.85"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "69a04225-ed8a-45ba-96af-a1ab8a5ae89e",
"overs_balls": "3.3",
"four": 6,
"index": 3,
"six": 0,
"dismissed": true,
"player_b_four": 6,
"start_over": "12.4",
"end_over": "15.6",
"innings": "1",
"player_b_runs": 33,
"player_b_six": 0,
"player_b_balls": 15,
"runs": 42,
"balls": 21,
"player_b": "m_hussey",
"player_a": "s_raina",
"player_a_balls": 1,
"last_ball": "bf356a48-5701-4d7a-abb0-025566679a1e",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "a",
"run_rate": "12.00"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "7106a8a0-021e-4049-807d-76debe55ac51",
"overs_balls": "1.2",
"four": 0,
"index": 4,
"six": 0,
"dismissed": true,
"player_b_four": 0,
"start_over": "16.1",
"end_over": "16.2",
"innings": "1",
"player_b_runs": 0,
"player_b_six": 0,
"player_b_balls": 0,
"runs": 0,
"balls": 2,
"player_b": "m_hussey",
"player_a": "ms_dhoni",
"player_a_balls": 1,
"last_ball": "5ab8afc4-a9eb-4b86-8a4b-41cf8a69af7e",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "a",
"run_rate": "0.00"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "c88b4bba-adf7-4de1-bf86-cc7b5d5e69ed",
"overs_balls": "2.3",
"four": 1,
"index": 5,
"six": 0,
"dismissed": true,
"player_b_four": 1,
"start_over": "16.3",
"end_over": "18.5",
"innings": "1",
"player_b_runs": 15,
"player_b_six": 0,
"player_b_balls": 9,
"runs": 21,
"balls": 15,
"player_b": "ms_dhoni",
"player_a": "r_jadeja",
"player_a_balls": 1,
"last_ball": "8456c578-31e0-41fe-a3ef-aef99dca68f3",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "a",
"run_rate": "8.40"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "81248550-5945-4b4d-80be-bf7fbec1b607",
"overs_balls": "1.0",
"four": 0,
"index": 6,
"six": 1,
"dismissed": true,
"player_b_four": 0,
"start_over": "18.6",
"end_over": "19.5",
"innings": "1",
"player_b_runs": 10,
"player_b_six": 1,
"player_b_balls": 4,
"runs": 11,
"balls": 6,
"player_b": "d_bravo",
"player_a": "ms_dhoni",
"player_a_balls": 1,
"last_ball": "161e36fd-0821-4697-affb-c99e0a45a66e",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "a",
"run_rate": "11.00"
}
]
},
"b_2": {
"batting_order": [
"s_watson",
"a_rahane",
"d_yagnik",
"r_dravid",
"s_binny",
"b_hodge"
],
"runs": 185,
"balls": 120,
"fall_of_wickets": [
"AM Rahane at 71 runs, in 7.2 over",
"DH Yagnik at 84 runs, in 9.3 over",
"RS Dravid at 113 runs, in 13.2 over",
"SR Watson at 159 runs, in 17.3 over"
],
"wide": 0,
"run_rate": "9.25",
"fours": 12,
"run_str": "185/4 in 20.0",
"wickets": 4,
"wicket_order": [
"a_rahane",
"d_yagnik",
"r_dravid",
"s_watson"
],
"extras": 10,
"bowling_order": [
"a_chandila",
"r_shukla",
"j_faulkner",
"k_cooper",
"s_trivedi",
"s_watson",
"s_binny"
],
"key": "b_1",
"noball": 0,
"sixes": 7,
"legbye": 0,
"bye": 0,
"overs": "20.0",
"dotballs": 30,
"partnerships": [
{
"player_a_six": 3,
"player_a_four": 5,
"first_ball": "bfee9a98-76a9-4959-b1cb-638989c96bff",
"overs_balls": "7.1",
"four": 6,
"index": 1,
"six": 3,
"dismissed": false,
"player_b_four": 1,
"start_over": "0.1",
"end_over": "7.1",
"innings": "1",
"player_b_runs": 16,
"player_b_six": 0,
"player_b_balls": 14,
"runs": 71,
"balls": 43,
"player_b": "a_rahane",
"player_a": "s_watson",
"player_a_balls": 29,
"last_ball": "4825aad2-25d8-434c-86bb-11f8eedbdc4f",
"player_a_runs": 54,
"match": "iplt20_2013_g30",
"team": "b",
"run_rate": "9.91"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "c7351d98-9012-4361-ba2a-f5cd8665587d",
"overs_balls": "2.1",
"four": 1,
"index": 2,
"six": 0,
"dismissed": true,
"player_b_four": 0,
"start_over": "7.2",
"end_over": "9.2",
"innings": "1",
"player_b_runs": 4,
"player_b_six": 0,
"player_b_balls": 5,
"runs": 13,
"balls": 13,
"player_b": "s_watson",
"player_a": "a_rahane",
"player_a_balls": 1,
"last_ball": "c4134225-b057-4a43-9e0f-cd227b377540",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "b",
"run_rate": "6.00"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "17d6fc7f-d37e-45ee-9b0a-aaf15221d8c4",
"overs_balls": "3.5",
"four": 1,
"index": 3,
"six": 1,
"dismissed": true,
"player_b_four": 1,
"start_over": "9.3",
"end_over": "13.1",
"innings": "1",
"player_b_runs": 21,
"player_b_six": 1,
"player_b_balls": 14,
"runs": 29,
"balls": 23,
"player_b": "s_watson",
"player_a": "d_yagnik",
"player_a_balls": 1,
"last_ball": "cf9137bb-9fd7-49c1-9fdc-3e5c8492ee3b",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "b",
"run_rate": "7.57"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "0ea39992-7647-4916-bfcc-b2beba9dbf00",
"overs_balls": "4.1",
"four": 2,
"index": 4,
"six": 2,
"dismissed": true,
"player_b_four": 0,
"start_over": "13.2",
"end_over": "17.2",
"innings": "1",
"player_b_runs": 22,
"player_b_six": 2,
"player_b_balls": 12,
"runs": 46,
"balls": 25,
"player_b": "s_watson",
"player_a": "r_dravid",
"player_a_balls": 1,
"last_ball": "2eaaa026-485f-493c-9a70-551abc2bb83c",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "b",
"run_rate": "11.04"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "4720b7ff-e963-4bc8-9138-6124c2b261fc",
"overs_balls": "2.4",
"four": 2,
"index": 5,
"six": 1,
"dismissed": true,
"player_b_four": 1,
"start_over": "17.3",
"end_over": "19.6",
"innings": "1",
"player_b_runs": 16,
"player_b_six": 1,
"player_b_balls": 9,
"runs": 26,
"balls": 16,
"player_b": "s_binny",
"player_a": "s_watson",
"player_a_balls": 1,
"last_ball": "76481b09-e7e4-4097-9e88-b6884f069082",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "b",
"run_rate": "9.75"
}
]
}
},
"title": "Chennai Super Kings vs Rajasthan Royals - 30th Match - IPL T20 2013",
"dl_applied": false,
"start_date": {
"timestamp": 1366641000,
"iso": "2013-04-22T14:30+00:00",
"str": "22nd Apr 2013 14:30 GMT"
},

Above is the json file where i need to get all the child object of innings(parent) object. I want to get all the child object in same order.

Please, help me to solve this problem.

danizmax
  • 2,446
  • 2
  • 32
  • 41
Milan Gajera
  • 962
  • 2
  • 14
  • 41

4 Answers4

0

JSON doesn't maintain any sequesnce. What you could do is add position of the json object when each object is inserted in the array. May be something like this:

"batting_order": [
{"name":"s_watson","position":0},
{"name":"a_rahane","position":1},
{"name":"d_yagnik","position":2},
{"name":"r_dravid","position":3},
{"name":"s_binny","position":4},
{"name":"b_hodge","position":5}
]
Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
0

Why are you facing this issue? Is the order of elements in a JSON list maintained? JSON order mixed up

How to resolve it:

  • Use indexes for the {} objects and sort them according to indexes.
  • Use [] where the array object is required and {} for objects

Check with these inputs:

    "innings": [ 
       {
          "name":"b_1",
          "batting_order": [...]
         ....
       },
       {
         "name":"a_1",
         "batting_order": [...]
         ....
       }
    ]
Community
  • 1
  • 1
thepace
  • 2,221
  • 1
  • 13
  • 21
0

Try this:

 ArrayList<String> innings_list= new ArrayList<String>();
    try {
        JSONObject jsonObj = new JSONObject("");

        JSONObject data = jsonObj.getJSONObject("data");
        JSONObject card = data.getJSONObject("card");

        JSONObject innings = card.getJSONObject("innings");

        Iterator<String> iter = innings.keys();
        while (iter.hasNext()) {
            String key = iter.next();
            try {
                Object value = innings.get(key);
                if (value instanceof Objects) {
                    innings_list.add(value.toString());
                }

            } catch (JSONException e) {
                // Something went wrong!
            }
        }


    } catch (JSONException e) {
        e.printStackTrace();
    }
Quang Doan
  • 632
  • 4
  • 12
0

@Milan ... json's objects (key-value) can't form the order sequence ..

in order to maintain the order sequence use jsonarray instead of jsonobject

i.e. change your original json

{
"innings": {
    "b_1": {
        "batting_order": [
            "s_watson",
            "a_rahane",
            "d_yagnik",
            "r_dravid",
            "s_binny",
            "b_hodge"
        ],
        "runs": 185,
        "balls": 120,
        "fall_of_wickets": [
            "AM Rahane at 71 runs, in 7.2 over",
            "DH Yagnik at 84 runs, in 9.3 over",
            "RS Dravid at 113 runs, in 13.2 over",
            "SR Watson at 159 runs, in 17.3 over"
        ],
        "wide": 0,
        "run_rate": "9.25",
        "fours": 12,
        "run_str": "185/4 in 20.0",
        "wickets": 4,
        "wicket_order": [
            "a_rahane",
            "d_yagnik",
            "r_dravid",
            "s_watson"
        ],
        "extras": 10,
        "bowling_order": [
            "a_chandila",
            "r_shukla",
            "j_faulkner",
            "k_cooper",
            "s_trivedi",
            "s_watson",
            "s_binny"
        ],
        "key": "b_1",
        "noball": 0,
        "sixes": 7,
        "legbye": 0,
        "bye": 0,
        "overs": "20.0",
        "dotballs": 30,
        "partnerships": [
            {
                "player_a_six": 3,
                "player_a_four": 5,
                "first_ball": "bfee9a98-76a9-4959-b1cb-638989c96bff",
                "overs_balls": "7.1",
                "four": 6,
                "index": 1,
                "six": 3,
                "dismissed": false,
                "player_b_four": 1,
                "start_over": "0.1",
                "end_over": "7.1",
                "innings": "1",
                "player_b_runs": 16,
                "player_b_six": 0,
                "player_b_balls": 14,
                "runs": 71,
                "balls": 43,
                "player_b": "a_rahane",
                "player_a": "s_watson",
                "player_a_balls": 29,
                "last_ball": "4825aad2-25d8-434c-86bb-11f8eedbdc4f",
                "player_a_runs": 54,
                "match": "iplt20_2013_g30",
                "team": "b",
                "run_rate": "9.91"
            },
            {
                "player_a_six": 0,
                "player_a_four": 0,
                "first_ball": "c7351d98-9012-4361-ba2a-f5cd8665587d",
                "overs_balls": "2.1",
                "four": 1,
                "index": 2,
                "six": 0,
                "dismissed": true,
                "player_b_four": 0,
                "start_over": "7.2",
                "end_over": "9.2",
                "innings": "1",
                "player_b_runs": 4,
                "player_b_six": 0,
                "player_b_balls": 5,
                "runs": 13,
                "balls": 13,
                "player_b": "s_watson",
                "player_a": "a_rahane",
                "player_a_balls": 1,
                "last_ball": "c4134225-b057-4a43-9e0f-cd227b377540",
                "player_a_runs": 0,
                "match": "iplt20_2013_g30",
                "team": "b",
                "run_rate": "6.00"
            },
            {
                "player_a_six": 0,
                "player_a_four": 0,
                "first_ball": "17d6fc7f-d37e-45ee-9b0a-aaf15221d8c4",
                "overs_balls": "3.5",
                "four": 1,
                "index": 3,
                "six": 1,
                "dismissed": true,
                "player_b_four": 1,
                "start_over": "9.3",
                "end_over": "13.1",
                "innings": "1",
                "player_b_runs": 21,
                "player_b_six": 1,
                "player_b_balls": 14,
                "runs": 29,
                "balls": 23,
                "player_b": "s_watson",
                "player_a": "d_yagnik",
                "player_a_balls": 1,
                "last_ball": "cf9137bb-9fd7-49c1-9fdc-3e5c8492ee3b",
                "player_a_runs": 0,
                "match": "iplt20_2013_g30",
                "team": "b",
                "run_rate": "7.57"
            },
            {
                "player_a_six": 0,
                "player_a_four": 0,
                "first_ball": "0ea39992-7647-4916-bfcc-b2beba9dbf00",
                "overs_balls": "4.1",
                "four": 2,
                "index": 4,
                "six": 2,
                "dismissed": true,
                "player_b_four": 0,
                "start_over": "13.2",
                "end_over": "17.2",
                "innings": "1",
                "player_b_runs": 22,
                "player_b_six": 2,
                "player_b_balls": 12,
                "runs": 46,
                "balls": 25,
                "player_b": "s_watson",
                "player_a": "r_dravid",
                "player_a_balls": 1,
                "last_ball": "2eaaa026-485f-493c-9a70-551abc2bb83c",
                "player_a_runs": 0,
                "match": "iplt20_2013_g30",
                "team": "b",
                "run_rate": "11.04"
            },
            {
                "player_a_six": 0,
                "player_a_four": 0,
                "first_ball": "4720b7ff-e963-4bc8-9138-6124c2b261fc",
                "overs_balls": "2.4",
                "four": 2,
                "index": 5,
                "six": 1,
                "dismissed": true,
                "player_b_four": 1,
                "start_over": "17.3",
                "end_over": "19.6",
                "innings": "1",
                "player_b_runs": 16,
                "player_b_six": 1,
                "player_b_balls": 9,
                "runs": 26,
                "balls": 16,
                "player_b": "s_binny",
                "player_a": "s_watson",
                "player_a_balls": 1,
                "last_ball": "76481b09-e7e4-4097-9e88-b6884f069082",
                "player_a_runs": 0,
                "match": "iplt20_2013_g30",
                "team": "b",
                "run_rate": "9.75"
            }
        ]
    },
    "a_1": {
        // ....
    },
    "b_2": {
        // ....
    }
}

}

to this json structure

{
"innings": [ {
        "batting_order": [
            "s_watson",
            "a_rahane",
            "d_yagnik",
            "r_dravid",
            "s_binny",
            "b_hodge"
        ],
        "runs": 185,
        "balls": 120,
        "fall_of_wickets": [
            "AM Rahane at 71 runs, in 7.2 over",
            "DH Yagnik at 84 runs, in 9.3 over",
            "RS Dravid at 113 runs, in 13.2 over",
            "SR Watson at 159 runs, in 17.3 over"
        ],
        "wide": 0,
        "run_rate": "9.25",
        "fours": 12,
        "run_str": "185/4 in 20.0",
        "wickets": 4,
        "wicket_order": [
            "a_rahane",
            "d_yagnik",
            "r_dravid",
            "s_watson"
        ],
        "extras": 10,
        "bowling_order": [
            "a_chandila",
            "r_shukla",
            "j_faulkner",
            "k_cooper",
            "s_trivedi",
            "s_watson",
            "s_binny"
        ],
        "key": "b_1",
        "noball": 0,
        "sixes": 7,
        "legbye": 0,
        "bye": 0,
        "overs": "20.0",
        "dotballs": 30,
        "partnerships": [
            {
                "player_a_six": 3,
                "player_a_four": 5,
                "first_ball": "bfee9a98-76a9-4959-b1cb-638989c96bff",
                "overs_balls": "7.1",
                "four": 6,
                "index": 1,
                "six": 3,
                "dismissed": false,
                "player_b_four": 1,
                "start_over": "0.1",
                "end_over": "7.1",
                "innings": "1",
                "player_b_runs": 16,
                "player_b_six": 0,
                "player_b_balls": 14,
                "runs": 71,
                "balls": 43,
                "player_b": "a_rahane",
                "player_a": "s_watson",
                "player_a_balls": 29,
                "last_ball": "4825aad2-25d8-434c-86bb-11f8eedbdc4f",
                "player_a_runs": 54,
                "match": "iplt20_2013_g30",
                "team": "b",
                "run_rate": "9.91"
            },
            {
                "player_a_six": 0,
                "player_a_four": 0,
                "first_ball": "c7351d98-9012-4361-ba2a-f5cd8665587d",
                "overs_balls": "2.1",
                "four": 1,
                "index": 2,
                "six": 0,
                "dismissed": true,
                "player_b_four": 0,
                "start_over": "7.2",
                "end_over": "9.2",
                "innings": "1",
                "player_b_runs": 4,
                "player_b_six": 0,
                "player_b_balls": 5,
                "runs": 13,
                "balls": 13,
                "player_b": "s_watson",
                "player_a": "a_rahane",
                "player_a_balls": 1,
                "last_ball": "c4134225-b057-4a43-9e0f-cd227b377540",
                "player_a_runs": 0,
                "match": "iplt20_2013_g30",
                "team": "b",
                "run_rate": "6.00"
            },
            {
                "player_a_six": 0,
                "player_a_four": 0,
                "first_ball": "17d6fc7f-d37e-45ee-9b0a-aaf15221d8c4",
                "overs_balls": "3.5",
                "four": 1,
                "index": 3,
                "six": 1,
                "dismissed": true,
                "player_b_four": 1,
                "start_over": "9.3",
                "end_over": "13.1",
                "innings": "1",
                "player_b_runs": 21,
                "player_b_six": 1,
                "player_b_balls": 14,
                "runs": 29,
                "balls": 23,
                "player_b": "s_watson",
                "player_a": "d_yagnik",
                "player_a_balls": 1,
                "last_ball": "cf9137bb-9fd7-49c1-9fdc-3e5c8492ee3b",
                "player_a_runs": 0,
                "match": "iplt20_2013_g30",
                "team": "b",
                "run_rate": "7.57"
            },
            {
                "player_a_six": 0,
                "player_a_four": 0,
                "first_ball": "0ea39992-7647-4916-bfcc-b2beba9dbf00",
                "overs_balls": "4.1",
                "four": 2,
                "index": 4,
                "six": 2,
                "dismissed": true,
                "player_b_four": 0,
                "start_over": "13.2",
                "end_over": "17.2",
                "innings": "1",
                "player_b_runs": 22,
                "player_b_six": 2,
                "player_b_balls": 12,
                "runs": 46,
                "balls": 25,
                "player_b": "s_watson",
                "player_a": "r_dravid",
                "player_a_balls": 1,
                "last_ball": "2eaaa026-485f-493c-9a70-551abc2bb83c",
                "player_a_runs": 0,
                "match": "iplt20_2013_g30",
                "team": "b",
                "run_rate": "11.04"
            },
            {
                "player_a_six": 0,
                "player_a_four": 0,
                "first_ball": "4720b7ff-e963-4bc8-9138-6124c2b261fc",
                "overs_balls": "2.4",
                "four": 2,
                "index": 5,
                "six": 1,
                "dismissed": true,
                "player_b_four": 1,
                "start_over": "17.3",
                "end_over": "19.6",
                "innings": "1",
                "player_b_runs": 16,
                "player_b_six": 1,
                "player_b_balls": 9,
                "runs": 26,
                "balls": 16,
                "player_b": "s_binny",
                "player_a": "s_watson",
                "player_a_balls": 1,
                "last_ball": "76481b09-e7e4-4097-9e88-b6884f069082",
                "player_a_runs": 0,
                "match": "iplt20_2013_g30",
                "team": "b",
                "run_rate": "9.75"
            }
        ]
    }
]

}

Note - i've replace your json with structure

{"innings"  : {"b_1" : {jsonobject}, "a_1" : {jsonobject}, "b_2" : {jsonobject}}}

to

{"innings"  : [{jsonobject}, [{jsonobject},[{jsonobject}]}

this jsonarray of "innings" doesn't change the sequence

it's all server side implementation

at your side (java code)

if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                JSONObject data=jsonObj.getJSONObject("data");
                JSONObject card=data.getJSONObject("card");

                JSONArray innings=card.getJSONArray("innings");
                for(int i=0;i<innings.length();i++){
                    //innings_list.add(arr.getString(i));
                    JSONObject inningJsonObj=innings.get(i);
                }
                for(String s:innings_list){
                    Log.e("INNINGS",s);
                }
            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }
Angad Tiwari
  • 1,738
  • 1
  • 12
  • 23