java 检查字符串是否是有效的 JSON 字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15124367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check whether the String is a Valid JSON String or not?
提问by
I am trying to execute a SELECT SQL
query and after that I need to loop through the ResultSet
and get the columns
and check whether the data I got back for those columns
is a Valid JSON String
or not.
我正在尝试执行一个SELECT SQL
查询,之后我需要遍历ResultSet
并获取columns
并检查我为这些返回的数据是否columns
有效JSON String
。
Here columnsList
is an ArrayList
which will contain all the names of the Columns
of a particular table.
这columnsList
是一个ArrayList
包含Columns
特定表的所有名称的。
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
for (String column : columnsList.split(",")) {
//check whether rs.getString(column) is a valid JSON String?
if(rs.getString(column).ISvalid_JSON_String()) {
System.out.println("Valid JSON String data")
}
}
}
I am not sure how should I check whether the data I got back for each column is a valid JSON String or not?
我不确定我应该如何检查我为每列返回的数据是否是有效的 JSON 字符串?
Any thoughts?
有什么想法吗?
采纳答案by Gus
If you need to be sure it really is valid JSON you're going to need to parse it. A fast, simple, lightweight parser that I like is json-simple. Have a look at their examples here.
如果您需要确定它确实是有效的 JSON,则需要对其进行解析。我喜欢的一个快速、简单、轻量级的解析器是 json-simple。在这里查看他们的示例。
Adapting your code I get:
调整你的代码我得到:
JSONParser parser = new JSONParser();
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
for (String column : columnsList.split(",")) {
//check whether rs.getString(column) is a valid JSON String?
try{
parser.parse(rs.getString(column));
System.out.println("Valid JSON String data");
} catch (ParseException e) {
System.out.printlnn("Invalid JSON String data");
}
}
}
回答by Lachlan Lindsay
I prefer using the Hymanson library its good at handling big files.
我更喜欢使用 Hymanson 库,它擅长处理大文件。
import com.fasterxml.Hymanson.databind.ObjectMapper;
import java.io.IOException;
private Boolean isValidJson(String maybeJson){
try {
final ObjectMapper mapper = new ObjectMapper();
mapper.readTree(maybeJson);
return true;
} catch (IOException e) {
return false;
}
}
I've written some test to check the behaviour of this approach.
我已经编写了一些测试来检查这种方法的行为。
Assert.assertThat(isValidJson("{\"token_type\":\"bearer\",\"access_token\":\"AAAA%2FAAA%3DAAAAAAAA\",\"scope\": \"scope of the token\",\"expires_in\": 200,\"refresh_token\":\"fdb8fdbecf1d03ce5e6125c067733c0d51de209c\"}"), Is.is(true));
Assert.assertThat(isValidJson("[ \"Ford\", \"BMW\", \"Fiat\" ]\n"), Is.is(true));
Assert.assertThat(isValidJson(""), Is.is(false));
Assert.assertThat(isValidJson("Lachlan"), Is.is(false));
Assert.assertThat(isValidJson("{ key: value }"), Is.is(false));
Integrate this solution back into your code. I've assumed that you'll be putting the validation code in the same class.
将此解决方案重新集成到您的代码中。我假设您会将验证代码放在同一个类中。
while (rs.next()) {
for (String column : columnsList.split(",")) {
String maybeJson = rs.getString(column)
//check whether rs.getString(column) is a valid JSON String?
if(isValidJson(maybeJson)) {
System.out.println("Valid JSON String data")
}
}
}
Consider adding a null check depending on what values and desired behaviour.
考虑根据什么值和所需行为添加空检查。