java mapper.readtree_jackson readTree

String jsonstr = "{\"msg\":{\"head\":{\"version\":\"1.0\",\"bizcode\":\"1006\",\"senddate\":\"20140827\",\"sendtime\":\"110325\",\"seqid\":\"1\"},\"body\":{\"datalist\":\"wahaha\",\"rstcode\":\"000000\",\"rstmsg\":\"成功\"}}}";

ObjectMapper mapper = new ObjectMapper();

//允许出现特殊字符和转义符

mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true) ;

JsonNode root = mapper.readTree(jsonstr);

//path与get作用相同,但是当找不到该节点的时候,返回missing node而不是Null.

JsonNode msg = root.path("msg");

JsonNode head = msg.path("head");

JsonNode body = msg.path("body");

String bizcode = head.path("bizcode").asText();

String datalist = body.path("datalist").asText();

System.err.println(bizcode);

System.err.println(datalist);

System.err.println(root.path("msg").path("body").path("datalist").asText());

try {

ObjectMapper mapper = new ObjectMapper();

// 允许出现特殊字符和转义符

mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);

// String jsonstr =

// "{\"msg\":{\"head\":{\"version\":\"1.0\",\"bizcode\":\"1006\",\"senddate\":\"20140827\",\"sendtime\":\"110325\",\"seqid\":\"1\"},\"body\":{\"datalist\":\"wahaha\",\"rstcode\":\"000000\",\"rstmsg\":\"成功\"}}}";

ObjectNode root = mapper.createObjectNode();

ObjectNode msg = mapper.createObjectNode();

ObjectNode head = mapper.createObjectNode();

head.put("version", "1.0");

head.put("bizcode", "1006");

head.put("senddate", "20140827");

head.put("sendtime", "110325");

head.put("seqid", "1");

ObjectNode body = mapper.createObjectNode();

body.put("datalist", "wahaha");

body.put("rstcode", "000000");

body.put("rstmsg", "成功");

msg.put("head", head);

msg.put("body", body);

root.put("msg", msg);

System.out.println(mapper.writeValueAsString(root));

} catch (Exception e) {

e.printStackTrace();

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值