JSON generation and parsing in Java

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.