firemail
标题:
解析json json串转对象
[打印本页]
作者:
java
时间:
2017-8-7 18:12
标题:
解析json json串转对象
本帖最后由 java 于 2017-8-10 19:15 编辑
String jsonString = stringHttpResponse.getContent();
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode root = mapper.readTree(jsonString);
Integer code = Integer.valueOf(root.get("code").asText());
String msg = root.get("msg").asText();
JsonNode dataAd = root.get("data");
Integer adCount = Integer.valueOf(dataAd.get("adCount").asText());
JsonNode adList = dataAd.get("adList");
if (code == 0 && adCount == 1) {
eventToast.setUrl(adList.get(0).get("url").asText());
eventToast.setDeepLink(adList.get(0).get("deepLink").asText());
eventToast.setText(adList.get(0).get("text").asText());
eventToast.setTextLeft(adList.get(0).get("textLeft").asText());
eventToast.setTextRight(adList.get(0).get("textRight").asText());
eventToast.setActionId(Integer.valueOf(adList.get(0).get("actionId").asText()));
JsonNode tkLoadUrlsNode = adList.get(0).get("tkLoadUrls");
if (tkLoadUrlsNode.size() > 0) {
String tkLoadUrl = tkLoadUrlsNode.get(0).asText();
eventToast.getTkLoadUrls().add(tkLoadUrl);
}
JsonNode tkExposureUrlsNode = adList.get(0).get("tkExposureUrls");
if (tkExposureUrlsNode.size() > 0) {
String tkExposureUrl = tkExposureUrlsNode.get(0).asText();
eventToast.getTkExposureUrls().add(tkExposureUrl);
}
JsonNode tkClickUrlsNode = adList.get(0).get("tkClickUrls");
if (tkClickUrlsNode.size() > 0) {
String tkClickUrl = tkClickUrlsNode.get(0).asText();
eventToast.getTkClickUrls().add(tkClickUrl);
}
}
} catch (IOException ex) {
logger.error("VipPayServiceImpl handleAdResult: parse data failed, data = '{}', errorMsg = '{}'", stringHttpResponse.getContent(), ex.getMessage());
}
复制代码
作者:
java
时间:
2017-8-8 11:21
本帖最后由 java 于 2017-8-10 19:14 编辑
import com.fasterxml.jackson.databind.JsonNode;
JsonNode root = retMapper.readTree(jsonString);
String result = root.get("result").asText();
Iterator<JsonNode> realElements = dataType.elements();
while (realElements.hasNext()) {
JsonNode realElement = realElements.next();
}
http://neverflyaway.iteye.com/blog/2299805
欢迎光临 firemail (http://firemail.wang:8088/)
Powered by Discuz! X3