Official Website: http://www.json.org/json-zh.html
Lib download: http://sourceforge.net/projects/json-lib/files/
Instance: https://github.com/douglascrockford/JSON-java
JSON. jsp: http://json-taglib.sourceforge.net/
Online validation: http://www.bejson.com/go.html? U = http://www.bejson.com/
Here, org. JSON and JSON-lib are relatively simple, and the two are similar in use. The following two source codes use the two tools to parse and construct the JSON demo programs.
1. This is a JSON-Lib program:
import java.util.HashMap; import java.util.Map; import net.sf.json.JSONObject; public class Test { public static void main(String[] args) { String json = "{/"name/":/"reiz/"}"; JSONObject jsonObj = JSONObject.fromObject(json); String name = jsonObj.getString("name"); jsonObj.put("initial", name.substring(0, 1).toUpperCase()); String[] likes = new String[] { "JavaScript", "Skiing", "Apple Pie" }; jsonObj.put("likes", likes); Map <String, String> ingredients = new HashMap <String, String>(); ingredients.put("apples", "3kg"); ingredients.put("sugar", "1kg"); ingredients.put("pastry", "2.4kg"); ingredients.put("bestEaten", "outdoors"); jsonObj.put("ingredients",ingredients); System.out.println(jsonObj); } }
2. Here is the program using org. JSON:
import java.util.HashMap; import java.util.Map; import org.json.JSONException; import org.json.JSONObject; public class Test { public static void main(String[] args) throws JSONException { String json = "{/"name/":/"reiz/"}"; JSONObject jsonObj = new JSONObject(json); String name = jsonObj.getString("name"); jsonObj.put("initial", name.substring(0, 1).toUpperCase()); String[] likes = new String[] { "JavaScript", "Skiing", "Apple Pie" }; jsonObj.put("likes", likes); Map <String, String> ingredients = new HashMap <String, String>(); ingredients.put("apples", "3kg"); ingredients.put("sugar", "1kg"); ingredients.put("pastry", "2.4kg"); ingredients.put("bestEaten", "outdoors"); jsonObj.put("ingredients", ingredients); System.out.println(jsonObj); System.out.println(jsonObj); } }
The use of the two is almost the same, but Org. JSON is much lighter than JSON-Lib. The former does not have any dependencies, while the latter depends on the Lang, logging, beanutils, collections and other components of ezmorph and commons.