Java-Json解析--02

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 + "]";
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值