解析案例html
String object="{total=1, rows=[{_Account=3646808, UserID=131514, Mt4Name=SewwoaIQQS, ServerID=14, InMoney=7911.0, OutMoney=6479.76, Balance=3001.95, Margin=0.0, Equity=2406.6, Profit=-595.35, GroupName=W-Default, Leverage=200, TradeFee=0.0, CreateTime=2017-08-09T06:55:28.14, Status=1, IsReal=0, Credit=0.0}]}"; JSONObject jsonObject= JSON.parseObject(object); JSONArray jsonArray=jsonObject.getJSONArray("rows"); JSONObject detail=jsonArray.getJSONObject(0); Double equity=detail.getDouble("Equity");
fastJson對於json格式字符串的解析主要用到了一下三個類:java
JSON:fastJson的解析器,用於JSON格式字符串與JSON對象及javaBean之間的轉換。json
JSONObject:fastJson提供的json對象。數組
JSONArray:fastJson提供json數組對象。ui
此外,因爲JSONObject和JSONArray繼承了JSON,因此說也能夠直接使用二者對JSON格式字符串與JSON對象及javaBean之間作轉換,不過爲了不混淆咱們仍是使用JSON。this
首先定義三個json格式的字符串,做爲咱們的數據源。code
//json字符串-簡單對象型 private static final String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}"; //json字符串-數組類型 private static final String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]"; //複雜格式json字符串 private static final String COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
示例1.1-json字符串-簡單對象型與JSONObject之間的轉換htm
/** * json字符串-簡單對象型與JSONObject之間的轉換 */ public static void testJSONStrToJSONObject(){ JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR); //JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); //由於JSONObject繼承了JSON,因此這樣也是能夠的 System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge")); }
示例1.2-json字符串-數組類型與JSONArray之間的轉換對象
/** * json字符串-數組類型與JSONArray之間的轉換 */ public static void testJSONStrToJSONArray(){ JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR); //JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);//由於JSONArray繼承了JSON,因此這樣也是能夠的 //遍歷方式1 int size = jsonArray.size(); for (int i = 0; i < size; i++){ JSONObject jsonObject = jsonArray.getJSONObject(i); System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge")); } //遍歷方式2 for (Object obj : jsonArray) { JSONObject jsonObject = (JSONObject) obj; System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge")); } }
示例1.3-複雜json格式字符串與JSONObject之間的轉換blog
/** * 複雜json格式字符串與JSONObject之間的轉換 */ public static void testComplexJSONStrToJSONObject(){ JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR); //JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);//由於JSONObject繼承了JSON,因此這樣也是能夠的 String teacherName = jsonObject.getString("teacherName"); Integer teacherAge = jsonObject.getInteger("teacherAge"); JSONObject course = jsonObject.getJSONObject("course"); JSONArray students = jsonObject.getJSONArray("students"); }
首先,咱們針對數據源所示的字符串,提供三個javaBean。
public class Student { private String studentName; private Integer studentAge; public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public Integer getStudentAge() { return studentAge; } public void setStudentAge(Integer studentAge) { this.studentAge = studentAge; } }
public class Course { private String courseName; private Integer code; public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } }
public class Teacher { private String teacherName; private Integer teacherAge; private Course course; private List<Student> students; public String getTeacherName() { return teacherName; } public void setTeacherName(String teacherName) { this.teacherName = teacherName; } public Integer getTeacherAge() { return teacherAge; } public void setTeacherAge(Integer teacherAge) { this.teacherAge = teacherAge; } public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } }
json字符串與javaBean之間的轉換推薦使用 TypeReference
示例2.1-json字符串-簡單對象型與javaBean之間的轉換
/** * json字符串-簡單對象與JavaBean_obj之間的轉換 */ public static void testJSONStrToJavaBeanObj(){ Student student = JSON.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {}); //Student student1 = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});//由於JSONObject繼承了JSON,因此這樣也是能夠的 System.out.println(student.getStudentName()+":"+student.getStudentAge()); }
示例2.2-json字符串-數組類型與javaBean之間的轉換
/** * json字符串-數組類型與JavaBean_List之間的轉換 */ public static void testJSONStrToJavaBeanList(){ ArrayList<Student> students = JSON.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {}); //ArrayList<Student> students1 = JSONArray.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});//由於JSONArray繼承了JSON,因此這樣也是能夠的 for (Student student : students) { System.out.println(student.getStudentName()+":"+student.getStudentAge()); } }
示例2.3-複雜json格式字符串與與javaBean之間的轉換
/** * 複雜json格式字符串與JavaBean_obj之間的轉換 */ public static void testComplexJSONStrToJavaBean(){ Teacher teacher = JSON.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {}); //Teacher teacher1 = JSON.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {});//由於JSONObject繼承了JSON,因此這樣也是能夠的 String teacherName = teacher.getTeacherName(); Integer teacherAge = teacher.getTeacherAge(); Course course = teacher.getCourse(); List<Student> students = teacher.getStudents(); }
對於TypeReference
此外的:
1,對於JSON對象與JSON格式字符串的轉換能夠直接用 toJSONString()這個方法。
2,javaBean與JSON格式字符串之間的轉換要用到:JSON.toJSONString(obj);
3,javaBean與json對象間的轉換使用:JSON.toJSON(obj),而後使用強制類型轉換,JSONObject或者JSONArray。