要解析的Json字符串:
{"item":[{"trainCode":"K1130/K1131","startStation":"烟台","arriveStation":"郑州","startTime":"15:30","endTime":"08:33","takeTime":"17小时3分","trainGrade":"快速","mileage":"1023KM","hard-seat":"--","hard-seat-prc":"150","hard-sleeper1":"--","hard-sleeper1-prc":"256","hard-sleeper2":"--","hard-sleeper2-prc":"265","hard-sleeper3":"--","hard-sleeper3-prc":"274","soft-sleeper1":"--","soft-sleeper1-prc":"400","soft-sleeper2":"--","soft-sleeper2-prc":"417"},{"trainCode":"K1162/K1159","startStation":"烟台","arriveStation":"郑州","startTime":"09:03","endTime":"00:02","takeTime":"14小时59分","trainGrade":"快速","mileage":"899KM","hard-seat":"--","hard-seat-prc":"125","hard-sleeper1":"--","hard-sleeper1-prc":"216","hard-sleeper2":"--","hard-sleeper2-prc":"224","hard-sleeper3":"--","hard-sleeper3-prc":"231","soft-sleeper1":"--","soft-sleeper1-prc":"337","soft-sleeper2":"--","soft-sleeper2-prc":"352"}],"title":"烟台 - 郑州 共2次车:"}
格式分析:
{
"item" : "" ,"title" : ""
}
“item”:[{"":"","":"",...},{"":"","":"",...}...]
创建json对象对应的Java对象:
首先把整个json对象对应于java中Train对象
新建类:Train.java
public class Train {
private Item[] item;
private String title;
public Item[] getItem() {
return item;
}
public void setItem(Item[] item) {
this.item = item;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
新建Item.java
public class Item {
private String trainCode;
private String startStation;
private String arriveStation;
private String startTime;
private String endTime;
private String takeTime;
private String trainGrade;
private String mileage;
private String hard_seat;
private String hard_seat_prc;
private String hard_sleeper1;
private String hard_sleeper1_prc;
private String hard_sleeper2;
private String hard_sleeper2_prc;
private String hard_sleeper3;
private String hard_sleeper3_prc;
private String soft_sleeper1;
private String soft_sleeper1_prc;
private String soft_sleeper2;
private String soft_sleeper2_prc;
public String getTrainCode() {
return trainCode;
}
public void setTrainCode(String trainCode) {
this.trainCode = trainCode;
}
public String getStartStation() {
return startStation;
}
public void setStartStation(String startStation) {
this.startStation = startStation;
}
public String getArriveStation() {
return arriveStation;
}
public void setArriveStation(String arriveStation) {
this.arriveStation = arriveStation;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getTakeTime() {
return takeTime;
}
public void setTakeTime(String takeTime) {
this.takeTime = takeTime;
}
public String getTrainGrade() {
return trainGrade;
}
public void setTrainGrade(String trainGrade) {
this.trainGrade = trainGrade;
}
public String getMileage() {
return mileage;
}
public void setMileage(String mileage) {
this.mileage = mileage;
}
public String getHard_seat() {
return hard_seat;
}
public void setHard_seat(String hardSeat) {
hard_seat = hardSeat;
}
public String getHard_seat_prc() {
return hard_seat_prc;
}
public void setHard_seat_prc(String hardSeatPrc) {
hard_seat_prc = hardSeatPrc;
}
public String getHard_sleeper1() {
return hard_sleeper1;
}
public void setHard_sleeper1(String hardSleeper1) {
hard_sleeper1 = hardSleeper1;
}
public String getHard_sleeper1_prc() {
return hard_sleeper1_prc;
}
public void setHard_sleeper1_prc(String hardSleeper1Prc) {
hard_sleeper1_prc = hardSleeper1Prc;
}
public String getHard_sleeper2() {
return hard_sleeper2;
}
public void setHard_sleeper2(String hardSleeper2) {
hard_sleeper2 = hardSleeper2;
}
public String getHard_sleeper2_prc() {
return hard_sleeper2_prc;
}
public void setHard_sleeper2_prc(String hardSleeper2Prc) {
hard_sleeper2_prc = hardSleeper2Prc;
}
public String getHard_sleeper3() {
return hard_sleeper3;
}
public void setHard_sleeper3(String hardSleeper3) {
hard_sleeper3 = hardSleeper3;
}
public String getHard_sleeper3_prc() {
return hard_sleeper3_prc;
}
public void setHard_sleeper3_prc(String hardSleeper3Prc) {
hard_sleeper3_prc = hardSleeper3Prc;
}
public String getSoft_sleeper1() {
return soft_sleeper1;
}
public void setSoft_sleeper1(String softSleeper1) {
soft_sleeper1 = softSleeper1;
}
public String getSoft_sleeper1_prc() {
return soft_sleeper1_prc;
}
public void setSoft_sleeper1_prc(String softSleeper1Prc) {
soft_sleeper1_prc = softSleeper1Prc;
}
public String getSoft_sleeper2() {
return soft_sleeper2;
}
public void setSoft_sleeper2(String softSleeper2) {
soft_sleeper2 = softSleeper2;
}
public String getSoft_sleeper2_prc() {
return soft_sleeper2_prc;
}
public void setSoft_sleeper2_prc(String softSleeper2Prc) {
soft_sleeper2_prc = softSleeper2Prc;
}
}
Item类中的各个成员变量对应着json中item对象中的各个属性。
这样Json对象和Java对象的对应关系类就写好了。要保证字段,类型一致。
测试类:test.java
import net.sf.json.JSONObject;
import com.hoo.entity.Item;
import com.hoo.entity.Train;
/**
* @author zcshun
* @see
* @date 2012-6-27 上午09:56:07
*/
public class test {
private JSONObject jsonObject = null;
private String json_yuan = "{\"item\":[{\"trainCode\":\"K1130/K1131\",\"startStation\":\"烟台\",\"arriveStation\":\"郑州\",\"startTime\":\"15:30\",\"endTime\":\"08:33\",\"takeTime\":\"17小时3分\",\"trainGrade\":\"快速\",\"mileage\":\"1023KM\",\"hard-seat\":\"--\",\"hard-seat-prc\":\"150\",\"hard-sleeper1\":\"--\",\"hard-sleeper1-prc\":\"256\",\"hard-sleeper2\":\"--\",\"hard-sleeper2-prc\":\"265\",\"hard-sleeper3\":\"--\",\"hard-sleeper3-prc\":\"274\",\"soft-sleeper1\":\"--\",\"soft-sleeper1-prc\":\"400\",\"soft-sleeper2\":\"--\",\"soft-sleeper2-prc\":\"417\"},{\"trainCode\":\"K1162/K1159\",\"startStation\":\"烟台\",\"arriveStation\":\"郑州\",\"startTime\":\"09:03\",\"endTime\":\"00:02\",\"takeTime\":\"14小时59分\",\"trainGrade\":\"快速\",\"mileage\":\"899KM\",\"hard-seat\":\"--\",\"hard-seat-prc\":\"125\",\"hard-sleeper1\":\"--\",\"hard-sleeper1-prc\":\"216\",\"hard-sleeper2\":\"--\",\"hard-sleeper2-prc\":\"224\",\"hard-sleeper3\":\"--\",\"hard-sleeper3-prc\":\"231\",\"soft-sleeper1\":\"--\",\"soft-sleeper1-prc\":\"337\",\"soft-sleeper2\":\"--\",\"soft-sleeper2-prc\":\"352\"}],\"title\":\"烟台 - 郑州 共2次车:\"}";
private String json = json_yuan.replaceAll("-", "_");//因为‘-‘为java中的关键字,不能用到变量名中,因此把所有的-替换成_。
public void readJSON2Bean() {
jsonObject = JSONObject.fromObject(json);
Train train = (Train) JSONObject.toBean(jsonObject, Train.class);
System.out.println(train.getTitle());
Item[] item = train.getItem();
for(int i=0;i<item.length;i++){
System.out.println(item[i].getTrainCode());
System.out.println(item[i].getArriveStation());
}
}
public static void main(String args[]) {
test t = new test();
t.readJSON2Bean();
}
}
导入json-lib用到的所有的jar包