无法使用FasterXMLJackson在Java中解析没有双引号的JSON



这是我的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问题

JSON字符串的形式无效,它看起来更像Java中的Properies类。

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

String inputStr = "{ht_missingConditions=null, employeeNumber=UMB1075962, firstName=Pete}";
inputStr = inputStr.replace("{", "").replace("}", "").replace(",", "rn");
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}

相关内容

  • 没有找到相关文章

最新更新