示例解释:
通过使用JSONObject.parseObject(json, 类名.class)进行json数据的解析,实体类解析对象可根据Json数据的对象类型进行定义,可嵌套多层对象关系进行解析,注意相应的json数据对象层级结构即可。
Json Jar包(maven依赖):
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
Json数据:
{
"status": "1",
"regeocode": {
"addressComponent": {
"city": "武汉市",
"province": "湖北省",
"adcode": "420106",
"district": "武昌区",
"towncode": "420106006000",
"streetNumber": {
"number": "46号",
"location": "114.300143,30.5515561",
"direction": "东北",
"distance": "32.3008",
"street": "青石桥"
},
"country": "中国",
"township": "中华路街街道",
"businessAreas": [
{
"location": "114.300581,30.547575",
"name": "司门口",
"id": "420106"
},
{
"location": "114.305741,30.583179",
"name": "江滩",
"id": "420102"
},
{
"location": "114.31082,30.560542",
"name": "积玉桥",
"id": "420106"
}
],
"building": {
"name": [
],
"type": [
]
},
"neighborhood": {
"name": [
],
"type": [
]
},
"citycode": "027"
},
"formatted_address": "湖北省武汉市武昌区中华路街街道银聚园小区南区武昌廉政文化公园"
},
"info": "OK",
"infocode": "10000"
}
解析代码操作类:
import com.alibaba.fastjson.JSONObject;
/**
* @ClassName: GaoDeStruct
* @Description: TODO
* @author: mzb
* @date: 2020年3月18日 下午3:57:34
*/
public class GaoDeStruct {
private String status;
private String info;
private String infocode;
private Regeocode regeocode;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getInfocode() {
return infocode;
}
public void setInfocode(String infocode) {
this.infocode = infocode;
}
public Regeocode getRegeocode() {
return regeocode;
}
public void setRegeocode(Regeocode regeocode) {
this.regeocode = regeocode;
}
public static void main(String[] args) {
String json = "{" +
" \"status\": \"1\"," +
" \"regeocode\": {" +
" \"addressComponent\": {" +
" \"city\": \"武汉市\"," +
" \"province\": \"湖北省\"," +
" \"adcode\": \"420106\"," +
" \"district\": \"武昌区\"," +
" \"towncode\": \"420106006000\"," +
" \"streetNumber\": {" +
" \"number\": \"46号\"," +
" \"location\": \"114.300143,30.5515561\"," +
" \"direction\": \"东北\"," +
" \"distance\": \"32.3008\"," +
" \"street\": \"青石桥\"" +
" }," +
" \"country\": \"中国\"," +
" \"township\": \"中华路街街道\"," +
" \"businessAreas\": [" +
" {" +
" \"location\": \"114.300581,30.547575\"," +
" \"name\": \"司门口\"," +
" \"id\": \"420106\"" +
" }," +
" {" +
" \"location\": \"114.305741,30.583179\"," +
" \"name\": \"江滩\"," +
" \"id\": \"420102\"" +
" }," +
" {" +
" \"location\": \"114.31082,30.560542\"," +
" \"name\": \"积玉桥\"," +
" \"id\": \"420106\"" +
" }" +
" ]," +
" \"building\": {" +
" \"name\": [" +
" " +
" ]," +
" \"type\": [" +
" " +
" ]" +
" }," +
" \"neighborhood\": {" +
" \"name\": [" +
" " +
" ]," +
" \"type\": [" +
" " +
" ]" +
" }," +
" \"citycode\": \"027\"" +
" }," +
" \"formatted_address\": \"湖北省武汉市武昌区中华路街街道银聚园小区南区武昌廉政文化公园\"" +
" }," +
" \"info\": \"OK\"," +
" \"infocode\": \"10000\"" +
"}";
AddressComponent address = parse(json);
System.out.println(address);
//对比一下打印出来的信息如果在你本来的json字符串一样就表示正确了
// System.out.println(JSONObject.toJSONString(address));
}
public static AddressComponent parse(String json) {
GaoDeStruct struct = JSONObject.parseObject(json, GaoDeStruct.class);
return struct.getRegeocode().getAddressComponent();
}
}
Json数据封装对象类:
import java.util.List;
public class AddressComponent {
private String city;
private String province;
private String adcode;
private String district;
private String towncode;
private StreetNumber streetNumber;
private String country;
private String township;
private List<BusinessArea> businessAreas;
private Building building;
private Neighborhood neighborhood;
private String citycode;
//get/set/equals/hashCode/toString自行补充
}
import java.util.List;
public class Building {
private List<String> name;
private List<String> type;
//get/set/equals/hashCode/toString自行补充
}
public class BusinessArea {
private String location;
private String name;
private String id;
//get/set/equals/hashCode/toString自行补充
}
import java.util.List;
public class Neighborhood {
private List<String> name;
private List<String> type;
//get/set/equals/hashCode/toString自行补充
}
public class Regeocode {
private AddressComponent addressComponent;
private String formatted_address;
//get/set/equals/hashCode/toString自行补充
}
public class StreetNumber {
private String number;
private String location;
private String direction;
private String distance;
private String street;
//get/set/equals/hashCode/toString自行补充
}