使用JSONObject.parseObject(String json, Class clazz)解析Json数据

使用JSONObject.parseObject(String json, Class clazz)解析Json数据示例解释 通过使用 JSONObject parseObject json 类名 class 进行 json 数据的解析 实体类解析对象可根据 Json 数据的对象类型进行定义 可嵌套多层对象关系进行解析 注意相应的 json 数据对象层级结构即可 Json Jar 包 maven 依赖

大家好,我是讯享网,很高兴认识大家。

示例解释:

通过使用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": "", "district": "武昌区", "towncode": "0", "streetNumber": { 
    "number": "46号", "location": "114.,30.", "direction": "东北", "distance": "32.3008", "street": "青石桥" }, "country": "中国", "township": "中华路街街道", "businessAreas": [ { 
    "location": "114.,30.", "name": "司门口", "id": "" }, { 
    "location": "114.,30.", "name": "江滩", "id": "" }, { 
    "location": "114.31082,30.", "name": "积玉桥", "id": "" } ], "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\": \"\"," + " \"district\": \"武昌区\"," + " \"towncode\": \"0\"," + " \"streetNumber\": {" + " \"number\": \"46号\"," + " \"location\": \"114.,30.\"," + " \"direction\": \"东北\"," + " \"distance\": \"32.3008\"," + " \"street\": \"青石桥\"" + " }," + " \"country\": \"中国\"," + " \"township\": \"中华路街街道\"," + " \"businessAreas\": [" + " {" + " \"location\": \"114.,30.\"," + " \"name\": \"司门口\"," + " \"id\": \"\"" + " }," + " {" + " \"location\": \"114.,30.\"," + " \"name\": \"江滩\"," + " \"id\": \"\"" + " }," + " {" + " \"location\": \"114.31082,30.\"," + " \"name\": \"积玉桥\"," + " \"id\": \"\"" + " }" + " ]," + " \"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自行补充 } 
小讯
上一篇 2025-01-29 11:43
下一篇 2025-02-24 13:49

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/55582.html