Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Jackson获取空JSON_Java_Json_Jackson - Fatal编程技术网

Java 使用Jackson获取空JSON

Java 使用Jackson获取空JSON,java,json,jackson,Java,Json,Jackson,现在我正在尝试解析以下格式的传入JSON: { <email>: { <name>: <string>, # setting value ... }, ... } 我也不知道有多少电子邮件将在这个JSON。我有点困惑,你怎么能在不知道这家公司的名称的情况下收到杰克逊的所有电子邮件,我想知道这是否可能 以下是我目前的代码: public class GDPRConsent extends Model { @JsonIgno

现在我正在尝试解析以下格式的传入JSON:

{
  <email>: {
    <name>: <string>,     # setting value
     ...
  },
  ...
}
我也不知道有多少电子邮件将在这个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类gdpr扩展模型{
@杰索尼奥雷
私有静态最终字符串GDPR_approve=“gdprapprove”;
私有映射additionalProperties=new HashMap();
@JsonProperty
私人ArrayList电子邮件;
@JsonProperty(“serviceDataCollection”)
私有字符串数据收集;
@JsonProperty(“serviceDataCollection”)
公共字符串GetDataCollectionApprovement(){
返回数据收集;
}
@JsonProperty
公共数组列表getEmails(){
回复邮件;
}
@JsonAnyGetter
公共映射getAdditionalProperties(){
返回此。附加属性;
}
@凌驾
公共字符串getId(){
返回GDPR_同意书;
}
}

这是我的解析器:

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;
}
publicstatictpassetobject(字符串sourceJson,类classToParse){
T parsedObject=null;
试一试{
parsedObject=sObjectMapper.readValue(sourceJson,classToParse);
}捕获(JSONParsee异常){
LogUtils.d(LOG_标记,“parseObject JsonParseException:+e.toString());
}捕获(JsonMappingException e){
LogUtils.d(LOG_标记,“parseObject JsonMappingException:+e.toString());
}捕获(IOE异常){
LogUtils.d(LOG_标记,“parseObject IOException:+e.toString());
}
返回parsedObject;
}
尽管我知道正在传入JSON,但当前返回的结果为空

我试图解析这种格式的传入JSON

如中所述,您可以解析为
映射

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 + "]";
    }
}
试验


如果您更喜欢
@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 + "]";
    }
}
输出

[EmailData]电子邮件=aaa@example.com,statement=true],EmailData[电子邮件=bbb@example.com,statement=false]]

如果JSON只包含示例中给出的数据,那么它对应于
类型引用,这基本上是字符串到布尔值的映射。示例解析器如下所示(不需要额外的POJO):

import java.io.IOException;
导入java.util.Map;
导入com.fasterxml.jackson.core.type.TypeReference;
导入com.fasterxml.jackson.databind.ObjectMapper;
公共类JSONParser{
静态最终字符串测试_JSON=“{”
+" \"aaa@example.com\": {"
+“\”语句\“:true”
+"},"
+"\"bbb@example.com\": {"
+“\”语句\“:false”
+"}"
+"}";
公共静态void main(字符串…参数){
ObjectMapper mapper=新的ObjectMapper();
试一试{
Map jsonAsNestedMap=mapper.readValue(
TEST_JSON,new TypeReference(){
});
System.out.println(jsonAsNestedMap);
}捕获(IOE异常){
抛出新的运行时异常(e);
}        
}
}
这将打印出来

{aaa@example.com={statement=true}, bbb@example.com={statement=false}} {aaa@example.com={statement=true},bbb@example.com={statement=false}} 如果JSON的最里面的值更复杂,那么可以使用
TypeReference

静态最终字符串测试_JSON=“{”
+" \"aaa@example.com\": {"
+“语句”:true
+“\“其他\u属性\”:\“值1”
+"},"
+"\"bbb@example.com\": {"
+“语句”:false
+“\“另一个\u属性\”:\“值2”
+"}"
+"}";    
//...    
公共静态void main(字符串…参数){
//...
Map jsonAsNestedMap=mapper.readValue(
TEST_JSON,new TypeReference(){
});
//...
}
可以通过普通映射迭代和访问器方法访问单个属性:

for (Entry<String, Map<String, Object>> e : jsonAsNestedMap.entrySet()) {
    System.out.println("email:" + e.getKey() + ", another_property: " 
        + e.getValue().get("another_property")); 
}
for(条目e:jsonAsNestedMap.entrySet()){
System.out.println(“电子邮件:+e.getKey()+”,另一个_属性:
+e.getValue().get(“另一个_属性”);
}
这会给

email:aaa@example.com, another_property: value 1 email:bbb@example.com, another_property: value 2 电邮:aaa@example.com,另一个_属性:值1 电邮:bbb@example.com,另一个_属性:值2
有任何异常被记录吗?嘿,Mick,你知道如何按照另一个答案提出的方式来做吗?我是如何在我的帖子中找到它的,也就是将它作为自己的类与@JsonPropertys等等一起使用的。这样做有可能吗?你可以看看JsonAnySetter的方法(Andreas也提到过)。今天晚些时候,我可以尝试提出另一个完整的示例。
import java.io.IOException;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JSONParser {

    static final String TEST_JSON = "{"
            +" \"aaa@example.com\": {"
            +"  \"statement\": true"
            +"},"
            +"\"bbb@example.com\": {"
            +"  \"statement\": false"
            +"}"
            +"}";


    public static void main (String... args) {

        ObjectMapper mapper = new ObjectMapper();

        try {
            Map<String, Map<String, Boolean>> jsonAsNestedMap = mapper.readValue(
                    TEST_JSON, new TypeReference<Map<String, Map<String, Boolean>>>() {
            });
            System.out.println(jsonAsNestedMap);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }        
    }

}
{aaa@example.com={statement=true}, bbb@example.com={statement=false}}
static final String TEST_JSON = "{"
            +" \"aaa@example.com\": {"
            +"  \"statement\": true,"
            +"  \"another_property\" : \"value 1\"" 
            +"},"
            +"\"bbb@example.com\": {"
            +"  \"statement\": false,"
            +"  \"another_property\" : \"value 2\"" 
            +"}"
            +"}";    
//...    
public static void main (String... args) {
    //...
    Map<String, Map<String, Object>> jsonAsNestedMap = mapper.readValue(
                    TEST_JSON, new TypeReference<Map<String, Map<String, Object>>>() {

        });
//...
}
for (Entry<String, Map<String, Object>> e : jsonAsNestedMap.entrySet()) {
    System.out.println("email:" + e.getKey() + ", another_property: " 
        + e.getValue().get("another_property")); 
}
email:aaa@example.com, another_property: value 1 email:bbb@example.com, another_property: value 2