简体   繁体   English

Gson 为 fromJson 方法返回 null

[英]Gson returns null for fromJson method

I am trying to parse my Tools class using Gson.我正在尝试使用 Gson 解析我的Tools class。 The problem is that the toolConverted have empty ToolA & ToolB when I run my unit test.问题是当我运行单元测试时 toolConverted 有空的 ToolA 和 ToolB。 The string itself is a valid JSON string, I figured the problem may be with my constructors in the Tools.java class.字符串本身是有效的 JSON 字符串,我认为问题可能出在 Tools.java class 中的构造函数上。

Tools.java工具.java

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class Tools{
    @JsonProperty("ToolA")
    List<ToolA> toolA;

    @JsonProperty("ToolB")
    List<ToolB> toolB;
}

ToolA.java工具A.java

@Getter
@AllArgsConstructor
public class ToolA{
    String uxA;
    String uxB;
    String uxC;
    String uxD;
}

Conversion转换

Gson gson = new Gson();
JsonMapper jsonMapper = JsonMapper.builder().build();
String toolsValue= jsonMapper.writeValueAsString(TOOLS_MAP);
Tools toolConverted = gson.fromJson(toolsValue, Tools .class);

toolsValue工具价值

{
   "ToolA":[
      {
         "uxA":"7",
         "uxB":"234.53",
         "uxC":"5345.23",
         "uxD":"0.155"
      }
   ],
   "ToolB":[
      {
         "state":"AZ",
         "compound_tax":"N",
         "federal_tax_percent":"5.0000",
         "provincial_tax_percent":"0.0000"
      }
   ]
}

try @SerializedName("ToolA") instead of @JsonProperty("ToolA") , last one is a jackson annotation尝试@SerializedName("ToolA")代替@JsonProperty("ToolA") ,最后一个是 jackson 注释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM