2 qq 33530692 qq_33530692 于 2016.01.27 11:34 提问

如何最简单的用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个回答

leilba
leilba   Rxr 2016.01.27 12: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/

leilba
leilba 回复qq_33530692: 就是将这个大的json转化成几个简单的由基础数据组成的数据模型,,将json里面的数据归归类,便于拆分和接口数据的修改
接近 2 年之前 回复
qq_33530692
qq_33530692 虽然还没看懂,但谢谢了
接近 2 年之前 回复
leilba
leilba 写错了,是:Root root = gson.fromJson(results, Root.class); //其中results是你的json字符串
接近 2 年之前 回复
bdmh
bdmh   Ds   Rxr 2016.01.27 11:37

JsonObject就可以,只不过你访问字段费劲,你可以定义一个和json结构一样的类,然后用gson包(谷歌的)直接转为类对象

qq_33530692
qq_33530692 大哥,有没具体怎么构造类,看这个文件对我来说有点复杂
接近 2 年之前 回复
91program
91program   Ds   Rxr 2016.01.27 11:42
plxiaopan
plxiaopan   2016.01.27 11:56

JsonObject就可以,如果想方便开发快使用Gson这个jar包,使用很方便,简单一句话,gson.fromJson(result,Type.class);
result : json 串
type : 实体类

QZC78
QZC78   2016.01.27 15:02

可以选择使用jackson-core-2.5.0.jar包,然后
ObjectMapper objMapper=new ObjectMapper();
List listObject=objMapper.readValue(jsonString,List.class);
for(Object obj:listObject){
Map<?,?> mapObj=(Map<?,?>)obj;
比如:String id=mapObj.get("id"));
}
Map<?,?> 具体情况可以根据你json的元素的使用

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!