java - How to ignore a specific field while parsing a JSON into map -
i want parse below json pojo. using jackson parse json.
{ "totalsize": 4, "done": true, "records": [ { "attributes": { "type": "oppor", "url": "/service/oppor/456" }, "accountid": "123", "id": "456", "proposalid": "103" } ] }
in above json, fields "totalsize", "done", "records" , "attributes" known fields. whereas, "accountid", "id" , "proposalid" unknown fields. , in above json, don't need "attributes" part of bean object.
and here equivalent bean class json
public class result { private int totalsize; private boolean done; private list<map<string, string>> records; public int gettotalsize() { return totalsize; } public void settotalsize(int totalsize) { this.totalsize = totalsize; } public boolean isdone() { return done; } public void setdone(boolean done) { this.done = done; } public list<map<string,string>> getrecords() { return records; } public void setrecords(list<map<string, string>> records) { this.records = records; } }
hence there unknown fields in records element used list results element in bean. here in map, don't want field "attributes". how can ignore while parsing? , below exception getting attributes not string element.
com.fasterxml.jackson.databind.jsonmappingexception: can not deserialize instance of java.lang.string out of start_object token @ [source: [b@66fdec9; line: 1, column: 40] (through reference chain: com.sample.json.result["records"]) @ com.fasterxml.jackson.databind.jsonmappingexception.from(jsonmappingexception.java:164) @ com.fasterxml.jackson.databind.deserializationcontext.mappingexception(deserializationcontext.java:691) @ com.fasterxml.jackson.databind.deser.std.stringdeserializer.deserialize(stringdeserializer.java:46) @ com.fasterxml.jackson.databind.deser.std.stringdeserializer.deserialize(stringdeserializer.java:11) @ com.fasterxml.jackson.databind.deser.std.mapdeserializer._readandbindstringmap(mapdeserializer.java:430) @ com.fasterxml.jackson.databind.deser.std.mapdeserializer.deserialize(mapdeserializer.java:312) @ com.fasterxml.jackson.databind.deser.std.mapdeserializer.deserialize(mapdeserializer.java:26) @ com.fasterxml.jackson.databind.deser.std.collectiondeserializer.deserialize(collectiondeserializer.java:227) @ com.fasterxml.jackson.databind.deser.std.collectiondeserializer.deserialize(collectiondeserializer.java:204) @ com.fasterxml.jackson.databind.deser.std.collectiondeserializer.deserialize(collectiondeserializer.java:23)
update 2015/08/29:
as have commented
i achieved dynamic field support parsing json map. ignoring bad json element pending
i suggest should process original jsonobject remove "attributes"
element it.
original jsonobject, example:
{ "totalsize": 4, "done": true, "records": [ { "attributes": { "type": "oppor", "url": "/service/oppor/456" }, "accountid": "123", "id": "456", "proposalid": "103" } ] }
after process, new jsonobject following:
{ "records": { "accountid": "123", "id": "456", "proposalid": "103" }, "totalsize": 4, "done": true }
use code following:
jsonobject jsonobject; try { jsonobject = new jsonobject(jsonstring1); jsonarray jsonarray = new jsonarray(jsonobject.get("records").tostring()); jsonobject jsonobject1 = jsonarray.getjsonobject(0); jsonobject1.remove("attributes"); jsonobject.put("records", jsonobject1); } catch (jsonexception e) { e.printstacktrace(); }
then, use own code achieved dynamic field support parsing json map
.
end of update 2015/08/29
i suggest use gson
, transient
in case
like this
string jsonstring1 = "{\n" + " \"totalsize\": 4,\n" + " \"done\": true,\n" + " \"records\": [\n" + " {\n" + " \"attributes\": {\n" + " \"type\": \"oppor\",\n" + " \"url\": \"/service/oppor/456\"\n" + " },\n" + " \"accountid\": \"123\",\n" + " \"id\": \"456\",\n" + " \"proposalid\": \"103\"\n" + " }\n" + " ]\n" + "}"; gson gson = new gson(); result result1 = gson.fromjson(jsonstring1, result.class);
your classes, pay attention transient
:
public class result { private int totalsize; private boolean done; private list<record> records; } public class record { private transient map<string, string> attributes; private int accountid; private int id; private int proposalid; }
you result:
p/s: tested in android studio :)
update:
string jsonstring1 = "{\n" + " \"totalsize\": 4,\n" + " \"done\": true,\n" + " \"records\": [\n" + " {\n" + " \"attributes\": {\n" + " \"type\": \"oppor\",\n" + " \"url\": \"/service/oppor/456\"\n" + " },\n" + " \"accountid\": \"123\",\n" + " \"id\": \"456\",\n" + " \"proposalid\": \"103\"\n" + " }\n" + " ]\n" + "}"; gson gson = new gson(); object object = gson.fromjson(jsonstring1, object.class); map<string, string> stringmap = (map<string, string>) object; result myresult = new result(); iterator entries = stringmap.entryset().iterator(); while (entries.hasnext()) { map.entry entry = (map.entry) entries.next(); string key = entry.getkey().tostring(); string value = entry.getvalue().tostring(); switch (key) { case "totalsize": myresult.totalsize = (int) double.parsedouble(entry.getvalue().tostring()); break; case "done": myresult.done = boolean.valueof(entry.getvalue().tostring()); break; case "records": try{ object object1 = entry.getvalue(); list<object> objectlist = (list<object>) object1; map<string, object> stringmap2 = (map<string, object>) objectlist.get(0); map<string, string> recordmap = new hashmap<>(); iterator entries2 = stringmap2.entryset().iterator(); while (entries2.hasnext()) { map.entry entry2 = (map.entry) entries2.next(); string key2 = entry2.getkey().tostring(); string value2 = entry2.getvalue().tostring(); if (!"attributes".equals(key2)) { recordmap.put(key2, value2); } entries2.remove(); } myresult.records = recordmap; } catch (exception e) { e.printstacktrace(); } break; } entries.remove(); }
classes:
public class result { private int totalsize; private boolean done; private map<string, string> records; }
debug result:
Comments
Post a Comment