같은 온라인 JSON 유효성 검사기 도구를 사용할 수있다 (여기서 유효성에 필요한 수정). 또한이 예제는 싱글 쿼트 JSON 요소를 허용하도록 Jackson을 구성하는 방법을 보여줍니다.
원래 질문에서 JSON을 비 직렬화하려는 시도와 관련된 특정 문제를 이해하지 못합니다. 간단한 데이터 바인딩의 경우, Java 특성 이름이 JSON 요소 이름과 일치해야하며 Java 데이터 구조가 JSON 데이터 구조와 일치해야합니다.
{
'ruleId': 1000000,
'Formula':
{
'ruleAggregates': 'foo',
'fields': ['foo', 'foo'],
'Children':
[
{
'Formula':
{
'ruleAggregates': 'a',
'fields': ['1', '2'],
'Children': []
}
},
{
'Formula':
{
'ruleAggregates': 'b',
'fields': ['3', '4'],
'Children': []
}
},
{}
]
}
}
자바 객체 모델
import com.fasterxml.jackson.annotation.JsonProperty;
class RuleModel
{
private long ruleId;
@JsonProperty("Formula") private Formula formula;
}
class Formula
{
private String ruleAggregates;
private List<String> fields;
private List<FormulaModel> Children;
}
class FormulaModel
{
@JsonProperty("Formula") private Formula formula;
}
input.json JacksonFoo.java
import java.io.File;
import java.util.List;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonFoo
{
public static void main(String[] args) throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
RuleModel model = mapper.readValue(new File("input.json"), RuleModel.class);
System.out.println(mapper.writeValueAsString(model));
}
}
출력 :
{
"ruleId": 1000000,
"Formula": {
"ruleAggregates": "foo",
"fields": [
"foo",
"foo"
],
"Children": [
{
"Formula": {
"ruleAggregates": "a",
"fields": [
"1",
"2"
],
"Children": []
}
},
{
"Formula": {
"ruleAggregates": "b",
"fields": [
"3",
"4"
],
"Children": []
}
},
{
"Formula": null
}
]
}
}
이 경우 C를 낮추기 위해 어린이가 사용하고 변경 시도는 여전히 대신 'A'와 'B' – yangdafish
HUMH 아주 이상한, 내가 것 낮은 수준의 디버깅 ruleAggregates에 null을 받고, 작동하지 않았다 , 왜 이것이 작동하지 않는지 전혀 알지 못합니다 (주석이 있거나 없음) –