public static final Object parse ( String text) ;
public static final JSONObject parseObject ( String text) ;
public static final < T> T parseObject ( String text, Class< T> clazz) ;
public static final JSONArray parseArray ( String text) ;
public static final < T> List< T> parseArray ( String text, Class< T> clazz) ;
public static final String toJSONString ( Object object) ;
public static final String toJSONString ( Object object, boolean prettyFormat) ;
public static final Object toJSON ( Object javaObject) ;
import com. alibaba. fastjson. JSON;
import com. alibaba. fastjson. JSONArray;
import com. alibaba. fastjson. JSONObject;
public class FastjsonTest {
@Test
public void test1 ( ) {
Person person = new Person ( ) ;
person. setName ( "zhangsan" ) ;
person. setAge ( 20 ) ;
String json = JSON. toJSONString ( person) ;
System. out. println ( json) ;
String format_json = JSON. toJSONString ( person, true ) ;
System. out. println ( format_json) ;
}
@Test
public void test2 ( ) {
String json_array = "[{\"age\":20,\"name\":\"zhangsan\"},{\"age\":22,\"name\":\"lisi\"}]" ;
JSONArray array = JSON. parseArray ( json_array) ;
System. out. println ( array) ;
List< Person> list = JSON. parseArray ( json_array, Person. class ) ;
for ( Person person : list) {
System. out. println ( person) ;
}
}
@Test
public void test3 ( ) {
String json = "{\"age\":20,\"name\":\"zhangsan\"}" ;
JSONObject object = JSON. parseObject ( json) ;
System. out. println ( object) ;
Person person = JSON. parseObject ( json, Person. class ) ;
System. out. println ( person) ;
}
}