首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Java8迭代JsonArray

如何使用Java8迭代JsonArray
EN

Stack Overflow用户
提问于 2018-08-29 19:48:37
回答 1查看 2.1K关注 0票数 1

我有Json数组,如下所示

代码语言:javascript
运行
复制
{ "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": "" }] } }

我使用下面的代码来获取值。

代码语言:javascript
运行
复制
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());
            }

由于代码已经变得重复,在Java 8或Java中有什么方法可以避免代码的重复。

EN

回答 1

Stack Overflow用户

发布于 2018-08-29 19:55:43

您可以尝试使用introspector或反射。并使用name查找属性或字段。Introspector:

代码语言:javascript
运行
复制
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"));
}

反射:

代码语言:javascript
运行
复制
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"));

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52076727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档