简体   繁体   English

如何检查完整的JSON字符串是否转换为Map

[英]How to check if complete JSON string is converted into Map

I want to check if complete JSON string gets converted to Map in below method.我想检查完整的 JSON 字符串是否在下面的方法中转换为 Map。

public boolean isCompleteStringParsedInToJson() {
    boolean isParsed = false;
    String str = "{ \"tierkey 1\": \"Application\", \"tierkey 2\": \"Desktop\", \"tierkey 3\": \"Software\"}, { \"tierkey 4\": \"Application1\", \"tierkey 5\": \"Desktop2\", \"tierkey 6\": \"Software2\"}";
    ObjectMapper mapper = new ObjectMapper();
    
    try {
        Map map = mapper.readValue(str, Map.class);
        //check if complete input string is parsed into Map above. 
        // then set isParsed flag accordingly. 
        
    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    return isParsed;
}

In above case, only "{ "tierkey 1": "Application", "tierkey 2": "Desktop", "tierkey 3": "Software"}" part is returned as Map.在上述情况下,只有 "{ "tierkey 1": "Application", "tierkey 2": "Desktop", "tierkey 3": "Software"}" 部分作为 Map 返回。 In actual scenario JSON String will be input to the method.在实际场景中,JSON String 将被输入到方法中。 In above method I just need to identify cases where complete JSON string is parsed into Map or not and then accordingly set isParsed flag value(true or false).在上面的方法中,我只需要确定完整的 JSON 字符串是否被解析为 Map 的情况,然后相应地设置 isParsed 标志值(true 或 false)。 JSON String mentioned in the method is not strict JSON and readValue don't fail for such JSON string.方法中提到的 JSON String 不是严格的 JSON,对于这样的 JSON 字符串,readValue 不会失败。 Also dont want to use DeserializationFeature.FAIL_ON_TRAILING_TOKENS on ObjectMapper.也不想在 ObjectMapper 上使用 DeserializationFeature.FAIL_ON_TRAILING_TOKENS。 Input JSON string can be manually written so there could be few spaces before key or Value.输入 JSON 字符串可以手动编写,因此键或值之前可以有几个空格。

What code I should have after mapper.readValue to check complete JSON string parsing into Map?在 mapper.readValue 之后我应该有什么代码来检查完整的 JSON 字符串解析到 Map 中?

if the readValue() line does not throw an exception, that means the parsing was successful and you can set your flag to true.如果 readValue() 行没有抛出异常,则意味着解析成功,您可以将标志设置为 true。 in the "catch" block, set your flag to false.在“catch”块中,将您的标志设置为 false。

The problem is that your string contains 2 separate JSONs.问题是您的字符串包含 2 个单独的 JSON。

{"tierkey 1 : "Application", "tierkey 2": "Desktop", "tierkey 3": "Software"}, { "tierkey 4": "Application1", "tierkey 5": "Desktop2", "tierkey 6": "Software2"}

Note that after key "tierkey 3" there is a '}' which closes the first opening '{'.请注意,在键"tierkey 3"之后有一个“}”用于关闭第一个开口“{”。 So when Json is parsed and it reaches the end of it it Ignores whaterver comes after.因此,当 Json 被解析并到达末尾时,它会忽略后面的任何内容。 so your part from ", { "tierkey 4": "Application1",...}" is ignored and it is correct behavior.所以你来自", { "tierkey 4": "Application1",...}"的部分被忽略,这是正确的行为。 If you want it to be parsed modify your string to如果您希望它被解析,请将您的字符串修改为

{"tierkey 1 : "Application", "tierkey 2": "Desktop", "tierkey 3": "Software",  "tierkey 4": "Application1", "tierkey 5": "Desktop2", "tierkey 6": "Software2"}

or要么

{{"tierkey 1 : "Application", "tierkey 2": "Desktop", "tierkey 3": "Software"}, { "tierkey 4": "Application1", "tierkey 5": "Desktop2", "tierkey 6": "Software2"}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM