Java解析Json字符串--數組或列表
Json示例:
[
{
"age": 25,
"gender": "female",
"grades": "三班",
"name": "露西",
"weight": 51.3
},
{
"age": 26,
"gender": "male",
"grades": "三班",
"name": "傑克",
"weight": 66.5
},
{
"age": 25,
"gender": "female",
"grades": "三班",
"name": "莉莉",
"weight": 55
}
]
我們來解析一下這個Json字符串。
首先,因為此Json字符串最外邊是由一個中括弧”[]”包裹,那麼,最終我們會用數組或者列表來接收它。
接下來,我們能看到中括弧裡邊有三個同級的大括弧,並且每個大括弧裡包含的內容是相同的,大括弧我們可以對應創建一個類,我們創建一個類Student.java,並對應大括弧內的元素,定義相應的成員變量,生成get/set方法。
--------------------------------------分割線 --------------------------------------
[譯]JSON數據范式化(normalizr) http://www.linuxidc.com/Linux/2016-02/128288.htm
如何處理JSON中的特殊字符 http://www.linuxidc.com/Linux/2015-09/123067.htm
Struts中異步傳送XML和JSON類型的數據 http://www.linuxidc.com/Linux/2013-08/88247.htm
Linux下JSON庫的編譯及代碼測試 http://www.linuxidc.com/Linux/2013-03/81607.htm
jQuery 獲取JSON數據[$.getJSON方法] http://www.linuxidc.com/Linux/2013-03/81673.htm
用jQuery以及JSON包將表單數據轉為JSON字符串 http://www.linuxidc.com/Linux/2013-01/77560.htm
在C語言中解析JSON配置文件 http://www.linuxidc.com/Linux/2014-05/101822.htm
--------------------------------------分割線 --------------------------------------
我們生成的Student.java如下:
package com.bean;
/**
* 學生
*/
public class Student {
private int age;//年齡
private String gender;//性別,male/female
private String grades;//班級
private String name;//姓名
private float weight;//體重
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getGrades() {
return grades;
}
public void setGrades(String grades) {
this.grades = grades;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
}
解析成數組或列表:
package com.test;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import com.bean.Student;
public class Domain {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
String jsonStr = "[{\"age\": 25,\"gender\": \"female\",\"grades\": \"三班\",\"name\": \"露西\",\"weight\": 51.3},{\"age\": 26,\"gender\": \"male\",\"grades\": \"三班\",\"name\": \"傑克\",\"weight\": 66.5},{\"age\": 25,\"gender\": \"female\",\"grades\": \"三班\",\"name\": \"莉莉\",\"weight\": 55}]";
JSONArray jsonArray = JSONArray.fromObject(jsonStr);
Student[] stus = new Student[3];
List<Student> list = new ArrayList<Student>();
stus = (Student[]) JSONArray.toArray(jsonArray, Student.class);//轉換成數組
list = (List<Student>) JSONArray.toCollection(jsonArray, Student.class);//轉換成列表
}
}
通過數組或者列表生成Json字符串:
package com.test;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import com.bean.Student;
public class Domain {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Student stu1 = new Student();
Student stu2 = new Student();
Student stu3 = new Student();
stu1.setAge(25);
stu1.setGender("female");
stu1.setGrades("三班");
stu1.setName("露西");
stu1.setWeight(51.3f);
stu2.setAge(26);
stu2.setGender("male");
stu2.setGrades("三班");
stu2.setName("傑克");
stu2.setWeight(66.5f);
stu3.setAge(25);
stu3.setGender("female");
stu3.setGrades("三班");
stu3.setName("莉莉");
stu3.setWeight(55.0f);
Student[] stus = new Student[3];
List<Student> list = new ArrayList<Student>();
stus[0] = stu1;
stus[1] = stu2;
stus[2] = stu3;
list.add(stu1);
list.add(stu2);
list.add(stu3);
String jsonStr1 = JSONArray.fromObject(stus).toString();
String jsonStr2 = JSONArray.fromObject(list).toString();
System.out.println(jsonStr1);
System.out.println(jsonStr2);
}
}