Json Object mapper读取Map

时间:2018-05-04 14:42:51

标签: java json arraylist objectmapper linkedhashmap

我有一个JSON字符串,我使用mapper.readValue()

转换为数据对象

我要转换的JSON字符串是:

[  
  {  
    "id":79107417,
    "email":"xxx@gmail.com",
    "optInType":"Single",
    "emailType":"Html",
    "dataFields":[  
      {  
        "key":"ABV",
        "value":0.0000000000
      },
      {  
        "key":"ACCOUNTNUMBER",
        "value":null
      },
      {  
        "key":"ACELLARING",
        "value":null
      },
      {  
        "key":"ACOLREDWINE",
        "value":null
      },
      {  
        "key":"ACOLWHITEWINE",
        "value":null
      },
      {  
        "key":"ADRINKING",
        "value":null
      },
      {  
        "key":"AENPRIMEUR",
        "value":null
      },
      {  
        "key":"AGIFTS",
        "value":null
      },
      {  
        "key":"AHASIBSTORAGE",
        "value":null
      },
      {  
        "key":"AINTERESTEDINRESALE",
        "value":null
      },
      {  
        "key":"AM_FROM_NAME",
        "value":"FINE+RARE"
      },
      {  
        "key":"AM_NAME",
        "value":"FINE+RARE"
      },
      {  
        "key":"AOV",
        "value":null
      },
      {  
        "key":"AREGBORDEAUX",
        "value":null
      },
      {  
        "key":"AREGCHAMPAGNE",
        "value":null
      },
      {  
        "key":"AREGFORTIFIEDS",
        "value":null
      },
      {  
        "key":"AREGITALY",
        "value":null
      },
      {  
        "key":"AREGNEWWORLD",
        "value":null
      },
      {  
        "key":"AREGREDBURGUNDY",
        "value":null
      },
      {  
        "key":"AREGRHONE",
        "value":null
      },
      {  
        "key":"AREGSPAIN",
        "value":null
      },
      {  
        "key":"AREGSPIRITS",
        "value":null
      },
      {  
        "key":"AREGUSA",
        "value":null
      },
      {  
        "key":"AREGWHITEBURGUNDY",
        "value":null
      },
      {  
        "key":"AROSE",
        "value":null
      },
      {  
        "key":"BLOCKBUSTER",
        "value":null
      },
      {  
        "key":"COMPANYNAME",
        "value":null
      },
      {  
        "key":"CONTACTNUMBER",
        "value":7740008599.0000000000
      },
      {  
        "key":"COUNTRY",
        "value":"France"
      },
      {  
        "key":"COUNTRYCODE",
        "value":"44"
      },
      {  
        "key":"DELIVERY-DATE",
        "value":null
      },
      {  
        "key":"DELIVERY-TIME",
        "value":null
      },
      {  
        "key":"DIRECT-SIGN-UP",
        "value":null
      },
      {  
        "key":"FIRSTNAME",
        "value":"Chloe"
      },
      {  
        "key":"FULLNAME",
        "value":""
      },
      {  
        "key":"GENDER",
        "value":null
      },
      {  
        "key":"LASTNAME",
        "value":"Ashton"
      },
      {  
        "key":"LASTSIDATE",
        "value":null
      },
      {  
        "key":"LASTSUBSCRIBED",
        "value":"2017-11-16T08:47:45"
      },
      {  
        "key":"M-B-REGION-VOL-NAME",
        "value":null
      },
      {  
        "key":"M-B-VINT-VOLUME-NAME",
        "value":null
      },
      {  
        "key":"M-B-WINE-VOLUME-NAME",
        "value":null
      },
      {  
        "key":"MOST-ACTIVE-MONTH",
        "value":null
      },
      {  
        "key":"NOTBLOCKBUSTER",
        "value":null
      },
      {  
        "key":"POSTCODE",
        "value":null
      },
      {  
        "key":"PRIVACYAGREEMENT",
        "value":true
      },
      {  
        "key":"REFERREDBY",
        "value":null
      },
      {  
        "key":"SALESORDERS",
        "value":null
      },
      {  
        "key":"STORAGE-NUMBER",
        "value":null
      },
      {  
        "key":"TITLE",
        "value":null
      },
      {  
        "key":"VINATONDONIABOUGHT",
        "value":null
      },
      {  
        "key":"WINE-NAME",
        "value":null
      }
    ],
    "status":"Subscribed"
  }
]

这是我的代码:

String jsin = "{\"dotMailerContact\":" + result.toString() + "}";
JSONObject jo = new JSONObject(jsin);
ObjectMapper mapper = new ObjectMapper();
StringReader reader = new StringReader(jo.toString()); // Json
// LASTSUBSCRIBED
MainDotMailerResponse moduleDto = mapper.readValue(reader, MainDotMailerResponse.class);
for (DotMailerContact dot: moduleDto.getDotMailerContact()) {
 ArrayList < LinkedHashMap < String, String >> dataFields = dot.getDataFields();
 for (Map < String, String > map: dataFields) {

  for (String key: map.keySet()) {
   System.out.println("key: " + key + " value " + String.valueOf(map.get(key)));
  }
 }
}

转换id,email等属性是有效的,但dataFields数组条目的转换方式错误(值,键):

[{value=0.0, key=ABV}, {value=null, key=ACCOUNTNUMBER}, {value=null, key=ACELLARING}, {value=null, key=ACOLREDWINE}, {value=null, key=ACOLWHITEWINE}, {value=null, key=ADRINKING}, {value=null, key=AENPRIMEUR}, {value=null, key=AGIFTS}, {value=null, key=AHASIBSTORAGE}, {value=null, key=AINTERESTEDINRESALE}, {value=FINE+RARE, key=AM_FROM_NAME}, {value=FINE+RARE, key=AM_NAME}, {value=null, key=AOV}, {value=null, key=AREGBORDEAUX}, {value=null, key=AREGCHAMPAGNE}, {value=null, key=AREGFORTIFIEDS}, {value=null, key=AREGITALY}, {value=null, key=AREGNEWWORLD}, {value=null, key=AREGREDBURGUNDY}, {value=null, key=AREGRHONE}, {value=null, key=AREGSPAIN}, {value=null, key=AREGSPIRITS}, {value=null, key=AREGUSA}, {value=null, key=AREGWHITEBURGUNDY}, {value=null, key=AROSE}, {value=null, key=BLOCKBUSTER}, {value=null, key=COMPANYNAME}, {value=7.740008599E9, key=CONTACTNUMBER}, {value=France, key=COUNTRY}, {value=44, key=COUNTRYCODE}, {value=null, key=DELIVERY-DATE}, {value=null, key=DELIVERY-TIME}, {value=null, key=DIRECT-SIGN-UP}, {value=Chloe, key=FIRSTNAME}, {value=, key=FULLNAME}, {value=null, key=GENDER}, {value=Ashton, key=LASTNAME}, {value=null, key=LASTSIDATE}, {value=2017-11-16T08:47:45, key=LASTSUBSCRIBED}, {value=null, key=M-B-REGION-VOL-NAME}, {value=null, key=M-B-VINT-VOLUME-NAME}, {value=null, key=M-B-WINE-VOLUME-NAME}, {value=null, key=MOST-ACTIVE-MONTH}, {value=null, key=NOTBLOCKBUSTER}, {value=null, key=POSTCODE}, {value=true, key=PRIVACYAGREEMENT}, {value=null, key=REFERREDBY}, {value=null, key=SALESORDERS}, {value=null, key=STORAGE-NUMBER}, {value=null, key=TITLE}, {value=null, key=VINATONDONIABOUGHT}, {value=null, key=WINE-NAME}]

你能帮忙吗,我在这里做错了什么?

请参阅以下主要类MainDotMailerResponse

    package uk.co.frw.test.DTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.annotate.JsonPropertyOrder;
import org.codehaus.jackson.map.annotate.JsonSerialize;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@JsonPropertyOrder(
{"dotMailerResponse", "dotMailerContact"})
public class MainDotMailerResponse
{

    @JsonProperty("dotMailerContact")
    @JsonIgnore
    private List<DotMailerContact> dotMailerContact = new ArrayList<DotMailerContact>();

    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    public List<DotMailerContact> getDotMailerContact()
    {
        return dotMailerContact;
    }

    public void setDotMailerContact(List<DotMailerContact> dotMailerContact)
    {
        this.dotMailerContact = dotMailerContact;
    }

}

请参阅子类

 package uk.co.frw.test.DTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder(
{"id", "email", "optInType", "emailType", "dataFields", "status"})
public class DotMailerContact
{

    @JsonProperty("id")
    private Integer id;
    @JsonProperty("email")
    private String email;
    @JsonProperty("optInType")
    private String optInType;
    @JsonProperty("emailType")
    private String emailType;
    @JsonProperty("dataFields")
    private ArrayList<LinkedHashMap<String, String>> dataFields;
    @JsonProperty("status")
    private String status;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("id")
    public Integer getId()
    {
        return id;
    }

    @JsonProperty("id")
    public void setId(Integer id)
    {
        this.id = id;
    }

    @JsonProperty("email")
    public String getEmail()
    {
        return email;
    }

    @JsonProperty("email")
    public void setEmail(String email)
    {
        this.email = email;
    }

    @JsonProperty("optInType")
    public String getOptInType()
    {
        return optInType;
    }

    @JsonProperty("optInType")
    public void setOptInType(String optInType)
    {
        this.optInType = optInType;
    }

    @JsonProperty("emailType")
    public String getEmailType()
    {
        return emailType;
    }

    @JsonProperty("emailType")
    public void setEmailType(String emailType)
    {
        this.emailType = emailType;
    }

    @JsonProperty("dataFields")
    public ArrayList<LinkedHashMap<String, String>> getDataFields()
    {
        return dataFields;
    }

    @JsonProperty("dataFields")
    public void setDataFields(ArrayList<LinkedHashMap<String, String>> dataFields)
    {
        this.dataFields = dataFields;
    }

    @JsonProperty("status")
    public String getStatus()
    {
        return status;
    }

    @JsonProperty("status")
    public void setStatus(String status)
    {
        this.status = status;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties()
    {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value)
    {
        this.additionalProperties.put(name, value);
    }

}

0 个答案:

没有答案