Jackson objectMapping not getting JSON data
The following is an example of deserializing the JSON from the original question (corrected where necessary for validity). This example also demonstrates how to configure Jackson to allow for single-quoted JSON elements.
From the original question, I don't understand where the specific problems were with attempts to deserialize the JSON. For simple data binding, note that the Java property names must match the JSON element names, and that the Java data structure must match the JSON data structure.
input.json
{
'ruleId': 1000000,
'Formula':
{
'ruleAggregates': 'foo',
'fields': ['foo', 'foo'],
'Children':
[
{
'Formula':
{
'ruleAggregates': 'a',
'fields': ['1', '2'],
'Children': []
}
},
{
'Formula':
{
'ruleAggregates': 'b',
'fields': ['3', '4'],
'Children': []
}
},
{}
]
}
}
Java Object Model
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;
}
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));
}
}
output:
{
"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
}
]
}
}
yangdafish
Updated on June 05, 2022Comments
-
yangdafish almost 2 years
I'm using Jackson objectMapper to parse a JSON String. I assigned the JSON to some object RuleModel, where
The JSON is
"{'ruleId': 1000000, Formula': { 'ruleAggregates': 'foo', 'fields': ['foo', 'foo'], 'Children':[{ 'Formula': {'ruleAggregates': 'a', 'fields': ['1', '2'], 'Children': []}}, { 'Formula': {'ruleAggregates': 'b', 'fields': ['3', '4'], 'Children': []}}, {} ]}}",
And the java model is
RuleModel{ private long ruleId; private Formula formula; }
And Formula is
Formula{ private String ruleAggregates private List<String> fields; private List<FormulaModel> Children; }
I can get the ruleId value, and the ruleAggregates value for the first ruleAggregates, but when I try to go into Children, it gets Formulas but not the values inside So I get nulls when I to get any value out of children
-
yangdafish almost 12 yearsTried using that and changing Children to lower case c, didn't work, still getting null for ruleAggregates instead of 'a' and 'b'
-
Mark Bakker almost 12 yearshumh very strange, I would low level debugging, I have no idea why this is not working (with or without annotation)