java - gson parse items from comma-separated-value string within a json value -
i parsing json string using gson. similar this:
{ "acode": "aa", "rcode": "rr", "errors": "e1,e2,e3" } i think errors should have been proper json array, don't have control of that.
i want errors array or collection in java. easy enough using string.split comma separator. new gson , don't know if ignoring functionality provides parse comma separated string.
does know whether gson can handle automatically?
i want errors array or collection in java.
try one
string json = "{\"acode\": \"aa\",\"rcode\": \"rr\", \"errors\": \"e1,e2,e3\" }"; class errorsdeserializer implements jsondeserializer<string[]> { public string[] deserialize(jsonelement json, type typeoft, jsondeserializationcontext context) throws jsonparseexception { return ((jsonobject) json).getasjsonprimitive("errors").getasstring().split(","); } } gson gson1 = new gsonbuilder().registertypeadapter(string[].class, new errorsdeserializer()) .create(); string[] errors = gson1.fromjson(json, string[].class); (string error : errors) { system.out.println(error); }
Comments
Post a Comment