0

I'm new to android developing and I'm learning parsing json data and I'm stuck at parsing multidimensional json data. this is the json data I want to parse.

{
   "responseCode": 200,
   "message": "Category List",
   "category_data": [
       {
           "id": "24",
           "slug": "product",
           "category_code": "PC",
           "category_image": "Category_1576578421lh5d.png",
           "status": "1",
           "created_at": "2020-01-10 01:23:22",
           "category_name": "ACCESORIES",
           "subcategory": [
               {
                   "id": "8",
                   "category_id": "24",
                   "slug": "belt",
                   "subcategory_code": "BELT",
                   "status": "1",
                   "created_at": "2019-12-17 08:47:27",
                   "subcategory_name": "BELT"
               },
               {
                   "id": "9",
                   "category_id": "24",
                   "slug": "bags",
                   "subcategory_code": "BAGS",
                   "status": "1",
                   "created_at": "2019-12-17 08:47:15",
                   "subcategory_name": "BAGS"
               },
               {
                   "id": "10",
                   "category_id": "24",
                   "slug": "hat",
                   "subcategory_code": "HAT",
                   "status": "1",
                   "created_at": "2019-12-17 08:46:28",
                   "subcategory_name": "HAT"
               },
               {
                   "id": "16",
                   "category_id": "24",
                   "slug": "underwear",
                   "subcategory_code": "UW",
                   "status": "1",
                   "created_at": "2019-12-17 08:47:47",
                   "subcategory_name": "UNDERWEAR"
               },
               {
                   "id": "17",
                   "category_id": "24",
                   "slug": "undershirt",
                   "subcategory_code": "US",
                   "status": "1",
                   "created_at": "2019-12-17 08:47:58",
                   "subcategory_name": "UNDERSHIRT"
               },
               {
                   "id": "18",
                   "category_id": "24",
                   "slug": "boxer",
                   "subcategory_code": "BX",
                   "status": "1",
                   "created_at": "2019-12-17 08:48:31",
                   "subcategory_name": "BOXER"
               },
               {
                   "id": "19",
                   "category_id": "24",
                   "slug": "shocks",
                   "subcategory_code": "SC",
                   "status": "1",
                   "created_at": "2019-12-17 08:48:40",
                   "subcategory_name": "SHOCKS"
               },
               {
                   "id": "20",
                   "category_id": "24",
                   "slug": "slipper",
                   "subcategory_code": "SLP",
                   "status": "1",
                   "created_at": "2019-12-17 08:49:08",
                   "subcategory_name": "SLIPPER"
               },
               {
                   "id": "21",
                   "category_id": "24",
                   "slug": "slip-on",
                   "subcategory_code": "SO",
                   "status": "1",
                   "created_at": "2019-12-17 08:48:48",
                   "subcategory_name": "SLIP ON"
               },
               {
                   "id": "22",
                   "category_id": "24",
                   "slug": "shoes",
                   "subcategory_code": "SH",
                   "status": "1",
                   "created_at": "2019-12-17 01:13:05",
                   "subcategory_name": "SHOES"
               },
               {
                   "id": "31",
                   "category_id": "24",
                   "slug": "updates",
                   "subcategory_code": "FU",
                   "status": "1",
                   "created_at": "2020-01-10 01:42:08",
                   "subcategory_name": "TERBARU"
               }
           ]
       },

please help me parsing this.

saeed foroughi
  • 1,662
  • 1
  • 13
  • 25
yogendra
  • 1
  • 1

3 Answers3

0

You can parse this JSON using gson parser easily.

Steps: 1. Create POJO class for sub_category data like this:-

 class SubCategory {

    @SerializedName("id") 
    String id;

    @SerializedName("category_id") 
    String categoryId;

    @SerializedName("subcategory_code") 
    String subcategoryCode;

//.... Declare all var here as like above ...//

    }
  1. Create another POJO for category_data like this:-

    class Category {

        @SerializedName("id") 
        String id;
    
        @SerializedName("category_code") 
        String categoryCode;
    
        @SerializedName("subcategory") 
        ArrayList<SubCategory> subcategories;
    

    //.... Declare all other var here as like above ...//

    }

  2. Last POJO for parent response (if you need) like this:-

    class ApiResponse {

    @SerializedName("responseCode") 
    int responseCode;
    
    @SerializedName("category_data") 
    ArrayList<Category> categories;
    
    }
    

Here you can find more tutorial for gson parsing:- https://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

0

Step 1: As you are new in Android you have to understand a few basics first like JSON object and JSON array-

@Override
public void onResponse(final JSONObject response) 
{
JSONObject obj = response;
        try {
            obj.getString("responseCode");
            obj.getString("message");

            //category array
            JSONArray array = new JSONArray();
            array = obj.getJSONArray("category_data");
            for(int i=0; i<array.length(); i++){
                //category data single object
                JSONObject object = array.getJSONObject(i);
                String id = object.getString("id");
                String slug = object.getString("slug");
                String category_code = object.getString("category_code");
                String category_image = object.getString("category_image");

                //sub category array
                JSONArray subCatArray = new JSONArray();
                subCatArray = obj.getJSON`enter code here`Array("subcategory");
                for(int j=0; i<subCatArray.length(); i++){
                    //sub category data single object
                    JSONObject subObject = array.getJSONObject(i);
                    String id2 = subObject.getString("id");
                    String slug2 = subObject.getString("slug");
                    String subcategory_code = subObject.getString("subcategory_code");
                    String subcategory_name = subObject.getString("subcategory_name");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
}

Step 2: You can use Array lists or Model classes to store parsed data Using GSon and Retrofit library

0

Your JSON data is wrong check your json data in JSON viewer

{
   "responseCode": 200,
   "message": "Category List",
   "category_data": [

         {"id": "24",
   "slug": "product",
   "category_code": "PC",
   "category_image": "Category_1576578421lh5d.png",
   "status": "1",
   "created_at": "2020-01-10 01:23:22",
   "category_name": "ACCESORIES",
   "subcategory": [
     {
       "id": "8",
       "category_id": "24",
       "slug": "belt",
       "subcategory_code": "BELT",
       "status": "1",
       "created_at": "2019-12-17 08:47:27",
       "subcategory_name": "BELT"
     },
     {
       "id": "9",
       "category_id": "24",
       "slug": "bags",
       "subcategory_code": "BAGS",
       "status": "1",
       "created_at": "2019-12-17 08:47:15",
       "subcategory_name": "BAGS"
     },
     {
       "id": "10",
       "category_id": "24",
       "slug": "hat",
       "subcategory_code": "HAT",
       "status": "1",
       "created_at": "2019-12-17 08:46:28",
       "subcategory_name": "HAT"
     },
     {
       "id": "16",
       "category_id": "24",
       "slug": "underwear",
       "subcategory_code": "UW",
       "status": "1",
       "created_at": "2019-12-17 08:47:47",
       "subcategory_name": "UNDERWEAR"
     },
     {
       "id": "17",
       "category_id": "24",
       "slug": "undershirt",
       "subcategory_code": "US",
       "status": "1",
       "created_at": "2019-12-17 08:47:58",
       "subcategory_name": "UNDERSHIRT"
     },
     {
       "id": "18",
       "category_id": "24",
       "slug": "boxer",
       "subcategory_code": "BX",
       "status": "1",
       "created_at": "2019-12-17 08:48:31",
       "subcategory_name": "BOXER"
     },
     {
       "id": "19",
       "category_id": "24",
       "slug": "shocks",
       "subcategory_code": "SC",
       "status": "1",
       "created_at": "2019-12-17 08:48:40",
       "subcategory_name": "SHOCKS"
     },
     {
       "id": "20",
       "category_id": "24",
       "slug": "slipper",
       "subcategory_code": "SLP",
       "status": "1",
       "created_at": "2019-12-17 08:49:08",
       "subcategory_name": "SLIPPER"
     },
     {
       "id": "21",
       "category_id": "24",
       "slug": "slip-on",
       "subcategory_code": "SO",
       "status": "1",
       "created_at": "2019-12-17 08:48:48",
       "subcategory_name": "SLIP ON"
     },
     {
       "id": "22",
       "category_id": "24",
       "slug": "shoes",
       "subcategory_code": "SH",
       "status": "1",
       "created_at": "2019-12-17 01:13:05",
       "subcategory_name": "SHOES"
     },
     {
       "id": "31",
       "category_id": "24",
       "slug": "updates",
       "subcategory_code": "FU",
       "status": "1",
       "created_at": "2020-01-10 01:42:08",
       "subcategory_name": "TERBARU"
     }]}]}

Use Retrofit library you can easily parse your data. Create POJO class with Source type JSON

Praveen
  • 430
  • 3
  • 11