Java Json对象映射器读取映射

Java Json对象映射器读取映射,java,json,arraylist,objectmapper,linkedhashmap,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 },

我有一个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)));
  }
 }
}
你能帮帮我吗,我做错了什么

请参阅下面的主类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;
    }

}
包uk.co.frw.test.DTO;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入org.codehaus.jackson.annotate.JsonIgnore;
导入org.codehaus.jackson.annotate.JsonProperty;
导入org.codehaus.jackson.annotate.JsonPropertyOrder;
导入org.codehaus.jackson.map.annotate.JsonSerialize;
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonPropertyOrder(
{“dotMailerResponse”,“dotMailerContact”})
公共类MainDotMailerResponse
{
@JsonProperty(“dotMailerContact”)
@杰索尼奥雷
private List dotMailerContact=new ArrayList();
@杰索尼奥雷
私有映射additionalProperties=new HashMap();
公共列表getDotMailerContact()
{
返回dotMailerContact;
}
public void setDotMailerContact(列出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);
    }

}
包uk.co.frw.test.DTO;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.LinkedHashMap;
导入java.util.Map;
导入com.fasterxml.jackson.annotation.JsonAnyGetter;
导入com.fasterxml.jackson.annotation.JsonAnySetter;
导入com.fasterxml.jackson.annotation.JsonIgnore;
导入com.fasterxml.jackson.annotation.JsonInclude;
导入com.fasterxml.jackson.annotation.JsonProperty;
导入com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder(
{“id”、“电子邮件”、“选项类型”、“电子邮件类型”、“数据字段”、“状态”})
公共类DotMailerContact
{
@JsonProperty(“id”)
私有整数id;
@JsonProperty(“电子邮件”)
私人字符串电子邮件;
@JsonProperty(“期权类型”)
私有字符串选项类型;
@JsonProperty(“emailType”)
私有字符串类型;
@JsonProperty(“数据字段”)
私有ArrayList数据字段;
@JsonProperty(“状态”)
私有字符串状态;
@杰索尼奥雷
私有映射additionalProperties=new HashMap();
@JsonProperty(“id”)
公共整数getId()
{
返回id;
}
@JsonProperty(“id”)
公共无效集合id(整数id)
{
this.id=id;
}
@JsonProperty(“电子邮件”)
公共字符串getEmail()
{
回复邮件;
}
@JsonProperty(“电子邮件”)
公用电子邮件(字符串电子邮件)
{
this.email=电子邮件;
}
@JsonProperty(“期权类型”)
公共字符串getOptInType()
{
返回选项类型;
}
@JsonProperty(“期权类型”)
public void setOptInType(字符串optInType)
{
this.optInType=optInType;
}
@JsonProperty(“emailType”)
公共字符串getEmailType()
{
返回电子邮件类型;
}
@JsonProperty(“emailType”)
public void setEmailType(字符串emailType)
{
this.emailType=emailType;
}
@JsonProperty(“数据字段”)
公共ArrayList getDataFields()
{
返回数据字段;
}
@JsonProperty(“数据字段”)
公共无效setDataFields(ArrayList数据字段)
{
this.dataFields=数据字段;
}
@JsonProperty(“状态”)
公共字符串getStatus()
{
返回状态;
}
@JsonProperty(“状态”)
公共无效设置状态(字符串状态)
{
这个状态=状态;
}
@JsonAnyGetter
公共映射getAdditionalProperties()
{
返回此。附加属性;
}
@JSONANYSETER
public void setAdditionalProperty(字符串名称、对象值)
{
this.additionalProperties.put(名称、值);
}
}

发布您的MainDotMailerResponse bean,这是唯一可以理解的,您如何映射键/值我不理解。字符串表示形式中的顺序无关紧要。感谢您的即时响应,我已经附加了类nowsystem.out.println打印,如下键:value 0.0键:key-value-ABV键:value-value-null键:key-value-ACCOUNTNUMBER键:value-value-null键:key-value-acellaring对不起,我的错!我需要创建一个单独的数据字段类,其中包含key和value属性,并将其映射到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);
    }

}