Brendon
Brendon

Reputation: 1368

JSON Array Splitter in Android

I am working in an Android project which involves JSON parsing into a list view.

JSON file contains one Array name with 10 sets of JSON object fields,

I have a requirement to split the first 5 sets in one activity page and another remaining 5 sets to the next activity page which should be activated when I click a button or anything to navigate.

Intent is helpful to navigate, but to split the json is complicated for me.

JSON Code

{
    "Productcategory": [
        {
            "shop_cat_id": "1",
            "shop_cat_name": "kurtis",
            "shop_scat_id1": "1",
            "shop_scat_id1name": "cotton kurtis",
            "shop_scat_id2": "2",
            "shop_scat_id2name": "Cotton Designer Kurtis",
            "shop_scat_id3": "3",
            "shop_scat_id3name": "soft Designer kurtis"
        },
        {
            "shop_cat_id": "2",
            "shop_cat_name": "Sarees",
            "shop_scat_id1": "1",
            "shop_scat_id1name": "Saree 1",
            "shop_scat_id2": "2",
            "shop_scat_id2name": "Saree 2",
            "shop_scat_id3": "3",
            "shop_scat_id3name": "Saree 4",
            "shop_scat_id4": "4"
        },
        {
            "shop_cat_id": "3",
            "shop_cat_name": "Anarkkali suits",
            "shop_scat_id1": "1",
            "shop_scat_id1name": "Readymade",
            "shop_scat_id2": "2",
            "shop_scat_id2name": "Stitched",
            "shop_scat_id3": "3",
            "shop_scat_id3name": "Unstitched"
        },
        {
            "shop_cat_id": "4",
            "shop_cat_name": "CottonLeggins",
            "shop_scat_id1": "1",
            "shop_scat_id1name": "LSize",
            "shop_scat_id2": "2",
            "shop_scat_id2name": "XLsize",
            "shop_scat_id3": "3",
            "shop_scat_id3name": "3XLsize",
            "shop_scat_id4": "4"
        },
        {
            "shop_cat_id": "5",
            "shop_cat_name": "PattialaPantsset"
        },
        {
            "shop_cat_id": "6",
            "shop_cat_name": "Kids'AnarkkaliSuits"
        },
        {
            "shop_cat_id": "7",
            "shop_cat_name": "Kids'AnarkkaliSuits"
        },
        {
            "shop_cat_id": "8",
            "shop_cat_name": "Kids'AnarkkaliSuits"
        },
         {
            "shop_cat_id": "9",
            "shop_cat_name": "Kids'AnarkkaliSuits"
        },
        {
            "shop_cat_id": "10",
            "shop_cat_name": "Kids'AnarkkaliSuits"
        }
    ]
}

Upvotes: 2

Views: 939

Answers (2)

BSKANIA
BSKANIA

Reputation: 1315

Yes you can do that using

JsonArray productarray = jsonobj.getjsonarray("Productcategory");

now get count

productarray.getCount();

and according to pagenumber click getdata array if 1 is clicked then from 0 to 5 and 1 then 5 to 10 and if you want number of page then

number_of_page = productarray.getCount()/5;

Upvotes: 2

Dinithe Pieris
Dinithe Pieris

Reputation: 1992

This is how the get json array details to the array.

JSONArray jsonArrContent = json.getJSONArray("Productcategory");
String [] arr_shop_cat_id;  
String [] arr_shop_cat_name;        
for (int i = 0; i < jsonArrContent.length(); i++) {
    JSONObject jsonobj = jsonArrContent.getJSONObject(i);
    // get data from the json array objetcs
    arr_shop_cat_id[i] = jsonobj.getString("shop_cat_id");
    arr_shop_cat_name[i] = jsonobj.getString("shop_cat_name");
    ...
}

after this you can easily access to the array items as your wish.

Upvotes: 0

Related Questions