1.Java object to JSON object
1 2 |
Student stu = new Student("Public Number BiggerBoy", "m", 2); JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu); |
2.Java object to JSON string
1 2 |
Student stu = new Student("Public Number BiggerBoy", "m", 2); String stuString = JSONObject.toJSONString(stu); |
3.JSON object to JSON string
1 2 3 |
Student stu = new Student("Public Number BiggerBoy", "m", 2); JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu); String jsonString = jsonObject.toJSONString(); |
4.JSON object to java object
1 2 3 |
Student stu = new Student("Public Number BiggerBoy", "m", 2); JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu); Student student = JSONObject.toJavaObject(jsonObject, Student.class); |
5.json string to JSON object
1 2 |
String stuString = "{\"age\":2,\"name\":\"Public Account BiggerBoy\",\"sex\":\"m\"}"; JSONObject jsonObject1 = JSONObject.parseObject(stuString); |
6.JSON string to Java object
1 2 |
String stuString = "{\"age\":2,\"name\":\"Public Account BiggerBoy\",\"sex\":\"m\"}"; Student student1 = JSONObject.parseObject(stuString, Student.class); |
7.JSON string to list
1 2 |
String stuString = "[{\"age\":2,\"name\":\"Public Account\",\"sex\":\"m\"},{\"age\":18,\ "name\":\"BiggerBoy\",\"sex\":\"m\"}]"; List studentList = JSONObject.parseArray(stuString, Student.class); |