Toolkitorg.json.jar
is a lightweight json construction and parsing toolkit under JAVA. It also includes conversion of JSON to XML, HTTP headers, Cookies, and CDL.
Recommended here: Alibaba FastJson is a Json processing toolkit, including "serialization" and "deserialization". It has the following characteristics:
Fastest speed, testing It shows that fastjson has extremely fast performance, surpassing any other Java Json parser. Including JackJson, which claims to be the fastest;
Recommended courses:Java Tutorial.
is powerful and fully supports Java Beans, collections, Maps, dates, Enums, supports paradigms, and supports introspection; has no dependencies and can run directly on Java SE 5.0 or above. ;Support Android; Open source (Apache 2.0)
Source code address:
https://github.com/alibaba/fastjson
The Fastjson API entry class is com.alibaba.fastjson.JSON. Common serialization operations can be completed directly through static methods on the JSON class.
public static final Object parse(String text); // Parse JSON text into JSONObject or JSONArray
public static final JSONObject parseObject(String text); // Parse JSON text into JSONObject
public static final T parseObject(String text, Class clazz); // Parse JSON text into JavaBean
public static final JSONArray parseArray(String text); // Parse JSON text into JSONArray
public static final List parseArray (String text, Class clazz); //Parse JSON text into a JavaBean collection
public static final String toJSONString(Object object); //Serialize JavaBean into JSON text
public static final String toJSONString(Object object , boolean prettyFormat); // Serialize JavaBean into formatted JSON text
public static final Object toJSON(Object javaObject); Convert JavaBean to JSONObject or JSONArray.
The above are common methods in some projects.
The following is the code I wrote for practice:
package com.test; public class TestPerson { private int age; private String name; public TestPerson(){ } public TestPerson(int age,String name){ this.age=age; this.name=name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } } package com.test; import java.util.*; import com.alibaba.fastjson.*; public final class TestFastJson { public static void main(String[] args) { // TODO Auto-generated method stub TestPerson json = new TestPerson(19,"李明"); Listlist = new ArrayList (); list.add(json); list.add(new TestPerson(12,"张三")); //将集合或者对象序例化成JSON System.out.println(JSON.toJSON(json)); System.out.println( JSON.toJSON(list) ); //Json串反序列化成对象 TestPerson person = JSON.parseObject("{\"name\":\"李明\",\"age\":19}", TestPerson.class); System.out.printf("name:%s,age:%d\n",person.getName(),person.getAge()); String str = "[{\"name\":\"李明\",\"age\":19},{\"name\":\"张三\",\"age\":12}]"; //数组对象反序列化成集合 List listPerson = JSON.parseArray(str,TestPerson.class); for(TestPerson item : listPerson){ System.out.println( item.getName() ); System.out.println( item.getAge()); } //没有对象直接解析JSON对象 JSONObject jobj = JSON.parseObject("{\"name\":\"李明\",\"age\":19}"); System.out.printf("name:%s,age:%d\n",jobj.getString("name"),jobj.getBigInteger("age")); //没有对象直接解析JSON数组 JSONArray jarr = JSON.parseArray("[{\"name\":\"李明\",\"age\":19},{\"name\":\"张三\",\"age\":12}]"); for(int i=0,len=jarr.size();i
The above is the detailed content of What package does java use to parse json?. For more information, please follow other related articles on the PHP Chinese website!