java - How to get bean property value from its getter method? -
i have json string contains custom object details 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 properties of class using getter methods. here code snippet tried 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 = 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 got values like.
getbesttime getpassword getcustomerid getemail getlastname getphone getcity getfirstname getaddress now want value besttime , password etc. bean properties.
note : cant use java reflection because dont have class in classpath. getters want properties. please me.
you can simple gson
jsonobject json = new jsonobject(your_string); //get customerdetailsobject jsonobject jsonobject = json.getjsonobject("bankapp.bean.account.customerdetails"); pase 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(); (string key : keys) { stringbuilder.append(key).append(","); } string str = stringbuilder.tostring(); str = str.replaceall("get", "").replaceall("set", ""); // remove duplicate linkedhashset<string> list = new linkedhashset<string>(arrays.aslist(str.split(","))); (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
Comments
Post a Comment