后台作为传输数据的格式,将数据传输到客户端。JSON有两种格式,一种是对象格式的,另一种是数组格式的。
下面是一组json字符串:
String json="{"resultcode":"200","reason":"Return Successd!","result":{"province":"北京","city":"","areacode":"010",
"zip":"100000","company":"联通","card":""},"error_code":0}"
1.我们按json原生解析的方法一步一步进行解析:
JSONObject jsonObject = new JSONObject(json);
JSONObject object = jsonObject.getJSONObject("result");
String province=object.getString("province");
String city=object.getString("city");
String areacode=object.getString("areacode");
public class Root {
private String resultcode;
private String reason;
private T result;
private int error_code;
public void setResultcode(String resultcode){
this.resultcode = resultcode;
}
public String getResultcode(){
return this.resultcode;
}
public void setReason(String reason){
this.reason = reason;
}
public String getReason(){
return this.reason;
}
public void setResult(T result){
this.result = result;
}
public T getResult(){
return this.result;
}
public void setError_code(int error_code){
this.error_code = error_code;
}
public int getError_code(){
return this.error_code;
}
}
public class result {
private String province;
private String city;
private String areacode;
private String zip;
private String company;
private String card;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getAreacode() {
return areacode;
}
public void setAreacode(String areacode) {
this.areacode = areacode;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getCard() {
return card;
}
public void setCard(String card) {
this.card = card;
}
}
JSONObject jsonObject=JSONObject.fromObject(json);
Root root=(Root)JSONObject.toBean(jsonObject, Root.class);
Root root=JSONObject.parseObject(json,Root.class);
Result result =JSONObject.toJavaObject(root.getResult(),Result.class);
使用GSON进行转换:对于有泛型引入的,需要多写一句话用于获取泛型信息。
Gson gson=new Gson();
Type userType = new TypeToken>(){}.getType();//用于获取泛型信息
Root root=gson.fromJson(json, userType);
Result result=root.getResult();
String json="{"resultcode":"200","reason":"Return Successd!","result":[{"province":"北京","city":"","areacode":"010",
"zip":"100000","company":"联通","card":""},{"province":"北京","city":"","areacode":"010",
"zip":"100000","company":"联通","card":""}],"error_code":0}"
Gson gson=new Gson();
Type userType = new TypeToken>>(){}.getType();
Root> root=gson.fromJson(json, userType);
List result=root.getResult();
使用FastJson进行转换:
Root root=JSONObject.parseObject(json,Root.class);
List result =JSONObject.parseArray(root.getResult().toJSONString(),Result.class);
将json数组转换为list对象:
String str="[{"name":"真实服务器","ip":"101.37.168.121","port":5672,"username":"shanghu","password":"GY5P20u1ix9vK8DI","isdemo":false},
{"name":"模拟服务器","ip":"101.37.34.221","port":5672,"username":"shanghu","password":"GY5P20u1ix9vK8DI","isdemo":true}]";
使用GSON进行转换:
Gson gson=new Gson();
Service[] array = gson.fromJson(str, Service[].class);
List service=Arrays.asList(array);
使用FastJson进行转换:
List service=JSONArray.parseArray(str, Service.class);