如何从getter方法获取bean属性值?

时间:2014-04-14 12:50:18

标签: java json

我有一个json字符串,其中包含自定义对象详细信息,如下所示。

{
    "bankapp.bean.account.CustomerDetails": {
        "setEmail": "value",
        "setCity": "value",
        "setAddress": "value",
        "setBestTime": "value",
        "getBestTime": "value",
        "setPhone": "value",
        "getPassword": "value",
        "getCustomerId": "value",
        "setLastName": "value",
        "getEmail": "value",
        "getLastName": "value",
        "setFirstName": "value",
        "setCustomerId": "value",
        "getPhone": "value",
        "setPassword": "value",
        "getFirstName": "value",
        "getCity": "value",
        "getAddress": "value"
    }
}

我希望使用其getter方法获取此类的所有属性。这是我尝试但失败的代码片段。

    String jsonString = "{\"bankapp.bean.account.CustomerDetails\":{\"setEmail\":\"value\",\"setCity\":\"value\",\"setAddress\":\"value\",\"setBestTime\":\"value\",\"getBestTime\":\"value\",\"setPhone\":\"value\",\"getPassword\":\"value\",\"getCustomerId\":\"value\",\"setLastName\":\"value\",\"getEmail\":\"value\",\"getLastName\":\"value\",\"setFirstName\":\"value\",\"setCustomerId\":\"value\",\"getPhone\":\"value\",\"setPassword\":\"value\",\"getFirstName\":\"value\",\"getCity\":\"value\",\"getAddress\":\"value\"}}";
JsonObject jObject = new JsonObject(jsonString);
Iterator i = jObject.keys();
while(i.hasNext()) {
    String currentKey = String.valueOf(i.next());
    Object currentValue = jObject.get(currentKey);

    if(currentValue instanceof String) {
        System.out.println("Primitive Object");
    }
    else if(currentValue instanceof JSONObject) {
        //System.out.println("JSON Object");
        String key = getKeyName(jsonString);
        Object jsonObject = jObject.get(key);
        List<String> lList = breakTheJson(jsonObject.toString());
        //List<String> allProperties = new ArrayList<String>(); 
        Iterator<String> iterator = lList.iterator();
        while (iterator.hasNext()) {                    
                String val = iterator.next();
                if(val.startsWith("get") || val.startsWith("is")){

                    System.out.println(val);

                }

        }

    }
    else if(currentValue instanceof JSONArray) {
        System.out.println("Custom Object");


    }
}

所以在while循环中我得到的值就像。

getBestTime
getPassword
getCustomerId
getEmail
getLastName
getPhone
getCity
getFirstName
getAddress

现在我想得到像bestTime,密码等bean属性的价值。

注意:我无法使用java反射,因为我的类路径中没有这个类。从它的吸气剂只有我想要的属性。 请帮帮我。

3 个答案:

答案 0 :(得分:0)

尝试gson

然后你可以做像

这样的事情

CustomerDetails customerDetails = gson.fromJson(jsonString,CustomerDetails .class)

答案 1 :(得分:0)

要从getter获取属性名称(即bestTime)(即getBestTime),假设该类符合camel case约定,我只需substring(3)并对其进行decapitalize结果,使用Introspector.decapitalize(在JDK中可用)或StringUtils.uncapitalize(Apache Commons):

if(val.startsWith("get")){
    System.out.println(Introspector.decapitalize(val.substring(3)));
    System.out.println(StringUtils.uncapitalize(val.substring(3)));
} else if(val.startsWith("is")){
    System.out.println(Introspector.decapitalize(val.substring(2)));
    System.out.println(StringUtils.uncapitalize(val.substring(2)));
}

答案 2 :(得分:0)

你可以用Gson

来做到这一点
JSONObject json = new JSONObject(Your_String);
//get CustomerDetailsobject
JSONObject jsonObject = json.getJSONObject("bankapp.bean.account.CustomerDetails");

用gson解析

Gson gson = new Gson();
Type typeOf = new TypeToken   <Map<String, String>>(){}.getType();
Map<String,String> map = gson.fromJson(jsonObject.toString(), typeOf);

 Set<String> keys = map.keySet();
    StringBuilder stringBuilder = new StringBuilder();
        for (String key : keys) {
         stringBuilder.append(key).append(",");
        }

String str = stringBuilder.toString();
str = str.replaceAll("get", "").replaceAll("set", "");

// also remove the duplicate
LinkedHashSet<String> list = new LinkedHashSet<String>(Arrays.asList(str.split(",")));
for (String property : list) {
    String lowerChar = Character.toString(property.charAt(0)).toLowerCase();
    System.out.println(lowerChar + property.substring(1, property.length()));
    }

您的属性

email
city
address
bestTime
phone
password
customerId
lastName
firstName