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"
        }
    ]
}
1
  • Get the whole data as objects, and work with the objects according to your requirement. For ref of parsing and model class + objects: stackoverflow.com/questions/21480634/… Commented Feb 7, 2014 at 7:11

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

4 Comments

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
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.
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?
then it is splited as 5 + 4.
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.

Comments

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.