1

I have Json array as below

{ "template": { "data": [{ "name": "customerGroupId", "value": "" }, { "name": "assetIntegrationId", "value": "" }, { "name": "problemCategory", "value": "" }, { "name": "problemSubCategory", "value": "" }, { "name": "resolutionCode", "value": "" }, { "name": "resolutionSubCode", "value": "" }, { "name": "imei", "value": "" }, { "name": "make", "value": "" }, { "name": "model", "value": "" }] } }

I am using following code to get values.

JSONArray jsonArray = jsonObject.getJSONObject("template").getJSONArray("data");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObjectData = jsonArray.getJSONObject(i);
            if ("customerGroupId".equals(jsonObjectData.get("name"))) {
                customerBean.setCustomerGroupId(jsonObjectData.get(VALUE).toString());
                LOGGER.debug("JSON customerGroupId  " + jsonObjectData.get(VALUE).toString());
            } else if ("assetIntegrationId".equals(jsonObjectData.get("name"))) {
                customerBean.setAssetIntegrationId(jsonObjectData.get(VALUE).toString());
                LOGGER.debug("JSON assetIntegrationId  " + jsonObjectData.get(VALUE).toString());
            } else if ("problemCategory".equals(jsonObjectData.get("name"))) {
                customerBean.setProblemCategory(jsonObjectData.get(VALUE).toString());
                LOGGER.debug("JSON problemCategory  " + jsonObjectData.get(VALUE).toString());
            } else if ("problemSubCategory".equals(jsonObjectData.get("name"))) {
                customerBean.setProblemSubCategory(jsonObjectData.get(VALUE).toString());
                LOGGER.debug("JSON problemSubCategory  " + jsonObjectData.get(VALUE).toString());
            } else if ("resolutionCode".equals(jsonObjectData.get("name"))) {
                customerBean.setResolutionCode(jsonObjectData.get(VALUE).toString());
                LOGGER.debug("JSON resolutionCode  " + jsonObjectData.get(VALUE).toString());
            }

As the code has become repetitive,Is there any way in Java 8 or Java to avoid repetition of code.

5
  • you can use switch statement Commented Aug 29, 2018 at 11:52
  • Looks like this has been answered here , please check. stackoverflow.com/questions/42854505/… Commented Aug 29, 2018 at 11:59
  • Hello. I'm not sure there's an alternative. This is the consequence of your data structure. Commented Aug 29, 2018 at 11:59
  • Why do you need to have name and value keys? It seems to me that they are redundant. You could have for instance "customerGroupId" : "" etc. In this way you can do direct assignement to the correct property since you know which property you will be getting from the json. Commented Aug 29, 2018 at 12:13
  • Had that to in mind but can't help with the backend Json Commented Aug 29, 2018 at 12:25

1 Answer 1

2

You can try to use introspector or reflection. And use name to find property or field. Introspector:

JSONArray json = new JSONArray();
CustomerBean customerBean = new CustomerBean();
for (int i = json.size() - 1; i >= 0; i--) {
    JSONObject data = json.getJSONObject(i);
    PropertyDescriptor propDesc = new PropertyDescriptor(data.getString("name"), CustomerBean.class);
    Method methodWriter = propDesc.getWriteMethod();
    methodWriter.invoke(customerBean, data.getString("value"));
}

Reflection:

JSONArray json = new JSONArray();
CustomerBean customerBean = new CustomerBean();
for (int i = json.size() - 1; i >= 0; i--) {
    JSONObject data = json.getJSONObject(i);
    Field field = CustomerBean.class.getDeclaredField(data.getString("name"));
    field.set(customerBean, data.get("data"));

}
Sign up to request clarification or add additional context in comments.

3 Comments

This problem is not related to the code itself but with the data structure so introspection or reflection won't help.
Why can't refletion be used? His requirement is that the name in the original array corresponds to the properties in the Java bean, and that data is the value of the properties . He doesn't want so much duplicate code, so is it wrong to use reflection or introspection instead of if?
You're right. It's a good solution. I wasn't thinking about this.

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.