2

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"
        }
    ]
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Brendon
  • 1,368
  • 1
  • 12
  • 28
  • Get the whole data as objects, and work with the objects according to your requirement. For ref of parsing and model class + objects: http://stackoverflow.com/questions/21480634/unable-to-loop-through-dynamic-json-string-recursively-in-android/21480997#21480997 – Pararth Feb 07 '14 at 07:11

2 Answers2

2

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;
Nimantha
  • 6,405
  • 6
  • 28
  • 69
BSKANIA
  • 1,317
  • 15
  • 27
  • problem is when i doing wat u said it displays the first five sets in two pages, the second five sets in not displaying. @BSKANIA – Brendon Feb 07 '14 at 07:16
  • check your for loop starting count. otherwise post your code so anyone can help you. Json is not required. may be you are doing some mistake while fetching array. – BSKANIA Feb 07 '14 at 07:21
  • There is no mistakes in coding, since i have doubts regarding the splitting of JSON items, if i have a 10 it will be splitted by 5+5, if i have 9 then how it will be splitted? then how first set and second set is stored? – Brendon Feb 07 '14 at 07:26
  • then it is splited as 5 + 4. – BSKANIA Feb 07 '14 at 08:25
0

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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Dinithe Pieris
  • 1,822
  • 3
  • 32
  • 44