以下列举了几种常用方式:
Json例子:
{
"obj":{
"propImages":[
{
"Key":"1415",
"propTargetUrl":"http://115.29.238.140:8017/cs/images/1068/target",
"propOriginalUrl":"http://115.29.238.140:8017/cs/images/1068",
"fdPrimUrl":"http://115.29.238.140:8017/cs/images/1068",
"fdAutoId":1
},
{
"Key":"1416",
"propTargetUrl":"http://115.29.238.140:8017/cs/images/1069/target",
"propOriginalUrl":"http://115.29.238.140:8017/cs/images/1069",
"fdPrimUrl":"http://115.29.238.140:8017/cs/images/1069",
"fdAutoId":2
},
{
"Key":"1417",
"propTargetUrl":"http://115.29.238.140:8017/cs/images/1070/target",
"propOriginalUrl":"http://115.29.238.140:8017/cs/images/1070",
"fdPrimUrl":"http://115.29.238.140:8017/cs/images/1070",
"fdAutoId":3
},
{
"Key":"1418",
"propTargetUrl":"http://115.29.238.140:8017/cs/images/1071/target",
"propOriginalUrl":"http://115.29.238.140:8017/cs/images/1071",
"fdPrimUrl":"http://115.29.238.140:8017/cs/images/1071",
"fdAutoId":4
},
{
"Key":"1419",
"propTargetUrl":"http://115.29.238.140:8017/cs/images/1072/target",
"propOriginalUrl":"http://115.29.238.140:8017/cs/images/1072",
"fdPrimUrl":"http://115.29.238.140:8017/cs/images/1072",
"fdAutoId":5
},
{
"Key":"1420",
"propTargetUrl":"http://115.29.238.140:8017/cs/images/1073/target",
"propOriginalUrl":"http://115.29.238.140:8017/cs/images/1073",
"fdPrimUrl":"http://115.29.238.140:8017/cs/images/1073",
"fdAutoId":6
}
],
"propStoreSKU":[
{
"Id":26,
"Name":"颜色",
"Values":[
"金色",
"可定制",
"深棕色",
"银色"
],
"ItemIds":[
27,
42,
32,
37
],
"IsStore":true,
"ValuesString":"金色,可定制,深棕色,银色"
}
],
"propUnStoreSKU":[
],
"propSKUs":[
{
"Id":26,
"Name":"颜色",
"Values":[
"金色",
"可定制",
"深棕色",
"银色"
],
"ItemIds":[
27,
42,
32,
37
],
"IsStore":true,
"ValuesString":"金色,可定制,深棕色,银色"
}
],
"propPCUrl":"http://runde.pfbaba.com:8016/Product/Detail/204",
"propFullPrice":"面议",
"propPrice":"面议",
"propShortPrice":"面议",
"propIsNegotiable":true,
"propProperties":[
{
"Name":"尺码",
"Value":"85%"
},
{
"Name":"张幅",
"Value":"30-50尺"
},
{
"Name":"厚度",
"Value":"1.6-1.8mm"
},
{
"Name":"皮层",
"Value":"二层"
},
{
"Name":"纹路",
"Value":"西雅图"
}
],
"propStartTip":"1件起购",
"propClassifyName":"黄牛皮",
"propShopName":"润德皮革",
"propAppListImage":"http://115.29.238.140:8017/cs/images/1068/300x300",
"propCategory":null,
"propIsRecommendName":"未推荐",
"propIsPublishName":"已发布",
"propIsFreeShippingName":"买家付",
"fdProdId":204,
"fdProdShortId":105,
"fdProdClassifyId":15,
"fdProdName":"厂家直销大量现货 牛皮二层西雅图 各种颜色可定制 纹路清晰",
"fdProdImage":"http://115.29.238.140:8017/cs/images/1068",
"fdProdPrice":"面议",
"fdProdUnit":"平方英尺",
"fdProdStart":"1",
"fdProdSort":336,
"fdProdCreateAt":"2015-12-03 16:01:23",
"fdProdIsRecommend":0,
"fdProdFeature":"",
"fdProdSoftness":"",
"fdProdUses":"箱包等",
"fdProdIntroduce":"<p> </p> <p><img src="http://115.29.238.140:8017/plugin/upload/web/store/editor//attas/20151203/635847552820393542.jpg" alt="" /></p>",
"fdProdNotice":"",
"fdProdCollectionCount":0,
"fdProdShareCount":0,
"fdProdIsPublish":1,
"fdProdIsFreeShipping":0,
"fdProdWeight":0,
"fdProdFreightTemplateId":0,
"fdProdIsHot":1,
"fdProdSummary":"<p> </p> <p> </p>",
"fdProdOriginProvince":23,
"fdProdOriginCity":278,
"fdProdOrigin":"浙江温州市",
"fdProdCategory":null,
"fdProdBrand":"",
"fdProdBrandImg":"",
"fdProdPriceLow":null,
"fdProdPriceHigh":null,
"fdProdType":0,
"fdProdNegotiable":true,
"fdProdNum":"",
"fdAutoId":0
},
"property":{
"obj_name":"Product",
"is_list":false
},
"error":{
"err_msg":"",
"err_code":0,
"request_args":"id=204,data=null,="
}
}
首先你得有个单例全局类
/**
*
* @ClassName: JackJsonUtil
* @Description: JackJson 解析
* @author William
* @date 2016年3月31日 上午10:50:52
*
*/
public class JackJsonUtil {
private static final ObjectMapper mapper = new ObjectMapper();
private JackJsonUtil() {
}
public static ObjectMapper getInstance() {
return (mapper == null) ? new ObjectMapper() : mapper;
}
}
然后下面是几种常用的读取方式: 一般性JsonNode直接读取方式、Collection类型读取、Bean 持久转换型读取
<pre name="code" class="java"> try {
ObjectMapper objectMapper = JackJsonUtil.getInstance();
JsonNode jsonNode = objectMapper.readTree(result);
JsonNode objNode = jsonNode.get("obj");
JsonNode errorNode = jsonNode.get("error");
JsonNode propertyNode = jsonNode.get("property");
String personString = jsonNode.toString();
JsonNode propNode = objNode.get("propImages");
Log.e("\n\n============= Get JsonArray Directly ======================");
Iterator<JsonNode> propIterator = propNode.getElements();
while (propIterator.hasNext()) {
JsonNode jn = propIterator.next();
Log.e(jn.get("propOriginalUrl").toString());
}
Log.e("\n\n============= To Map ======================");
Map map_ = objectMapper.readValue(propNode.toString(), Map.class);
for (int i=0 ; i<map_.size(); i++) {
Iterator it = map_.entrySet().iterator();
while(it.hasNext()) {
Entry entry = (Entry) it.next();
Log.e(entry.getKey() + " : " + entry.getValue());
}
}
Log.e("\n\n============= To List ======================");
List propChilds = objectMapper.readValue(propNode.toString(), List.class);
for (int i=0 ; i<propChilds.size(); i++) {
Map map = (Map) propChilds.get(i);
Iterator it = map.entrySet().iterator();
while(it.hasNext()) {
Entry entry = (Entry) it.next();
Log.e(entry.getKey() + " : " + entry.getValue());
}
}
Log.e("\n\n============= To Bean ======================");
Error error = objectMapper.readValue(errorNode, Error.class);
Log.e("error.getErr_code() : " + error.getErr_code());
Log.e("error.getErr_msg() : " + error.getErr_msg());
Log.e("error.getRequest_args() : " + error.getRequest_args());
} catch (Exception e) {
return;
}
Error.java文件:
/**
*
* @ClassName: Error
* @Description: 接口错误返回信息
* @author William
* @date 2016年3月31日 上午11:40:17
*
*/
public class Error {
/**
* 错误信息
*/
private String err_msg;
/**
* 错误码
*/
private int err_code;
/**
* 请求参数
*/
private String request_args;
public String getErr_msg() {
return err_msg;
}
public void setErr_msg(String err_msg) {
this.err_msg = err_msg;
}
public int getErr_code() {
return err_code;
}
public void setErr_code(int err_code) {
this.err_code = err_code;
}
public String getRequest_args() {
return request_args;
}
public void setRequest_args(String request_args) {
this.request_args = request_args;
}
}