无法使用 FasterXML Jackson 在 Java 中不使用双引号解析 JSON

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

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

注意:没有双引号

我的JavaClass

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[=15=](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)

我尝试用 : 替换 = 但还是不行 我指的是下面的文章:Whosebug-Issue

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

因此,您可以通过替换一些字符来转换您无效的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}