java 對象與json互轉,用到了谷歌(Google)的Gson類庫。
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
class JsonOpen {
private List<Zone> zones;
public List<Zone> getZones() {
return zones;
}
public void setZones(List<Zone> zones) {
this.zones = zones;
}
}
class Zone {
private List<CardObject> cards;
public List<CardObject> getCards() {
return cards;
}
public void setCards(List<CardObject> cards) {
this.cards = cards;
}
}
class CardObject {
private String zone;
private String line;
private String row;
private String batch;
private String number;
private String path;
public String getZone() {
return zone;
}
public void setZone(String zone) {
this.zone = zone;
}
public String getLine() {
return line;
}
public void setLine(String line) {
this.line = line;
}
public String getRow() {
return row;
}
public void setRow(String row) {
this.row = row;
}
public String getBatch() {
return batch;
}
public void setBatch(String batch) {
this.batch = batch;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
/**
* 測試java對象和json轉換
*
* @author lxl
* @version 1.0
* @created 2015-08-20 16:54:26
* @history
* @see
*/
public class Test {
public static void main(String[] args) {
// toJson();
toObject();
}
public static void toJson(){
List<CardObject> cards = new ArrayList<CardObject>();
CardObject card = new CardObject();
card.setZone("A");
card.setLine("A");
card.setRow("1");
card.setBatch("15315AA020");
card.setNumber("008100000803");
card.setPath("");
cards.add(card);
CardObject card2 = new CardObject();
card2.setZone("A");
card2.setLine("B");
card2.setRow("1");
card2.setBatch("15315AA020");
card2.setNumber("008100000803");
card2.setPath("");
cards.add(card2);
CardObject card3 = new CardObject();
card3.setZone("A");
card3.setLine("C");
card3.setRow("1");
card3.setBatch("15315AA020");
card3.setNumber("008100000803");
card3.setPath("");
cards.add(card3);
Zone zone = new Zone();
zone.setCards(cards);
List<Zone> zones = new ArrayList<Zone>();
zones.add(zone);
JsonOpen jo = new JsonOpen();
jo.setZones(zones);
Gson gson = new GsonBuilder().create();
String result = gson.toJson(jo);
System.out.println(result);
}
public static void toObject(){
String json = "{'zones':[{'cards':[{'zone':'A','line':'A','row':'1','batch':'15315AA020','number':'008100000803','path':''}," +
"{'zone':'A','line':'B','row':'1','batch':'15315AA020','number':'008100000803','path':''}," +
"{'zone':'A','line':'C','row':'1','batch':'15315AA020','number':'008100000803','path':''}," +
"{'zone':'A','line':'D','row':'1','batch':'15315AA020','number':'008100000803','path':''}," +
"{'zone':'A','line':'E','row':'1','batch':'15315AA020','number':'008100000803','path':''}]}]}";
Gson gson = new Gson();
JsonOpen jo = gson.fromJson(json, JsonOpen.class);
System.out.println(jo.getZones().size());
System.out.println(jo.getZones().get(0).getCards().size());
for (int i = 0; i < jo.getZones().size(); i++) {
List<CardObject> cards = jo.getZones().get(i).getCards();
for (int j = 0; j < cards.size(); j++) {
System.out.println("zone:" + cards.get(j).getZone());
System.out.println("line:" + cards.get(j).getLine());
System.out.println("row:" + cards.get(j).getRow());
System.out.println("batch:" + cards.get(j).getBatch());
System.out.println("number:" + cards.get(j).getNumber());
System.out.println("path:" + cards.get(j).getPath());
}
}
}
}