i consuming rest web services using rest template in project returns json below:
{"data": [{ "id": "int-1468819244684-event-no-97", "object1": { "jsonstring": "{\"object2\":[{\"object3\":\"value3\",\"object4\":\"value4\"}]}" } }]
}
while consuming above json response able create bean class , able dump json object/values same.
but problem above json response contains string below:
"jsonstring": "{\"object2\":[{\"object3\":\"value3\",\"object4\":\"value4\"}]}"
which json. have bean in can fetch jsonstring string. can use below bean structure fetch response in objects:
public class response { data data; } public class data { string id; object1 object1; } public class object1 { string jsonstring; }
but above jsonstring contains string in form of json, want somehow convert json string json object @ run time when other objects created , dump content in same bean application should ready use content. ideally bean hierarchy should below:
public class response { data data; } public class data { string id; object1 object1; } public class object1 { jsonstring jsonstring; } public class jsonstring { object2 object2; } public class object2 { string object3; string object4; }
please guide me how same.
you can use jackson's objectmapper.readvalue in way:
// create or use existing objectmapper objectmapper om = new objectmapper(); @jsonproperty("jsonstring") public string getjsonstring() { if (this.jsonstring == null) return null; return om.writevalueasstring(this.jsonstring); } public void setjsonstring(string s) { this.jsonstring = om.readvalue(s, jsonstring.class); }
Comments
Post a Comment