现在,我正在尝试解析以下格式的传入JSON:
{
<email>: {
<name>: <string>, # setting value
...
},
...
}
例如:
{
"aaa@example.com": {
"statement": true
},
"bbb@example.com": {
"statement": false
}
}
我也不知道有多少电子邮件将在这个JSON。我有点困惑,你怎么能在不知道它的属性名称的情况下获得所有这些与杰克逊的电子邮件,我想知道这是否可能。
到目前为止,我的代码如下:
public class GDPRConsent extends Model {
@JsonIgnore
private static final String GDPR_CONSENT = "gdprConsent";
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty
private ArrayList<String> emails;
@JsonProperty("serviceDataCollection")
private String dataCollection;
@JsonProperty("serviceDataCollection")
public String getDataCollectionConsent() {
return dataCollection;
}
@JsonProperty
public ArrayList<String> getEmails() {
return emails;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@Override
public String getId() {
return GDPR_CONSENT;
}
}
下面是我的解析器:
public static <T> T parseObject(String sourceJson, Class<T> classToParse) {
T parsedObject = null;
try {
parsedObject = sObjectMapper.readValue(sourceJson, classToParse);
} catch (JsonParseException e) {
LogUtils.d(LOG_TAG, "parseObject JsonParseException: " + e.toString());
} catch (JsonMappingException e) {
LogUtils.d(LOG_TAG, "parseObject JsonMappingException: " + e.toString());
} catch (IOException e) {
LogUtils.d(LOG_TAG, "parseObject IOException: " + e.toString());
}
return parsedObject;
}
即使我知道JSON正在被传入,但我目前仍得到一个空结果返回。
发布于 2018-07-20 07:25:26
JSON我正在尝试解析这种格式的传入
正如在您的duplicate question中所解释的,您可以解析为Map
。
public class EmailData {
private boolean statement;
public boolean isStatement() {
return this.statement;
}
public void setStatement(boolean statement) {
this.statement = statement;
}
@Override
public String toString() {
return "EmailData[statement=" + this.statement + "]";
}
}
测试
String json = "{" +
"\"aaa@example.com\": {" +
"\"statement\": true" +
"}," +
"\"bbb@example.com\": {" +
"\"statement\": false" +
"}" +
"}";
ObjectMapper mapper = new ObjectMapper();
TypeReference<HashMap<String, EmailData>> typeRef = new TypeReference<>() {/**/};
HashMap<String, EmailData> emails = mapper.readValue(json, typeRef);
System.out.println(emails);
输出
{aaa@example.com=EmailData[statement=true], bbb@example.com=EmailData[statement=false]}
如果您更喜欢@JsonAnySetter
方法,您可以这样做:
public class Content {
private List<EmailData> emailData = new ArrayList<>();
@JsonAnySetter
public void addEmail(String name, EmailData value) {
value.setEmail(name);
this.emailData.add(value);
}
@Override
public String toString() {
return this.emailData.toString();
}
}
public class EmailData {
private String email;
private boolean statement;
@JsonIgnore
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean isStatement() {
return this.statement;
}
public void setStatement(boolean statement) {
this.statement = statement;
}
@Override
public String toString() {
return "EmailData[email=" + this.email + ", statement=" + this.statement + "]";
}
}
测试
String json = "{" +
"\"aaa@example.com\": {" +
"\"statement\": true" +
"}," +
"\"bbb@example.com\": {" +
"\"statement\": false" +
"}" +
"}";
ObjectMapper mapper = new ObjectMapper();
Content content = mapper.readValue(json, Content.class);
System.out.println(content);
输出
[EmailData[email=aaa@example.com, statement=true], EmailData[email=bbb@example.com, statement=false]]
https://stackoverflow.com/questions/51432808
复制相似问题