简体   繁体   中英

JSON parser not returning numbers

I have the following JSON structure:

{
   "status": "Completed",
   "notes": null,
   "members":    {
      "0":       {
         "year": "2",
         "details":          {
            "id": "14899975",
            "anotherId": "11013306"
         },
         "aName": "Fred",
         "amounts":          {
            "First": 589.48,
            "Second": 1000,
            "Third": 339.48
         }
      },
      "1":       {
         "year": "2",
         "details":          {
            "id": "14899976",
            "anotherId": "11013306"
         },
         "aName": "George",
         "amounts":          {
            "First": 222.22,
            "Second": 2000,
            "Third": 22.22
         }
      },
      "2":       {
         "year": 1,
         "details":          {
            "id": "14899976",
            "anotherId": "11013306"
         },
         "aName": "Albert",
         "amounts":          {
            "First": 333.33,
            "Second": 3000,
            "Third": 33.33
         },
      }
   }
}

I am using Spring RESTTemplate and JacksonMapping2HttpMessageConverter, and the following structures to receive the result of parsing the above JSON structure:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Response {
  private String            status;
  private String            notes;
  private Map<String,Struct1>   quotes;
}

@JsonIgnoreProperties(ignoreUnknown = true)
 class Struct1 {
    private int         year;
    private Struct2     details;
    private String          aName;
    private Struct3     amounts;
}

@JsonIgnoreProperties(ignoreUnknown = true)
 class Struct2 {
    private String id;
    private String anotherId;
}

@JsonIgnoreProperties(ignoreUnknown = true)
 class Struct3 {
    private float First;
    private float Second;
    private float Third;
 }

All of these also have appropriate setters and getters for all fields.

My problem is that the number values in Struct3 are not filled in. I've tried making them float, Float, String, and BigDecimal, and the result is either null or 0.0.

I've tried putting a breakpoint in the setter for the first field, hoping

What am I missing? Can the capital letters in the JSON be causing a problem, do I need alternate field names?

It turned out to be the capital letters at the beginning of the field names; I added annotations like @JsonProperty("First") on the line before the getter of the field, and renamed the field to first , and now it's working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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