Converting Strings to booleans when converting XML to JSON
This block of code essentially takes a JAXB Object and turns it into a JSONObject
StringWriter stringWriter = new StringWriter();
marshaller.marshal(jaxbObj, stringWriter);
try {
JSONObject jsonObject = XML.toJSONObject(stringWriter.toString());
resp.getOutputStream().write(jsonObject.toString(2).getBytes());
} catch (JSONException e) {
throw new ServletException("Could not parse JSON",e);
}
Unfortunately, this transformation doesn't turn, say, a String like "true" into a boolean, leaving the poor front end guy to do it.
I would think that I want to somehow map over the values in the JSONObject, calling s开发者_开发技巧tringToValue on each. I have a feeling there is a better way. Any ideas?
Well, JAXB itself won't even produce JSON to begin with, so you are using something else in addition (maybe use it via Jersey). So maybe package in question has something.
But why try to do this with org.json objects? Just use regular Java bean with expected type, create that from whatever input bean you have. No need for magic, just explicit code.
精彩评论