Reputation: 79
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
Upvotes: 2
Views: 13260
Reputation: 66963
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
}
]
}
}
Upvotes: 6
Reputation: 1980
In JSON: Use double quotes for field names; Start field names with lower case;
In Java: Add getters and setters methods for fields; Implementing java.io.Serializable may help;
You could also use an online json validator tool like http://jsonlint.com/
Upvotes: 0
Reputation: 1348
Childeren starts with a capital C, jackson if I am not mistaken the default behaviour of jackson is camel case. In other words jackson searches for 'childeren'. You can overide the property name by using this field annotation.
@JsonProperty("Children")
private List<FormulaModel> Children;
Upvotes: 0