public class TestJson {
@Test
public void test1(){
Student s1 = new Student();
s1.setAge(22);
s1.setName("fuck");
s1.setScore(91.0);
List<Student> list = new ArrayList<Student>();
list.add(s1);
System.out.println("************************************");
JSONArray array = JSONArray.fromObject(list);
System.out.println(array.toString());
System.out.println("************************************");
JSONArray array2 = JSONArray.fromObject(s1);
System.out.println(array2.toString());
System.out.println("************************************");
JSONObject object = JSONObject.fromObject(s1);
System.out.println(object);
System.out.println("************************************");
Student student = (Student) JSONObject.toBean(object, Student.class);
System.out.println("Student: "+student.getAge());
System.out.println("************************************");
List<Student> list2 = JSONArray.toList(array2);
System.out.println(list2);
}
@Test
public void test2(){
JSONObject object = new JSONObject();
object.put(1, "hao");
object.put(2, "good");
object.put(3, "544");
object.put(4, "you");
System.out.println(object.getInt("3"));
System.out.println(object.getString("4"));
}
@Test
public void test3() throws IOException{
FileInputStream fis = new FileInputStream("R:\\testjson.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String result = "";
String line = "";
while((line=br.readLine())!=null){
result = result + line ;
}
fis.close();
br.close();
JSONObject obj = JSONObject.fromObject(result);
System.out.println(obj.toString());
System.out.println(obj.getString("from"));
System.out.println("**************************");
JSONArray arr = obj.getJSONArray("go");
System.out.println("arr: "+arr);
System.out.println(arr.getJSONObject(0));
}
@Test
public void test4(){
boolean[] boo = new boolean[]{false,true,false,true};
JSONArray obj = JSONArray.fromObject(boo);
System.out.println(obj);
}
@Test
public void test5(){
String json = "{'who':'me','what':[{'way':'fuck you'}],'where':'now'}";
JSONObject obj = JSONObject.fromObject(json);
System.out.println(obj);
System.out.println("************************");
JSONArray arr = obj.getJSONArray("what");
System.out.println(arr);
System.out.println(arr.getJSONObject(0));
System.out.println(arr.getJSONObject(0).getString("way"));
}
}