JSON (JavaScript Object Notation) is a lightweight data exchange format that uses a completely language-independent text format and is an ideal data exchange format. Meanwhile, JSON is a JavaScript native format, which means that no special API or toolkit is required to process JSON data in JavaScript.
Many json construction and parsing tools under JAVA are published on www.json.org. Among them, org.json and json-lib are relatively simple. The two are similar in use but there are still some differences. The following is a first example of how to construct and parse Json data using json-lib.
For detailed explanation of the methods of constructing and parsing Json data using org.son, please refer to my next blog post: Two methods of constructing and parsing Json data using Java (Detailed explanation 2)
1. Introduction
The JSON-lib package is a package that converts beans, collections, maps, java arrays and XML and JSON. It is mainly used to parse Json data. It has detailed explanations on its official website http://www.json.org/. If you are interested, you can study it.
2. Download the jar dependency package: you can download it here
3. Introduction to basic methods
1. Convert List collection to json method
List list = new ArrayList();list.add( "first" );list.add( "second" );JSONArray jsonArray2 = JSONArray.fromObject( list );
2. Convert Map collection to json method
Map map = new HashMap();map.put("name", "json");map.put("bool", Boolean.TRUE);map.put("int", new Integer(1));map.put("arr", new String[] { "a", "b" });map.put("func", "function(i){ return this.arr[i]; }");JSONObject json = JSONObject.fromObject(map); 3. Convert Bean to JSON code
JSONObject jsonObject = JSONObject.fromObject(new JsonBean());
4. Convert array to json code
boolean[] boolArray = new boolean[] { true, false, true };JSONArray jsonArray1 = JSONArray.fromObject(boolArray);5. Convert general data into json code
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );6. Convert beans to json code
List list = new ArrayList();JsonBean2 jb1 = new JsonBean2();jb1.setCol(1);jb1.setRow(1);jb1.setValue("xx");JsonBean2 jb2 = new JsonBean2();jb2.setCol(2);jb2.setRow(2);jb2.setValue("");list.add(jb1);list.add(jb2);JSONArray ja = JSONArray.fromObject(list); 4. Demonstration example
Here are some basic common methods to test
package com.json;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import net.sf.json.JSONArray;import net.sf.json.JSONObject;/*** Use json-lib to construct and parse Json data* * @author Alexia* @date 2013/5/23**/public class JsonTest {/*** Construct Json data* * @return*/public static String BuildJson() {// JSON format data parsing object JSONObject jo = new JSONObject();// The following constructs two maps, a list and an Employee object Map<String, String> map1 = new HashMap<String, String>();map1.put("name", "Alexia");map1.put("sex", "female");map1.put("age", "23");Map<String, String> map2 = new HashMap<String, String>();map2.put("name", "Edward");map2.put("sex", "male");map2.put("age", "24");List<Map> list = new ArrayList<Map>();list.add(map1);list.add(map2);Employee employee = new Employee();employee.setName("wjl");employee.setSex("female");employee.setAge(24);// Convert Map to JSONArray dataJSONArray ja1 = JSONArray.fromObject(map1);// Convert List to JSONArray dataJSONArray ja2 = JSONArray.fromObject(list);// Convert Bean to JSONArray data JSONArray ja3 = JSONArray.fromObject(employee);System.out.println("JSONArray object data format:");System.out.println(ja1.toString());System.out.println(ja2.toString());System.out.println(ja3.toString());// Construct Json data, including a map and an Employee object jo.put("map", ja1);jo.put("employee", ja2);System.out.println("/n final constructed JSON data format: ");System.out.println(jo.toString());return jo.toString();}/*** parse Json data* * @param jsonString Json data string*/public static void ParseJson(String jsonString) {// Taking employee as an example to parse, map is similar to JSONObject jb = JSONObject.fromObject(jsonString);JSONArray ja = jb.getJSONArray("employee");List<Employee> empList = new ArrayList<Employee>();// Loop adds Employee object (maybe there are multiple) for (int i = 0; i < ja.size(); i++) {Employee employee = new Employee();employee.setName(ja.getJSONObject(i).getString("name"));employee.setSex(ja.getJSONObject(i).getString("sex"));employee.setAge(ja.getJSONObject(i).getInt("age"));empList.add(employee);}System.out.println("/nConvert Json data to Employee object:");for (int i = 0; i < empList.size(); i++) {Employee emp = empList.get(i);System.out.println("name: " + emp.getName() + " sex: "+ emp.getSex() + " age: " + emp.getAge());}}/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubParseJson(BuildJson());}} The operation results are as follows
5. Compare with org.json
The use of json-lib and org.json is almost the same. I have summarized two differences:
1. org.json is much lighter than json-lib. The former does not rely on any other jar packages, while the latter depends on lang, logging, beanutils, collections and other components of ezmorph and commons.
2. json-lib is much more convenient than org.json when constructing beans and parsing beans. json-lib can directly convert with beans, while org.json cannot directly convert with beans and requires maps as transit. If you convert beans to json data, you must first convert the beans to maps and then convert the maps to json, which is more troublesome.
In short, the same sentence is still the best thing for you. Everyone should choose which method to use to analyze it as needed. Finally, I will introduce two tools to analyze Json data: one is the online tool JSON Edit (http://braincast.nl/samples/jsoneditor/); the other is Eclipse plugin JSON Tree Analyzer, both of which are very useful and are recommended for everyone to use!
The above are two methods introduced to you by using Java to construct and parse Json data (detailed explanation one). I hope it will be helpful to everyone!