简体   繁体   中英

How to get bean property value from its getter method?

I have a json string which contains custom object details as follow.

{
    "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"
    }
}

I want all the properties of this class using its getter methods. Here is the code snippet which i tried but failed.

    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");


    }
}

So Here in while loop i got values like.

getBestTime
getPassword
getCustomerId
getEmail
getLastName
getPhone
getCity
getFirstName
getAddress

Now i want to get value like bestTime , password etc. the bean properties.

Note : I cant use java reflection because i dont have this class in my classpath. From its getters only i want properties. Please help me.

Try gson .

Then you can do stuff like

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

To get the property name (ie. bestTime ) from the getter (ie getBestTime ), assuming the class complies with the camel case convention, I would simply substring(3) and decapitalize the result, using either Introspector.decapitalize (available in the JDK) or 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)));
}

You can you do it simple with Gson

JSONObject json = new JSONObject(Your_String);
//get CustomerDetailsobject
JSONObject jsonObject = json.getJSONObject("bankapp.bean.account.CustomerDetails");

Pase with 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()));
    }

Your properties

email
city
address
bestTime
phone
password
customerId
lastName
firstName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM