繁体   English   中英

无法使用 FasterXML Jackson 解析 Java 中没有双引号的 JSON

[英]Not able to parse JSON without double quotes in Java using FasterXML Jackson

这是我的原始 Json 数据: {ht_missingConditions=null, employeeNumber=UMB1075962, firstName=Pete}

注意:没有双引号

我的 Java Class

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class MBIdClass {

    private String ht_missingConditions; 
    private String name; 
    private String employeeNumber; 

//All setter getters
}

我的实用程序转换 JSON

public static Map<String,String> test(String json) {
        ObjectMapper om=new ObjectMapper();
        om.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        om.configure(com.fasterxml.jackson.core.JsonParser.Feature.AUTO_CLOSE_SOURCE, true);
        try {
            return om.readValue(json, new TypeReference<HashMap<String,String>>(){});
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

早些时候我尝试通过通用 class 但现在我至少尝试将它解析并转换为HashMap

我正在使用以下方式打电话

Map<String, String> testObj = test(sanitizedcleanData);

我最终出现以下错误:


com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'UMB1075962': was expecting ('true', 'false' or 'null')
 at [Source: (String)"{ht_missingConditions:null, employeeNumber:UMB1075962, firstName:Pete  st"[truncated 1569 chars]; line: 1, column: 54]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1804)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:703)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2853)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddValue(ReaderBasedJsonParser.java:1899)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextFieldName(ReaderBasedJsonParser.java:968)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:512)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:364)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:29)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3023)
    at com.devsoftbd.palash.studentsinfo.utils.CommonUtils.test(CommonUtils.java:80)
    at com.devsoftbd.palash.studentsinfo.StudentsInfoApplication.lambda$0(StudentsInfoApplication.java:55)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:324)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
    at com.devsoftbd.palash.studentsinfo.StudentsInfoApplication.main(StudentsInfoApplication.java:21)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

我试图用:替换=但我在下面的文章中提到的仍然没有运气: StackOverflow-Issue

您的 JSON 字符串的形式无效,它看起来更像是Java 中的 Properies class

因此,您可以通过替换一些字符来转换您无效的 JSON 字符串以满足.properties文件的格式,然后使用Properties读取它,如下所示:

String inputStr = "{ht_missingConditions=null, employeeNumber=UMB1075962, firstName=Pete}";

inputStr = inputStr.replace("{", "").replace("}", "").replace(",", "\r\n");

Properties properties = new Properties();
properties.load(new ByteArrayInputStream(inputStr.getBytes(StandardCharsets.UTF_8)));
System.out.println(properties.toString()); // {ht_missingConditions=null, firstName=Pete, employeeNumber=UMB1075962}

或者您也可以将其转换为Map并获得类似的结果:

Map<String, String> propMap = properties.entrySet().stream()
        .collect(Collectors.toMap(
                e -> String.valueOf(e.getKey()),
                e -> String.valueOf(e.getValue()),
                (prev, next) -> next, HashMap::new
        ));
System.out.println(propMap.toString()); // {firstName=Pete, ht_missingConditions=null, employeeNumber=UMB1075962}

暂无
暂无

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

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