qq_33530692 2016-01-27 03:34 采纳率: 100%
浏览 3501
已采纳

如何最简单的用java解析这个json文件

小弟新人,不知道怎么解析dhl网站传回来的json文件,有代码最好,谢谢啦
{
"results" : [ {
"id" : "6582515623",
"label" : "运单",
"type" : "airwaybill",
"duplicate" : false,
"delivery" : {
"code" : "101",
"status" : "delivered"
},
"origin" : {
"value" : "SHANGHAI - SHANGHAI - CHINA, PEOPLES REPUBLIC",
"label" : "发件地服务区域",
"url" : "http://www.cn.dhl.com/en/country_profile.html"
},
"destination" : {
"value" : "KUALA LUMPUR - 40470 SHAH ALAM SELANGOR DARUL EHSA - MALAYSIA",
"label" : "目的地服务区域",
"url" : "http://www.dhl.com.my/en/country_profile.html"
},
"description" : "已签收: MS SANDRA 星期三, 一月 06, 2016 于 13:54",
"hasDuplicateShipment" : false,
"signature" : {
"link" : {
"url" : "https://webpod.dhl.com/webPOD/DHLePODRequest?hwb=H%2B8q%2Bsy%2BDgBfBxY4TPyQHQ%3D%3D&pudate=yHkb%2Fogas7GpGJE7%2ByaM0A%3D%3D&appuid=9u1%2BsSO1fsdK7F6MTNnRjg%3D%3D&language=zh&country=CN",
"label" : "获取电子签收凭证"
},
"type" : "epod",
"description" : "星期三, 一月 06, 2016 于 13:54",
"signatory" : "MS SANDRA",
"label" : "已签收",
"help" : "help"
},
"pieces" : {
"value" : 1,
"label" : "件",
"showSummary" : true,
"pIds" : [ "JD014600001868677611" ]
},
"checkpoints" : [ {
"counter" : 4,
"description" : "快件已完成清关手续并从海关放行 EAST CHINA AREA - CHINA, PEOPLES REPUBLIC",
"time" : "21:55",
"date" : "星期一, 一月 04, 2016 ",
"location" : "EAST CHINA AREA - CHINA, PEOPLES REPUBLIC"
}, {
"counter" : 3,
"description" : "离开转运地 SHANGHAI - CHINA, PEOPLES REPUBLIC",
"time" : "21:47",
"date" : "星期一, 一月 04, 2016 ",
"location" : "SHANGHAI - CHINA, PEOPLES REPUBLIC",
"totalPieces" : 1,
"pIds" : [ "JD014600001868677611" ]
}, {
"counter" : 2,
"description" : "正在(已经)安排下一站的转运 SHANGHAI - CHINA, PEOPLES REPUBLIC",
"time" : "21:42",
"date" : "星期一, 一月 04, 2016 ",
"location" : "SHANGHAI - CHINA, PEOPLES REPUBLIC",
"totalPieces" : 1,
"pIds" : [ "JD014600001868677611" ]
}, {
"counter" : 1,
"description" : "快件已从发件人处提取",
"time" : "20:20",
"date" : "星期一, 一月 04, 2016 ",
"location" : "SHANGHAI - CHINA, PEOPLES REPUBLIC"
} ],
"checkpointLocationLabel" : "位置",
"checkpointTimeLabel" : "时间"
} ]
}

  • 写回答

5条回答 默认 最新

  • 林深 2016-01-27 04:20
    关注

    可以拆分成下面这几个model

     =================================
    
    package ;
    public class Delivery {
    private String code;
    
    private String status;
    
    public void setCode(String code){
    this.code = code;
    }
    public String getCode(){
    return this.code;
    }
    public void setStatus(String status){
    this.status = status;
    }
    public String getStatus(){
    return this.status;
    }
    
    }
    =================================
    
    package ;
    public class Origin {
    private String value;
    
    private String label;
    
    private String url;
    
    public void setValue(String value){
    this.value = value;
    }
    public String getValue(){
    return this.value;
    }
    public void setLabel(String label){
    this.label = label;
    }
    public String getLabel(){
    return this.label;
    }
    public void setUrl(String url){
    this.url = url;
    }
    public String getUrl(){
    return this.url;
    }
    
    }
    =================================
    
    package ;
    public class Destination {
    private String value;
    
    private String label;
    
    private String url;
    
    public void setValue(String value){
    this.value = value;
    }
    public String getValue(){
    return this.value;
    }
    public void setLabel(String label){
    this.label = label;
    }
    public String getLabel(){
    return this.label;
    }
    public void setUrl(String url){
    this.url = url;
    }
    public String getUrl(){
    return this.url;
    }
    
    }
    =================================
    
    package ;
    public class Link {
    private String url;
    
    private String label;
    
    public void setUrl(String url){
    this.url = url;
    }
    public String getUrl(){
    return this.url;
    }
    public void setLabel(String label){
    this.label = label;
    }
    public String getLabel(){
    return this.label;
    }
    
    }
    =================================
    
    package ;
    public class Signature {
    private Link link;
    
    private String type;
    
    private String description;
    
    private String signatory;
    
    private String label;
    
    private String help;
    
    public void setLink(Link link){
    this.link = link;
    }
    public Link getLink(){
    return this.link;
    }
    public void setType(String type){
    this.type = type;
    }
    public String getType(){
    return this.type;
    }
    public void setDescription(String description){
    this.description = description;
    }
    public String getDescription(){
    return this.description;
    }
    public void setSignatory(String signatory){
    this.signatory = signatory;
    }
    public String getSignatory(){
    return this.signatory;
    }
    public void setLabel(String label){
    this.label = label;
    }
    public String getLabel(){
    return this.label;
    }
    public void setHelp(String help){
    this.help = help;
    }
    public String getHelp(){
    return this.help;
    }
    
    }
    =================================
    
    package ;
    public class PIds {
    
    }
    =================================
    
    package ;
    import java.util.List;
    public class Pieces {
    private int value;
    
    private String label;
    
    private boolean showSummary;
    
    private List<pIds> pIds ;
    
    public void setValue(int value){
    this.value = value;
    }
    public int getValue(){
    return this.value;
    }
    public void setLabel(String label){
    this.label = label;
    }
    public String getLabel(){
    return this.label;
    }
    public void setShowSummary(boolean showSummary){
    this.showSummary = showSummary;
    }
    public boolean getShowSummary(){
    return this.showSummary;
    }
    public void setPIds(List<pIds> pIds){
    this.pIds = pIds;
    }
    public List<pIds> getPIds(){
    return this.pIds;
    }
    
    }
    =================================
    
    package ;
    public class Checkpoints {
    private int counter;
    
    private String description;
    
    private String time;
    
    private String date;
    
    private String location;
    
    public void setCounter(int counter){
    this.counter = counter;
    }
    public int getCounter(){
    return this.counter;
    }
    public void setDescription(String description){
    this.description = description;
    }
    public String getDescription(){
    return this.description;
    }
    public void setTime(String time){
    this.time = time;
    }
    public String getTime(){
    return this.time;
    }
    public void setDate(String date){
    this.date = date;
    }
    public String getDate(){
    return this.date;
    }
    public void setLocation(String location){
    this.location = location;
    }
    public String getLocation(){
    return this.location;
    }
    
    }
    =================================
    
    package ;
    import java.util.List;
    public class Results {
    private String id;
    
    private String label;
    
    private String type;
    
    private boolean duplicate;
    
    private Delivery delivery;
    
    private Origin origin;
    
    private Destination destination;
    
    private String description;
    
    private boolean hasDuplicateShipment;
    
    private Signature signature;
    
    private Pieces pieces;
    
    private List<checkpoints> checkpoints ;
    
    private String checkpointLocationLabel;
    
    private String checkpointTimeLabel;
    
    public void setId(String id){
    this.id = id;
    }
    public String getId(){
    return this.id;
    }
    public void setLabel(String label){
    this.label = label;
    }
    public String getLabel(){
    return this.label;
    }
    public void setType(String type){
    this.type = type;
    }
    public String getType(){
    return this.type;
    }
    public void setDuplicate(boolean duplicate){
    this.duplicate = duplicate;
    }
    public boolean getDuplicate(){
    return this.duplicate;
    }
    public void setDelivery(Delivery delivery){
    this.delivery = delivery;
    }
    public Delivery getDelivery(){
    return this.delivery;
    }
    public void setOrigin(Origin origin){
    this.origin = origin;
    }
    public Origin getOrigin(){
    return this.origin;
    }
    public void setDestination(Destination destination){
    this.destination = destination;
    }
    public Destination getDestination(){
    return this.destination;
    }
    public void setDescription(String description){
    this.description = description;
    }
    public String getDescription(){
    return this.description;
    }
    public void setHasDuplicateShipment(boolean hasDuplicateShipment){
    this.hasDuplicateShipment = hasDuplicateShipment;
    }
    public boolean getHasDuplicateShipment(){
    return this.hasDuplicateShipment;
    }
    public void setSignature(Signature signature){
    this.signature = signature;
    }
    public Signature getSignature(){
    return this.signature;
    }
    public void setPieces(Pieces pieces){
    this.pieces = pieces;
    }
    public Pieces getPieces(){
    return this.pieces;
    }
    public void setCheckpoints(List<checkpoints> checkpoints){
    this.checkpoints = checkpoints;
    }
    public List<checkpoints> getCheckpoints(){
    return this.checkpoints;
    }
    public void setCheckpointLocationLabel(String checkpointLocationLabel){
    this.checkpointLocationLabel = checkpointLocationLabel;
    }
    public String getCheckpointLocationLabel(){
    return this.checkpointLocationLabel;
    }
    public void setCheckpointTimeLabel(String checkpointTimeLabel){
    this.checkpointTimeLabel = checkpointTimeLabel;
    }
    public String getCheckpointTimeLabel(){
    return this.checkpointTimeLabel;
    }
    
    }
    =================================
    
    package ;
    import java.util.List;
    public class Root {
    private List<results> results ;
    
    public void setResults(List<results> results){
    this.results = results;
    }
    public List<results> getResults(){
    return this.results;
    }
    
    }
    

    解析的时候使用Gson的jar包

     Gson gson = new Gson();
     Result result = gson.fromJson(result,Result.class);
    
    

    最后给你几个json转java文件的链接:
    http://www.bejson.com/json2javapojo/
    http://www.jsonschema2pojo.org/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元