java Jackson objectMapping 未获取 JSON 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11531298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:27:48  来源:igfitidea点击:

Hymanson objectMapping not getting JSON data

javajsonrecursionHymanson

提问by yangdafish

I'm using Hymanson objectMapper to parse a JSON String. I assigned the JSON to some object RuleModel, where

我正在使用 Hymanson objectMapper 来解析 JSON 字符串。我将 JSON 分配给某个对象 RuleModel,其中

The JSON is

JSON 是

"{'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

而java模型是

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

我可以获得第一个 ruleAggregates 的 ruleId 值和 ruleAggregates 值,但是当我尝试进入 Children 时,它获取公式而不是里面的值 所以当我从 children 中获取任何值时我得到空值

回答by Programmer Bruce

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 Hymanson to allow for single-quoted JSON elements.

以下是从原始问题反序列化 JSON 的示例(在必要时更正以确保有效性)。此示例还演示了如何配置 Hymanson 以允许使用单引号 JSON 元素。

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.

从最初的问题来看,我不明白尝试反序列化 JSON 的具体问题在哪里。对于简单数据绑定,请注意 Java 属性名称必须与 JSON 元素名称匹配,并且 Java 数据结构必须与 JSON 数据结构匹配。

input.json

输入.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

Java对象模型

import com.fasterxml.Hymanson.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;
}

HymansonFoo.java

HymansonFoo.java

import java.io.File;
import java.util.List;

import com.fasterxml.Hymanson.annotation.PropertyAccessor;
import com.fasterxml.Hymanson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.Hymanson.core.JsonParser;
import com.fasterxml.Hymanson.databind.ObjectMapper;

public class HymansonFoo
{
  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
            }
        ]
    }
}

回答by Mark Bakker

Childeren starts with a capital C, Hymanson if I am not mistaken the default behaviour of Hymanson is camel case. In other words Hymanson searches for 'childeren'. You can overide the property name by using this field annotation.

Childeren 以大写 C 开头,Hymanson 如果我没记错的话,Hymanson 的默认行为是驼峰式的。换句话说,Hymanson 搜索的是“childeren”。您可以使用此字段注释来覆盖属性名称。

@JsonProperty("Children")
private List<FormulaModel> Children;

回答by Ale Zalazar

In JSON: Use double quotes for field names; Start field names with lower case;

在 JSON 中:对字段名称使用双引号;以小写开头的字段名称;

In Java: Add getters and setters methods for fields; Implementing java.io.Serializable may help;

在 Java 中:为字段添加 getter 和 setter 方法;实现 java.io.Serializable 可能会有所帮助;

You could also use an online json validator tool like http://jsonlint.com/

您还可以使用在线 json 验证器工具,例如http://jsonlint.com/