+5 votes
by (100 points)
{
  "Catagories": [
    {
      "cat_id": "01",
      "name": "Business",
      "logo": "catagoryLogo1",
      "sub_catagories": [
        {
          "subcat_id": "11",
          "name": "Business11",
          "logo": "logo11",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "[email protected]",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },
        {
          "subcat_id": "12",
          "name": "Business12",
          "logo": "logo12",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "[email protected]",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },
        {
          "subcat_id": "13",
          "name": "Business13",
          "logo": "logo13",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "[email protected]",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },

      ]
    },
    {
      "cat_id": "02",
      "name": "Restorant",
      "logo": "catagoryLogo2",
      "sub_catagories": [
        {
          "subcat_id": "21",
          "name": "Restorant21",
          "logo": "logo21",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "[email protected]",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },
        {
          "subcat_id": "22",
          "name": "Restorant22",
          "logo": "logo22",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "[email protected]",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },
        {
          "subcat_id": "23",
          "name": "Restorant23",
          "logo": "logo23",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "[email protected]",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },

      ]
    },
    {
      "cat_id": "03",
      "name": "Shop",
      "logo": "catagoryLogo3",
      "lon": "72.1500",
      "sub_catagories": [
        {
          "subcat_id": "31",
          "name": "Shop31",
          "logo": "logo31",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "[email protected]",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },
        {
          "subcat_id": "32",
          "name": "Shop32",
          "logo": "logo32",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "[email protected]",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },
        {
          "subcat_id": "33",
          "name": "Shop33",
          "logo": "logo33",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "[email protected]",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },

      ]
    }
  ]
}

Here I have a JSON string. How can I parse and store it in this particular class object?

public static void getDomainData() {

    try {
        JSONObject jsonobj = new JSONObject(Result);
        BusinessAllDataClass AllData = new BusinessAllDataClass();

        if (jsonobj.has(Constants.Cat)) {
            List<CategoryDataClass> Cat_list = new ArrayList<CategoryDataClass>();
            CatData = jsonobj.getJSONArray(Constants.Cat);

            for (int i = 0; i < CatData.length(); i++) {

                CategoryDataClass Cat = new CategoryDataClass();
                JSONObject objCat = CatData.getJSONObject(i);
                if (objCat.has(Constants.Cat_ID)) {
                    Cat.setCatId(objCat.getString(Constants.Cat_ID));
                }
                if (objCat.has(Constants.Cat_NAME)) {
                    Cat.setCatName(objCat.getString(Constants.Cat_NAME));
                }
                if (objCat.has(Constants.Cat_Logo)) {
                    Cat.setCatLogo(objCat.getString(Constants.Cat_Logo));

                }
                if (objCat.has(Constants.Sub_Cat)) {
                    SubCat_list = new ArrayList<SubCategoryDataClass>();
                    SubCatData = objCat.getJSONArray(Constants.Sub_Cat);

                    for (int j = 0; j < 3; j++) {

                        SubCategoryDataClass SubCat = new SubCategoryDataClass();
                        JSONObject objSubCat = SubCatData.getJSONObject(j);
                        if (objSubCat.has(Constants.Sub_CatID)) {
                            SubCat.setSubCatId(objSubCat
                                    .getString(Constants.Sub_CatID));

                        }
                        if (objSubCat.has(Constants.Sub_CatNAME)) {
                            SubCat.setSubCatName(objSubCat
                                    .getString(Constants.Sub_CatNAME));

                        }
                        if (objSubCat.has(Constants.Sub_CatLogo)) {
                            SubCat.setSubCatLogo(objSubCat
                                    .getString(Constants.Sub_CatLogo));

                        }
                        if (objSubCat.has(Constants.Longitude)) {
                            SubCat.setLongitude(objSubCat
                                    .getString(Constants.Longitude));

                        }
                        if (objSubCat.has(Constants.Latitudde)) {
                            SubCat.setLatitiude(objSubCat
                                    .getString(Constants.Latitudde));

                        }
                        if (objSubCat.has(Constants.Description)) {
                            List<SubCategorDescriptionDataClass> SubCatDescription_list = new ArrayList<SubCategorDescriptionDataClass>();
                            JSONObject DescObj = objSubCat
                                    .getJSONObject(Constants.Description);
                            SubCategorDescriptionDataClass SubCatDesc = new SubCategorDescriptionDataClass();
                            if (DescObj.has(Constants.Street)) {
                                SubCatDesc.setStreet(DescObj
                                        .getString(Constants.Street));

                            }
                            if (DescObj.has(Constants.City)) {
                                SubCatDesc.setCity(DescObj
                                        .getString(Constants.City));

                            }
                            if (DescObj.has(Constants.State)) {
                                SubCatDesc.setState(DescObj
                                        .getString(Constants.State));

                            }
                            if (DescObj.has(Constants.Zipcode)) {
                                SubCatDesc.setzipcode(DescObj
                                        .getString(Constants.Zipcode));

                            }
                            if (DescObj.has(Constants.DataDescription)) {
                                SubCatDesc
                                        .setDesc(DescObj
                                                .getString(Constants.DataDescription));

                            }

                            if (DescObj.has(Constants.Services)) {
                                SubCatDesc.setServices(DescObj
                                        .getString(Constants.Services));
                            }
                            if (DescObj.has(Constants.WorkingHour)) {
                                SubCatDesc.setWorkingHour(DescObj
                                        .getString(Constants.WorkingHour));
                            }
                            if (DescObj.has(Constants.Contact_No)) {
                                SubCatDesc.setContactNo(DescObj
                                        .getString(Constants.Contact_No));
                            }
                            if (DescObj.has(Constants.Usre_Email)) {
                                SubCatDesc.setUsreEmail(DescObj
                                        .getString(Constants.Usre_Email));
                            }
                            if (DescObj.has(Constants.Website)) {
                                SubCatDesc.setWebsite(DescObj
                                        .getString(Constants.Website));
                            }
                            if (DescObj.has(Constants.Image)) {
                                DescImage = DescObj
                                        .getJSONArray(Constants.Image);
                                Log.e("JsonArray",
                                        String.valueOf(DescImage.length()));
                                for (int k = 0; k < DescImage.length(); k++) {

                                    SubCatDesc.ImageData.add(DescImage
                                            .getString(k));

                                }
                                SubCatDescription_list.add(SubCatDesc);

                                SubCat.setSubCatDescList(SubCatDescription_list);

                            }
                            SubCat_list.add(SubCat);

                            Cat.setSubCatList(SubCat_list);
                        }
                    }
                }

                Cat_list.add(Cat);

            }
            AllData.setCatList(Cat_list);

        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

11 Answers

+16 votes
by (100 points)

You shoul use gson parser. Gson lib google' s product and very good lib. Only you declare class and json data.

Gson gson = new Gson();
Object result = gson.fromJSON(jsonData, object);

But you declare class fields as same as json data. Such as your class must contains Catagories array.(Same name )

+14 votes
by (100 points)

Try out with the GSON Parsing tutorial which contains the similar JSON response.

+11 votes
by (100 points)

Here, I declare SubCat_list as static so I don't get the index-wise value:

public static void getDomainData() {

    try {
        JSONObject jsonobj = new JSONObject(Result);
        BusinessAllDataClass AllData = new BusinessAllDataClass();

        if (jsonobj.has(Constants.Key_CATEGORY)) {
            List<CategoryDataClass> Category_list = new ArrayList<CategoryDataClass>();
            JSONArray CatData = jsonobj
                    .getJSONArray(Constants.Key_CATEGORY);

            for (int i = 0; i < CatData.length(); i++) {
                if (CatData.getJSONObject(i).length() > 0) {
                    CategoryDataClass Cat = new CategoryDataClass();
                    JSONObject objCat = CatData.getJSONObject(i);
                    if (objCat.has(Constants.Key_CATEGORY_ID)) {
                        Cat.setCatId(objCat
                                .getString(Constants.Key_CATEGORY_ID));
                    }
                    if (objCat.has(Constants.Key_CATEGORY_NAME)) {
                        Cat.setCatName(objCat
                                .getString(Constants.Key_CATEGORY_NAME));
                    }
                    if (objCat.has(Constants.Key_CATEGORY_LOGO)) {
                        Cat.setCatLogo(objCat
                                .getString(Constants.Key_CATEGORY_LOGO));

                    }
                    if (objCat.has(Constants.Key_SUBCATEGORY)) {
                        List<SubCategoryDataClass> SubCategory_list = new ArrayList<SubCategoryDataClass>();
                        JSONArray SubCatData = objCat
                                .getJSONArray(Constants.Key_SUBCATEGORY);

                        for (int j = 0; j < 3; j++) {
                            if (SubCatData.getJSONObject(j).length() > 0) {
                                SubCategoryDataClass SubCat = new SubCategoryDataClass();
                                JSONObject objSubCat = SubCatData
                                        .getJSONObject(j);
                                if (objSubCat
                                        .has(Constants.Key_SUBCATEGORY_ID)) {
                                    SubCat.setSubCatId(objSubCat
                                            .getString(Constants.Key_SUBCATEGORY_ID));

                                }
                                if (objSubCat
                                        .has(Constants.Key_SUBCATEGORY_NAME)) {
                                    SubCat.setSubCatName(objSubCat
                                            .getString(Constants.Key_SUBCATEGORY_NAME));

                                }
                                if (objSubCat
                                        .has(Constants.Key_SUBCATEGORY_LOGO)) {
                                    SubCat.setSubCatLogo(objSubCat
                                            .getString(Constants.Key_SUBCATEGORY_LOGO));

                                }
                                if (objSubCat
                                        .has(Constants.Key_SUBCATEGORY_LONGITUDE)) {
                                    SubCat.setLongitude(objSubCat
                                            .getString(Constants.Key_SUBCATEGORY_LONGITUDE));

                                }
                                if (objSubCat
                                        .has(Constants.Key_SUBCATEGORY_LATITUDE)) {
                                    SubCat.setLatitiude(objSubCat
                                            .getString(Constants.Key_SUBCATEGORY_LATITUDE));

                                }
                                if (objSubCat.has(Constants.Key_DETAIL)) {
                                    List<SubCategorDescriptionDataClass> SubCatDescription_list = new ArrayList<SubCategorDescriptionDataClass>();
                                    JSONObject DescObj = objSubCat
                                            .getJSONObject(Constants.Key_DETAIL);
                                    SubCategorDescriptionDataClass SubCatDesc = new SubCategorDescriptionDataClass();
                                    if (DescObj.has(Constants.Key_STREET)) {
                                        SubCatDesc
                                                .setStreet(DescObj
                                                        .getString(Constants.Key_STREET));

                                    }
                                    if (DescObj.has(Constants.Key_CITY)) {
                                        SubCatDesc
                                                .setCity(DescObj
                                                        .getString(Constants.Key_CITY));

                                    }
                                    if (DescObj.has(Constants.Key_STATE)) {
                                        SubCatDesc
                                                .setState(DescObj
                                                        .getString(Constants.Key_STATE));

                                    }
                                    if (DescObj.has(Constants.Key_ZIPCODE)) {
                                        SubCatDesc
                                                .setzipcode(DescObj
                                                        .getString(Constants.Key_ZIPCODE));

                                    }
                                    if (DescObj
                                            .has(Constants.Key_DESCRIPTION)) {
                                        SubCatDesc
                                                .setDesc(DescObj
                                                        .getString(Constants.Key_DESCRIPTION));

                                    }

                                    if (DescObj.has(Constants.Key_SERVICES)) {
                                        SubCatDesc
                                                .setServices(DescObj
                                                        .getString(Constants.Key_SERVICES));
                                    }
                                    if (DescObj
                                            .has(Constants.Key_WORKINGHOUR)) {
                                        SubCatDesc
                                                .setWorkingHour(DescObj
                                                        .getString(Constants.Key_WORKINGHOUR));
                                    }
                                    if (DescObj
                                            .has(Constants.Key_CONTACT_NO)) {
                                        SubCatDesc
                                                .setContactNo(DescObj
                                                        .getString(Constants.Key_CONTACT_NO));
                                    }
                                    if (DescObj
                                            .has(Constants.Key_USER_EMAIL)) {
                                        SubCatDesc
                                                .setUsreEmail(DescObj
                                                        .getString(Constants.Key_USER_EMAIL));
                                    }
                                    if (DescObj.has(Constants.Key_WEBSITE)) {
                                        SubCatDesc
                                                .setWebsite(DescObj
                                                        .getString(Constants.Key_WEBSITE));
                                    }
                                    if (DescObj.has(Constants.Key_IMAGE)) {
                                        JSONArray DescImage = DescObj
                                                .getJSONArray(Constants.Key_IMAGE);

                                        for (int k = 0; k < DescImage
                                                .length(); k++) {

                                            SubCatDesc.ImageData
                                                    .add(DescImage
                                                            .getString(k));

                                        }
                                        SubCatDescription_list
                                                .add(SubCatDesc);

                                        SubCat.setSubCatDescList(SubCatDescription_list);
                                    }
                                }
                                SubCategory_list.add(SubCat);

                                Cat.setSubCatList(SubCategory_list);
                            }
                        }

                    }
                    Category_list.add(Cat);
                }
            }

            AllData.setCatList(Category_list);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
+6 votes
by (100 points)

In Android Studio, you can preview lorem ipsum TextViews with:

tools:text="@tools:sample/lorem/random"

You can also use this for Names, Cities, Zip codes, Avatars, and more.

see https://developer.android.com/studio/write/tool-attributes.html#toolssample_resources

+2 votes
by (100 points)
0 votes
by (100 points)

As error shown you should use a list instead of Sheet class. Try this:

     {"Sheets" :
       { 
       "Sheet1": [
          {
             "one": 1,
             "two": 18
          },
          {
             "one": 16,
             "two": 33
          },
          {
             "one": 17,
             "two": 34
          }
       ]
     }
    }
0 votes
by (100 points)

If /* Condition */ is ever true then the loop will end - as break will end the loop, irrespective of what nL is set to.

Thus the problem is /* Condition */ is never true.


Now, as Matt Greer pointed out, if the goal really is to loop infinitely (or to loop a really long time), this must be done in either small batches (such as with setTimeout ) or off the current global context (such as with Web Workers) such that browser will not freeze.

0 votes
by (100 points)

Change

sign_in_and_redirect(:user, authentication.user)

To

sign_in_and_redirect(authentication.user)
0 votes
by (100 points)

Your algorithm is not quite right. You need two nested for loops. The basic approach is:

For each character in the string, calculate how many characters immediately to its right are the same character, and break out once you find a difference

//basic pseudo-code, untested
for(int i = 0; i < someString.length() - 1; i++) { 
    char next = someString.charAt(i+1);
    int counter = 1;
    while(next == c) { 
        //increment counter, etc
    }
}

There is no need for an if/else to special case length == 1

0 votes
by (100 points)

you are adding redundant layers of properties to .menu and menu-item . Trying taking care of the concerned properties only. For example in your case menu needs to follow your coloring pattern when either hovered or in showing event. So add

.menu:hover, .menu:showing{
  -fx-background-color: <preferred backgound>;
}

Similarly add only focused property to menu-item , because the rest it shares the context-menu backgound

.menu-item:focused{
  -fx-background-color: <preferred background>;
}

Look at the snapshot of the simple demo:

enter image description here

No related questions found

...