java 解析json字符串的方式

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

// Parse the JSON string
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(json);

// Get the "serviceInfoList" array
JsonNode serviceInfoList = rootNode.get("infoList");

// Get the first element of the array and retrieve its "countryCode" value
String countryCode = serviceInfoList.get(0).get("Code").asText();

或者

import com.fasterxml.jackson.databind.ObjectMapper;

// Parse the JSON string
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> rootMap = objectMapper.readValue(json, Map.class);

// Get the "machineInfo" object and convert it to a map
Map<String, Object> machineInfoMap = (Map<String, Object>) rootMap.get("info");

 或者

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

// Parse the JSON string
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(json);

// Get the "serviceInfoList" array
JsonNode serviceInfoList = rootNode.get("infoList");

// Convert the "serviceInfoList" array to a list of maps
List<Map<String, Object>> serviceInfoListMap = objectMapper.convertValue(serviceInfoList, new TypeReference<List<Map<String, Object>>>() {});

 

posted @ 2023-03-03 14:51  生命不息bug不止  阅读(225)  评论(0编辑  收藏  举报