import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class packet3 {
public static void main(String[] args) {
try {
List<Student> list = new ArrayList<Student>();
String json = "[{name:'张三',scores:{en:90,math:99,cn:89}},{name:'李四',scores:{en:80,math:90,cn:99}}]";
JSONArray array = new JSONArray(json);
Student student = null;
Score scores = null;
for (int i = 0; i < array.length(); i++) {
student = new Student();
JSONObject o1 = array.getJSONObject(i);
String name = o1.getString("name");
student.name = name;
JSONObject o2 = o1.getJSONObject("scores");
scores = new Score();
int en = o2.getInt("en");
int math = o2.getInt("math");
int cn = o2.getInt("cn");
scores.en = en;
scores.cn = cn;
scores.math = math;
student.scores = scores;
list.add(student);
}
System.out.println(list);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Student {
String name;
Score scores;
@Override
public String toString() {
return "Student [name=" + name + ", scores=" + scores + "]";
}
}
class Score {
int en;
int cn;
int math;
@Override
public String toString() {
return "Score [en=" + en + ", cn=" + cn + ", math=" + math + "]";
}
}
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class packet3 {
public static void main(String[] args) {
try {
List<Student> list = new ArrayList<Student>();
String json = "[{name:'张三',scores:{en:90,math:99,cn:89}},{name:'李四',scores:{en:80,math:90,cn:99}}]";
JSONArray array = new JSONArray(json);
Student student = null;
Score scores = null;
for (int i = 0; i < array.length(); i++) {
student = new Student();
JSONObject o1 = array.getJSONObject(i);
String name = o1.getString("name");
student.name = name;
JSONObject o2 = o1.getJSONObject("scores");
scores = new Score();
int en = o2.getInt("en");
int math = o2.getInt("math");
int cn = o2.getInt("cn");
scores.en = en;
scores.cn = cn;
scores.math = math;
student.scores = scores;
list.add(student);
}
System.out.println(list);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Student {
String name;
Score scores;
@Override
public String toString() {
return "Student [name=" + name + ", scores=" + scores + "]";
}
}
class Score {
int en;
int cn;
int math;
@Override
public String toString() {
return "Score [en=" + en + ", cn=" + cn + ", math=" + math + "]";
}
}