开发者社区> 问答> 正文

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

小弟新人,不知道怎么解析dhl网站传回来的json文件,有代码最好,谢谢啦screenshot
screenshot

}

展开
收起
杨冬芳 2016-07-14 18:25:04 2524 0
1 条回答
写回答
取消 提交回答
  • IT从业

    可以拆分成下面这几个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 ;

    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){
    this.pIds = pIds;
    }
    public List 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 ;

    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){
    this.checkpoints = checkpoints;
    }
    public List 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 ;

    public void setResults(List results){
    this.results = results;
    }
    public List 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/

    2019-07-17 19:56:41
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
神龙云服务器产品及技术深度解析 立即下载
弹性创造价值:基于ECS的最佳性价比实践解析 立即下载
又快又稳:阿里云下一代虚拟交换机解析 立即下载

相关镜像