【问题标题】:Not able to parse JSON without double quotes in Java using FasterXML Jackson无法使用 FasterXML Jackson 在 Java 中解析没有双引号的 JSON
【发布时间】:2022-01-19 18:02:18
【问题描述】:

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

注意:没有双引号

我的 Java 类

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;
    }

之前我尝试过传递 Generic 类,但现在我至少尝试将其解析并转换为 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

【问题讨论】:

  • 注意它是ALLOW_UNQUOTED_FIELD_NAMES,它处理names而不是values。未加引号的字符串值是无效的 JSON。看看这个可能的重复stackoverflow.com/q/15005185/18157

标签: java json fasterxml


【解决方案1】:

您的 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}

【讨论】:

  • 感谢@LHCHIN,这种方式也对我有用!非常感谢
猜你喜欢
  • 1970-01-01
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
  • 2021-11-11
  • 1970-01-01
  • 2012-07-05
  • 1970-01-01
  • 2019-07-10
相关资源
最近更新 更多