How to read stringified JSON string in Java [duplicate]
Kindly need your help as this really taking me long time to try. From JSP, I passed the stingnify JSON object as a String to the Java action, it like
String jsonDealer = [{"dealerID":"VARSSWK103","dealerName":"N开发者_运维问答PD STATION SDN BHD"},
{"dealerID":"VARSSTH008","dealerName":"Winjaya Teleshop"}]
How I can convert this to JSON object/ or ArrayList of Dealer, so that I can retrieve the dealer ID and dealer name?
Thanks for all help!!!
Well you could write your own JSON parser, but there's no need to re-invent the wheel - there are a number of robust, mature JSON parsers freely available. I use JSONObject and JSONArray from Tapestry. If you download Tapestry 5 and unpack it, then just include the library tapestry-core-5.0.18.jar
in your build path and you'll be set to go. Both JSONObject and JSONArray take a String as constructor argument, and the api is fully documented in those links.
using http://json-lib.sourceforge.net/ can be realy easy.
See http://json-lib.sourceforge.net/snippets.html
String str = "{'dealerName':'NPD STATION SDN BHD', 'dealerID': 1, 'dealerReputation': 2.0, 'dealerActive': true}";
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str );
and
String str = "['NPD STATION SDN BHD', 1, 2.0, true]";
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( str );
精彩评论