检查JSON字符串-Java中是否存在多个键
我有一个复杂的JSON字符串(数组/对象),我必须检查它是否包含一些键。这基本上是对我得到的JSON字符串的验证 乙二醇 我必须验证JSON是否包含检查JSON字符串-Java中是否存在多个键,java,json,validation,key,schema,Java,Json,Validation,Key,Schema,我有一个复杂的JSON字符串(数组/对象),我必须检查它是否包含一些键。这基本上是对我得到的JSON字符串的验证 乙二醇 我必须验证JSON是否包含a,c,c.key4,a[*].key1。如果键存在,则其值不应为空 我在这里看到了许多其他的讨论,但要么它不支持对象内部的数组,要么它使用大量的库进行JSON验证。我要寻找的是一个简单的库或方法,它只检查键是否存在于JSON字符串中 请给我指点怎么做 谢谢大家。我根据自己的要求编写了代码 public Object parseJsonForKey(
a
,c
,c.key4
,a[*].key1
。如果键存在,则其值不应为空
我在这里看到了许多其他的讨论,但要么它不支持对象内部的数组,要么它使用大量的库进行JSON验证。我要寻找的是一个简单的库或方法,它只检查键是否存在于JSON字符串中
请给我指点怎么做
谢谢大家。我根据自己的要求编写了代码
public Object parseJsonForKey(String key,JSONObject jsonObject) throws DigitalSelfException{
try {
String[] keyPathArray = key.split("\\.");
System.out.println("Array length :" + keyPathArray.length);
if (keyPathArray.length == 1) {
if (jsonObject.containsKey(key)) {
return jsonObject.get(key);
} else {
return null;
}
}
JSONObject tempObj = new JSONObject();
JSONArray tempArray = new JSONArray();
if (jsonObject.get(keyPathArray[0]) instanceof JSONObject) {
tempObj = (JSONObject) jsonObject.get(keyPathArray[0]);
} else if (jsonObject.get(keyPathArray[0]) instanceof JSONArray) {
tempArray = (JSONArray) jsonObject.get(keyPathArray[0]);
tempObj = (JSONObject) tempArray.get(0);
}
System.out.println(tempObj.toString());
for (int i = 1; i < keyPathArray.length - 1; i++) {
if (tempObj.containsKey(keyPathArray[i])) {
if (tempObj.get(keyPathArray[i]) instanceof JSONObject) {
tempObj = (JSONObject) tempObj.get(keyPathArray[i]);
} else if (tempObj.get(keyPathArray[i]) instanceof JSONArray) {
tempArray = (JSONArray) tempObj.get(keyPathArray[i]);
tempObj = (JSONObject) tempArray.get(0);
}
System.out.println(tempObj.toString());
} else {
return null;
}
}
if (tempObj.containsKey(keyPathArray[keyPathArray.length - 1])) {
return tempObj.get(keyPathArray[keyPathArray.length - 1]);
} else {
return null;
}
} catch (Exception e) {
// throw Exception
}
因此,如果该值为null,则表示该键不存在。否则,它包含键的值
mandatoryKeys示例:[“a”、“c”、“c.key4”、“a.key1”]
我使用了org.simple.json库
这个方法适合我的要求。谢谢大家。我根据自己的要求编写了代码
public Object parseJsonForKey(String key,JSONObject jsonObject) throws DigitalSelfException{
try {
String[] keyPathArray = key.split("\\.");
System.out.println("Array length :" + keyPathArray.length);
if (keyPathArray.length == 1) {
if (jsonObject.containsKey(key)) {
return jsonObject.get(key);
} else {
return null;
}
}
JSONObject tempObj = new JSONObject();
JSONArray tempArray = new JSONArray();
if (jsonObject.get(keyPathArray[0]) instanceof JSONObject) {
tempObj = (JSONObject) jsonObject.get(keyPathArray[0]);
} else if (jsonObject.get(keyPathArray[0]) instanceof JSONArray) {
tempArray = (JSONArray) jsonObject.get(keyPathArray[0]);
tempObj = (JSONObject) tempArray.get(0);
}
System.out.println(tempObj.toString());
for (int i = 1; i < keyPathArray.length - 1; i++) {
if (tempObj.containsKey(keyPathArray[i])) {
if (tempObj.get(keyPathArray[i]) instanceof JSONObject) {
tempObj = (JSONObject) tempObj.get(keyPathArray[i]);
} else if (tempObj.get(keyPathArray[i]) instanceof JSONArray) {
tempArray = (JSONArray) tempObj.get(keyPathArray[i]);
tempObj = (JSONObject) tempArray.get(0);
}
System.out.println(tempObj.toString());
} else {
return null;
}
}
if (tempObj.containsKey(keyPathArray[keyPathArray.length - 1])) {
return tempObj.get(keyPathArray[keyPathArray.length - 1]);
} else {
return null;
}
} catch (Exception e) {
// throw Exception
}
因此,如果该值为null,则表示该键不存在。否则,它包含键的值
mandatoryKeys示例:[“a”、“c”、“c.key4”、“a.key1”]
我使用了org.simple.json库
这个方法很适合我的要求。重库是什么意思?从验证json键和值的意义上来说,这可能对@Andremoniy重库很有帮助,我需要的是简单的键验证你所说的重库是什么意思?从验证json键和值的意义上来说,这可能对@Andremoniy重库很有帮助,我需要的是简单的键验证
public Object parseJsonForKey(String key,JSONObject jsonObject) throws DigitalSelfException{
try {
String[] keyPathArray = key.split("\\.");
System.out.println("Array length :" + keyPathArray.length);
if (keyPathArray.length == 1) {
if (jsonObject.containsKey(key)) {
return jsonObject.get(key);
} else {
return null;
}
}
JSONObject tempObj = new JSONObject();
JSONArray tempArray = new JSONArray();
if (jsonObject.get(keyPathArray[0]) instanceof JSONObject) {
tempObj = (JSONObject) jsonObject.get(keyPathArray[0]);
} else if (jsonObject.get(keyPathArray[0]) instanceof JSONArray) {
tempArray = (JSONArray) jsonObject.get(keyPathArray[0]);
tempObj = (JSONObject) tempArray.get(0);
}
System.out.println(tempObj.toString());
for (int i = 1; i < keyPathArray.length - 1; i++) {
if (tempObj.containsKey(keyPathArray[i])) {
if (tempObj.get(keyPathArray[i]) instanceof JSONObject) {
tempObj = (JSONObject) tempObj.get(keyPathArray[i]);
} else if (tempObj.get(keyPathArray[i]) instanceof JSONArray) {
tempArray = (JSONArray) tempObj.get(keyPathArray[i]);
tempObj = (JSONObject) tempArray.get(0);
}
System.out.println(tempObj.toString());
} else {
return null;
}
}
if (tempObj.containsKey(keyPathArray[keyPathArray.length - 1])) {
return tempObj.get(keyPathArray[keyPathArray.length - 1]);
} else {
return null;
}
} catch (Exception e) {
// throw Exception
}