迭代嵌套 json 和提取值的指导
guidance with iterating over nested json and extract values
编码和 java 的新手,所以想弄清楚如何迭代这个 JSON 并提取嵌套值
{
"prop1": {
"description": "",
"type": "string"
},
"prop2": {
"description": "",
"type": "string"
},
"prop3": {
"description": "",
"type": "string"
},
"prop4": {
"description": "",
"type": "string"
}
}
到目前为止我有这个:
public class JSONReadExample
{
public static void main(String[] args) throws Exception {
Object procedure = new JSONParser().parse(new FileReader("myFile.json"));
ObjectMapper objectMapper = new ObjectMapper();
String procedureString = objectMapper.writeValueAsString(procedure);
Map<String, Object> map
= objectMapper.readValue(procedureString, new TypeReference<Map<String,Object>>(){});
map.forEach((key, value) -> System.out.println(key + ":" + value));
如何检索每个键的“类型”,例如“字符串”或“数字”嵌套值?
像这样尝试特定类型的地图:
Map<String, LinkedHashMap<String, String>> map = objectMapper.readValue(procedureString, new TypeReference<>() {});
然后您可以使用以下方法检索“类型”:
map.forEach((key, value) -> System.out.println(key + ":" + value.get("type")));
编码和 java 的新手,所以想弄清楚如何迭代这个 JSON 并提取嵌套值
{
"prop1": {
"description": "",
"type": "string"
},
"prop2": {
"description": "",
"type": "string"
},
"prop3": {
"description": "",
"type": "string"
},
"prop4": {
"description": "",
"type": "string"
}
}
到目前为止我有这个:
public class JSONReadExample
{
public static void main(String[] args) throws Exception {
Object procedure = new JSONParser().parse(new FileReader("myFile.json"));
ObjectMapper objectMapper = new ObjectMapper();
String procedureString = objectMapper.writeValueAsString(procedure);
Map<String, Object> map
= objectMapper.readValue(procedureString, new TypeReference<Map<String,Object>>(){});
map.forEach((key, value) -> System.out.println(key + ":" + value));
如何检索每个键的“类型”,例如“字符串”或“数字”嵌套值?
像这样尝试特定类型的地图:
Map<String, LinkedHashMap<String, String>> map = objectMapper.readValue(procedureString, new TypeReference<>() {});
然后您可以使用以下方法检索“类型”:
map.forEach((key, value) -> System.out.println(key + ":" + value.get("type")));