有一个json是这样的: { { "exCode": "SUC0000", "lu48Data": { "CITQIE51Z1": [ { "zOrdNum": "210513662558510", "zFqSta": "0", "xRsvFld": "", "zOrgRat": "0.0000", "zOrdEDa": "20230108", "zWpTim": "6", "zCrdNum": "", "zMerTyp": "34", "zTolAmt": "100000.00", "zNPsDat": "20220808", "zTolFee": "5000.00", "zOrgCcy": "156", "zOTxNum": "0", "zLsPFee": "416.67", "zFqTyp": "99", "zMerNam": "上海广丰", "zLsPAmt": "8333.33", "zOrdChl": "01", "zNPsFee": "416.67", "zActNum": "", "zGenDat": "20210513", "zOrgAmt": "0.00", "zWpAmt": "50000.02", "zAplUsg": "", "zTolTim": "12", "zPActNu": "0299783239050001", "zWpFee": "2499.98", "zNPsAmt": "8333.33" } ], "$REQSEQ$": [ { "xReqSeq": "CIT20BM06150030000DF" } ], "CITQIE51Y1": [ { "yOrdNo": "", "yFqSta": "", "yActNo": "", "yPagFlg": "Y", "yGenDat": "", "yFqTyp": "" } ] }, "status": "0" } } 用JsonParser解析获取到zWpTim的值
时间: 2023-07-21 11:09:51 浏览: 190
QOCI使用例子 初级使用 测试通过
要使用 JsonParser 解析获取到 "zWpTim" 的值,你可以按照以下步骤进行:
```java
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonParserExample {
public static void main(String[] args) {
String json = "{ \"exCode\": \"SUC0000\", \"lu48Data\": { \"CITQIE51Z1\": [ { \"zOrdNum\": \"210513662558510\", \"zFqSta\": \"0\", \"xRsvFld\": \"\", \"zOrgRat\": \"0.0000\", \"zOrdEDa\": \"20230108\", \"zWpTim\": \"6\", \"zCrdNum\": \"\", \"zMerTyp\": \"34\", \"zTolAmt\": \"100000.00\", \"zNPsDat\": \"20220808\", \"zTolFee\": \"5000.00\", \"zOrgCcy\": \"156\", \"zOTxNum\": \"0\", \"zLsPFee\": \"416.67\", \"zFqTyp\": \"99\", \"zMerNam\": \"上海广丰\", \"zLsPAmt\": \"8333.33\", \"zOrdChl\": \"01\", \"zNPsFee\": \"416.67\", \"zActNum\": \"\", \"zGenDat\": \"20210513\", \"zOrgAmt\": \"0.00\", \"zWpAmt\": \"50000.02\", \"zAplUsg\": \"\", \"zTolTim\": \"12\", \"zPActNu\": \"0299783239050001\", \"zWpFee\": \"2499.98\", \"zNPsAmt\": \"8333.33\" } ], \"$REQSEQ$\": [ { \"xReqSeq\": \"CIT20BM06150030000DF\" } ], \"CITQIE51Y1\": [ { \"yOrdNo\": \"\", \"yFqSta\": \"\", \"yActNo\": \"\", \"yPagFlg\": \"Y\", \"yGenDat\": \"\", \"yFqTyp\": \"\" } ] }, \"status\": \"0\" }";
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(json);
// 获取 lu48Data 节点
JsonNode lu48DataNode = rootNode.get("lu48Data");
// 获取 CITQIE51Z1 节点
JsonNode citqie51z1Node = lu48DataNode.get("CITQIE51Z1");
// 获取第一个元素
JsonNode firstElement = citqie51z1Node.get(0);
// 获取 zWpTim 的值
String zWpTimValue = firstElement.get("zWpTim").asText();
System.out.println("zWpTim value: " + zWpTimValue);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
以上代码使用 Jackson 库中的 ObjectMapper 类和 JsonNode 类来解析 JSON 数据。首先,我们将 JSON 字符串转换为根节点 rootNode,然后根据 JSON 结构逐级获取所需的节点和值。
运行上述代码,将输出 "zWpTim value: 6",即成功获取到了 "zWpTim" 的值。记得确保你的项目中已添加了 Jackson 库的依赖。
阅读全文