在不知道 JSON 格式的情况下用 Java 解析 JSON [英] Parsing JSON in Java without knowing JSON format
问题描述
我正在尝试用 Java 解析 JSON 字符串并找到键值对,以便我可以确定 JSON 对象的大致结构,因为 JSON 字符串的对象结构是未知的.
例如,一个执行可能有一个像这样的 JSON 字符串:
{"id" : 12345, "days" : [ "Monday", "Wednesday" ], "person" : { "firstName" : "David", "lastName" : "Menoyo" } }
还有一个这样的:
{"url" : "http://someurl.com", "method" : "POST", "isauth" : false }
我将如何遍历各种 JSON 元素并确定键及其值?我查看了 jackson-core
的 JsonParser
.我知道如何获取下一个令牌"并确定它是什么类型的令牌(即字段名称、值、数组开始等),但是,我不知道如何获取实际令牌的值.>
例如:
public void parse(String json) {尝试 {JsonFactory f = new JsonFactory();JsonParser 解析器 = f.createParser(json);JsonToken token = parser.nextToken();而(令牌!= null){if (token.equals(JsonToken.START_ARRAY)) {logger.debug("起始数组:" + token.toString());} else if (token.equals(JsonToken.END_ARRAY)) {logger.debug("结束数组:" + token.toString());} else if (token.equals(JsonToken.START_OBJECT)) {logger.debug("起始对象:" + token.toString());} else if (token.equals(JsonToken.END_OBJECT)) {logger.debug("结束对象:" + token.toString());} else if (token.equals(JsonToken.FIELD_NAME)) {logger.debug("字段名称:" + token.toString());} else if (token.equals(JsonToken.VALUE_FALSE)) {logger.debug("Value False:" + token.toString());} else if (token.equals(JsonToken.VALUE_NULL)) {logger.debug("值为空:" + token.toString());} else if (token.equals(JsonToken.VALUE_NUMBER_FLOAT)) {logger.debug("数值浮点数:" + token.toString());} else if (token.equals(JsonToken.VALUE_NUMBER_INT)) {logger.debug("Value Number Int:" + token.toString());} else if (token.equals(JsonToken.VALUE_STRING)) {logger.debug("值字符串:" + token.toString());} else if (token.equals(JsonToken.VALUE_TRUE)) {logger.debug("值真:" + token.toString());} 别的 {logger.debug("别的东西:" + token.toString());}token = parser.nextToken();}} 捕获(异常 e){logger.error("", e);}}
jackson
或其他一些库(gson
或 simple-json
)中是否有一个生成树的类,或允许一个类循环遍历 json 元素并获取实际的键名和值?
看一看 Jacksons 内置树模型功能.
您的代码将是:
public void parse(String json) {JsonFactory 工厂 = new JsonFactory();ObjectMapper mapper = new ObjectMapper(factory);JsonNode rootNode = mapper.readTree(json);Iterator>fieldsIterator = rootNode.fields();而 (fieldsIterator.hasNext()) {Map.Entryfield = fieldsIterator.next();System.out.println("Key:" + field.getKey() + " Value:" + field.getValue());}}
I am trying to parse JSON strings in Java and find the key-value pairs so that I can determine the approximate structure of the JSON object since object structure of JSON string is unknown.
For example, one execution may have a JSON string like this:
{"id" : 12345, "days" : [ "Monday", "Wednesday" ], "person" : { "firstName" : "David", "lastName" : "Menoyo" } }
And another like this:
{"url" : "http://someurl.com", "method" : "POST", "isauth" : false }
How would I cycle through the various JSON elements and determine the keys and their values? I looked at jackson-core
's JsonParser
. I see how I can grab the next "token" and determine what type of token it is (i.e., field name, value, array start, etc), but, I don't know how to grab the actual token's value.
For example:
public void parse(String json) {
try {
JsonFactory f = new JsonFactory();
JsonParser parser = f.createParser(json);
JsonToken token = parser.nextToken();
while (token != null) {
if (token.equals(JsonToken.START_ARRAY)) {
logger.debug("Start Array : " + token.toString());
} else if (token.equals(JsonToken.END_ARRAY)) {
logger.debug("End Array : " + token.toString());
} else if (token.equals(JsonToken.START_OBJECT)) {
logger.debug("Start Object : " + token.toString());
} else if (token.equals(JsonToken.END_OBJECT)) {
logger.debug("End Object : " + token.toString());
} else if (token.equals(JsonToken.FIELD_NAME)) {
logger.debug("Field Name : " + token.toString());
} else if (token.equals(JsonToken.VALUE_FALSE)) {
logger.debug("Value False : " + token.toString());
} else if (token.equals(JsonToken.VALUE_NULL)) {
logger.debug("Value Null : " + token.toString());
} else if (token.equals(JsonToken.VALUE_NUMBER_FLOAT)) {
logger.debug("Value Number Float : " + token.toString());
} else if (token.equals(JsonToken.VALUE_NUMBER_INT)) {
logger.debug("Value Number Int : " + token.toString());
} else if (token.equals(JsonToken.VALUE_STRING)) {
logger.debug("Value String : " + token.toString());
} else if (token.equals(JsonToken.VALUE_TRUE)) {
logger.debug("Value True : " + token.toString());
} else {
logger.debug("Something else : " + token.toString());
}
token = parser.nextToken();
}
} catch (Exception e) {
logger.error("", e);
}
}
Is there a class in jackson
or some other library (gson
or simple-json
) that produces a tree, or allows one to cycle through the json elements and obtain the actual key names in addition to the values?
Take a look at Jacksons built-in tree model feature.
And your code will be:
public void parse(String json) {
JsonFactory factory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper(factory);
JsonNode rootNode = mapper.readTree(json);
Iterator<Map.Entry<String,JsonNode>> fieldsIterator = rootNode.fields();
while (fieldsIterator.hasNext()) {
Map.Entry<String,JsonNode> field = fieldsIterator.next();
System.out.println("Key: " + field.getKey() + " Value:" + field.getValue());
}
}
这篇关于在不知道 JSON 格式的情况下用 Java 解析 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!