在java中递归解析未知的json输入结构
我尝试用java解析递归未知的json输入结构,如下面的格式,并尝试在另一个json中重写相同的结构 同时,我需要在解析时验证每个json键/值在java中递归解析未知的json输入结构,java,json,recursion,jackson,gson,Java,Json,Recursion,Jackson,Gson,我尝试用java解析递归未知的json输入结构,如下面的格式,并尝试在另一个json中重写相同的结构 同时,我需要在解析时验证每个json键/值 {"Verbs":[{ "aaaa":"30d", "type":"ed", "rel":1.0, "id":"80", "spoken":"en", "ct":"on", "sps":null },{ "aaaa":"31", "type":"cc", "rel":3.0, "id":"10", "spoken":"en", "ct"
{"Verbs":[{
"aaaa":"30d", "type":"ed", "rel":1.0, "id":"80", "spoken":"en", "ct":"on", "sps":null
},{
"aaaa":"31", "type":"cc", "rel":3.0, "id":"10", "spoken":"en", "ct":"off", "sps":null
},{
"aaaa":"81", "type":"nn", "rel":3.0, "id":"60", "spoken":"en", "ct":"on", "sps":null
}]}
请告知我可以使用哪个json解析器来读取和写入未知json内容。通过这种方式,您可以递归解析json对象:
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
public class JsonQuestion {
public static void main(String[] args) {
String input = "{\"Verbs\":[{\n" +
" \"aaaa\":\"30d\", \"type\":\"ed\", \"rel\":1.0, \"id\":\"80\", \"spoken\":\"en\", \"ct\":\"on\", \"sps\":null\n" +
"},{\n" +
" \"aaaa\":\"31\", \"type\":\"cc\", \"rel\":3.0, \"id\":\"10\", \"spoken\":\"en\", \"ct\":\"off\", \"sps\":null\n" +
"},{\n" +
" \"aaaa\":\"81\", \"type\":\"nn\", \"rel\":3.0, \"id\":\"60\", \"spoken\":\"en\", \"ct\":\"on\", \"sps\":null\n" +
"}]}";
JsonObject jsonObject = JsonObject.readFrom(input);
handleObject(jsonObject);
}
private static void handleValue(JsonObject.Member member, JsonValue value) {
if (value.isArray()) {
if (member != null) {
System.out.print("name = " + member.getName());
}
System.out.println("array value ");
recurseArray(value.asArray());
} else if (value.isBoolean()) {
if (member != null) {
System.out.print("name = " + member.getName());
}
System.out.println(", boolean value = " + value.asBoolean());
} else if (value.isNull()) {
if (member != null) {
System.out.print("name = " + member.getName());
}
System.out.println(", null value");
} else if (value.isNumber()) {
if (member != null) {
System.out.print("name = " + member.getName());
}
System.out.println(", number value = " + value.asDouble());
} else if (value.isObject()) {
if (member != null) {
System.out.print("name = " + member.getName());
}
System.out.println(", object value ");
handleObject(value.asObject());
} else if (value.isString()) {
if (member != null) {
System.out.print("name = " + member.getName());
}
System.out.println(", string value = " + value.asString());
}
}
private static void handleObject(JsonObject object) {
for (JsonObject.Member next : object) {
JsonValue value = next.getValue();
handleValue(next, value);
}
}
private static void recurseArray(JsonArray array) {
for (JsonValue value : array) {
handleValue(null, value);
}
}
}
通过这种方式,您可以递归解析JSON对象:
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
public class JsonQuestion {
public static void main(String[] args) {
String input = "{\"Verbs\":[{\n" +
" \"aaaa\":\"30d\", \"type\":\"ed\", \"rel\":1.0, \"id\":\"80\", \"spoken\":\"en\", \"ct\":\"on\", \"sps\":null\n" +
"},{\n" +
" \"aaaa\":\"31\", \"type\":\"cc\", \"rel\":3.0, \"id\":\"10\", \"spoken\":\"en\", \"ct\":\"off\", \"sps\":null\n" +
"},{\n" +
" \"aaaa\":\"81\", \"type\":\"nn\", \"rel\":3.0, \"id\":\"60\", \"spoken\":\"en\", \"ct\":\"on\", \"sps\":null\n" +
"}]}";
JsonObject jsonObject = JsonObject.readFrom(input);
handleObject(jsonObject);
}
private static void handleValue(JsonObject.Member member, JsonValue value) {
if (value.isArray()) {
if (member != null) {
System.out.print("name = " + member.getName());
}
System.out.println("array value ");
recurseArray(value.asArray());
} else if (value.isBoolean()) {
if (member != null) {
System.out.print("name = " + member.getName());
}
System.out.println(", boolean value = " + value.asBoolean());
} else if (value.isNull()) {
if (member != null) {
System.out.print("name = " + member.getName());
}
System.out.println(", null value");
} else if (value.isNumber()) {
if (member != null) {
System.out.print("name = " + member.getName());
}
System.out.println(", number value = " + value.asDouble());
} else if (value.isObject()) {
if (member != null) {
System.out.print("name = " + member.getName());
}
System.out.println(", object value ");
handleObject(value.asObject());
} else if (value.isString()) {
if (member != null) {
System.out.print("name = " + member.getName());
}
System.out.println(", string value = " + value.asString());
}
}
private static void handleObject(JsonObject object) {
for (JsonObject.Member next : object) {
JsonValue value = next.getValue();
handleValue(next, value);
}
}
private static void recurseArray(JsonArray array) {
for (JsonValue value : array) {
handleValue(null, value);
}
}
}
使用gson库
还是杰克逊
public void processJson() {
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode node = objectMapper.readTree(jsonStr);
System.out.println(node);
processNode(node);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void processNode(JsonNode n) {
if (n.isContainerNode()) {
processJsonContainer(n.iterator());
} else if (n.isNull()) {
System.out.println("Null || :" + n);
} else if (n.isNumber()) {
System.out.println("Number || :" + n.asDouble());
} else if (n.isBoolean()) {
System.out.println("Boolean || :" + n.asBoolean());
} else if (n.isTextual()) {
System.out.println("Text || :" + n.asText());
}
}
private void processJsonContainer(Iterator<JsonNode> iterator) {
while (iterator.hasNext()) {
processNode(iterator.next());
}
}
使用gson库
还是杰克逊
public void processJson() {
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode node = objectMapper.readTree(jsonStr);
System.out.println(node);
processNode(node);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void processNode(JsonNode n) {
if (n.isContainerNode()) {
processJsonContainer(n.iterator());
} else if (n.isNull()) {
System.out.println("Null || :" + n);
} else if (n.isNumber()) {
System.out.println("Number || :" + n.asDouble());
} else if (n.isBoolean()) {
System.out.println("Boolean || :" + n.asBoolean());
} else if (n.isTextual()) {
System.out.println("Text || :" + n.asText());
}
}
private void processJsonContainer(Iterator<JsonNode> iterator) {
while (iterator.hasNext()) {
processNode(iterator.next());
}
}
任何它的json;他们解析它。如何解析每个json元素,并将其重新组装为与Input相同的元素?您想对解析后的数据做什么?我想验证每个json令牌键/值是否具有特殊字符。然后,我将替换这些值并将json重新格式化为旧结构。不要使用Jackson或其他任何工具。我们将为您构建POJO工具,除非该结构非常规则且可重复。除非你真的理解它们,否则它们会造成更多的混乱。它的json;他们解析它。如何解析每个json元素,并将其重新组装为与Input相同的元素?您想对解析后的数据做什么?我想验证每个json令牌键/值是否具有特殊字符。然后,我将替换这些值并将json重新格式化为旧结构。不要使用Jackson或其他任何工具。我们将为您构建POJO工具,除非该结构非常规则且可重复。除非您真正理解它们,否则它们会造成更多的混乱。感谢您的回复,我们可以在解析每个值时并行地构建json响应。很抱歉,我不理解您的评论,这是问题吗?是的,我们可以在gson/jackson库中实现相同的解析逻辑吗?因为我们只在我的项目中使用gson/jackson libs,我想是这样的。但我用的是这个。如果你在别人花时间试图帮助你之前提前指定完整的需求会更好。谢谢你的回复,我们可以在解析每个值时并行地构建json响应。对不起,我不理解你的评论,这是个问题吗?是的,我们能在gson/jackson库中实现相同的解析逻辑吗?因为我们只在我的项目中使用gson/jackson libs,我想是这样的。但我用的是这个,如果你在别人花时间试图帮助你之前提前指定完整的需求会更好。