简体   繁体   English

Jackson - 将JSON字符串字段映射到只有一个字段(字符串)的类/ pojo

[英]Jackson - Map JSON string field to class/pojo that has only one field (a string)

I am writing a JSON interface/library to work with Bugzilla's webservice. 我正在编写一个JSON接口/库来与Bugzilla的webservice一起工作。

Is this possible using an annotation or something? 这可能是使用注释或其他东西吗? Or am I to write a custom deserializer for every instance like this? 或者我是否为这样的每个实例编写自定义反序列化器?

I've tried doing some research, and found some information about a Value Instantiator or using constructors, but isn't using constructors anti-bean like? 我已经尝试过做一些研究,发现了一些关于Value Instantiator或使用构造函数的信息,但是不使用构造函数反bean吗? I've found documentation to be sparse or hard to understand with the newer features. 我发现使用较新的功能,文档很稀疏或难以理解。

Example: 例:

public class Bug{
  // Bug info, strings, ints, yadda yadda.
 private User creator;   // creator of the bug, json is like   {"creator":"blahblah@email.com"}
}

public class User{
 private String username;
}
//insert setter/getter.

The reason I am using a pojo for One field is because this User class is extended by another that has more fields. 我在一个字段中使用pojo的原因是因为此User类由具有更多字段的另一个扩展。 While I can implement a constructor to achieve this effect, doing so means I have to implement the same constructor for all other subclasses. 虽然我可以实现一个构造函数来实现这种效果,但这样做意味着我必须为所有其他子类实现相同的构造函数。 I feel like there is something similar to @JsonValue but for setting (I tried JsonCreator on my setter but still got the same error as I have been getting below). 我觉得有类似于@JsonValue的东西,但是对于设置(我在我的setter上尝试了JsonCreator,但仍然得到了与我下面相同的错误)。

"Can not instantiate value of type [simple type, class User], from String value; no single-String constructor/factory method" “无法实例化类型[simple type,class User]的值,来自String值;没有单一的String构造函数/工厂方法”

Thanks. 谢谢。

Use @JsonCreator for deserializing and use @JsonValue for Serializing. 使用@JsonCreator进行反序列化并使用@JsonValue进行序列化。 Have tested the code. 测试了代码。 Please make sure that getJsonString() is public method. 请确保getJsonString()是公共方法。

For example: 例如:

public class User{
    private String username;

    @JsonValue
    public String getJsonString() {
         return username;
    }

    @JsonCreator
    private static User parseJson(String jsonStr) {
        User u = new User();
        u.username = jsonStr;
        return u;
    }

    public static void main(String[] args) {
        User u = new User();
        u.userName = "Niraj";

        ObjectMapper m = new ObjectMapper();
        String jsonString = m.writeValueAsString(u);
        User u1 = m.readValue(jsonString, User.class);
    }
}

You don't need to use a @JsonCreator or @JsonValue or a construtor, Jackson will work OTTB with javabean methods: Try this instead: 您不需要使用@JsonCreator或@JsonValue或construtor,Jackson将使用javabean方法处理OTTB:请尝试以下方法:

public class User{
    private String username;

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    public static void main(String[] args) throws Exception {
        User u = new User();
        u.setUsername("name");

        ObjectMapper m = new ObjectMapper();
        String userString = m.writeValueAsString(u);
        System.out.println(userString);
        User u1 = m.readValue(userString, User.class);
        System.out.println(u1.getUsername());
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jackson:转换 JSON 代表 Map<string,object> 单场 POJO 到 Map<string,string> 字段值?</string,string></string,object> - Jackson: convert JSON representing a Map<String,Object> of single-field POJO's to a Map<String,String> of field values? Map 多个 Java pojo 字段到一个 json 字段与 Z7930C951E609E461E82ZED 注释6004 - Map multiiple Java pojo fields to one json field with Jackson annotations POJO 有一个 byte[] 字段,但与之对应的 JSON 有 string 字段。 如何将字符串转换为字节[]? - POJO has a byte[] field, but JSON corresponding to it has string field. How to convert the string to byte[]? 杰克逊将字符串字段解析为JSON - Jackson parse string field as JSON Jackson 如何 map 一个 Pojo 字段到 2 个(json)字段(相同的内容,不同的名称) - Jackson how map one Pojo field to 2 (json) fields (same content, different name) 在 Java 中,如何将 JSON 或 XML 字符串映射到同一个 POJO,但 XML 具有与 JSON 不同的字段/属性名称? - In Java, how can I map a string that is either JSON or XML to the same POJO, but with the XML having one different field/attribute name from the JSON? 如何将字符串 json 映射到 POJO 类? - How to map string json to POJO class? 仅解析大JSON字符串中的一个字段 - Parse only one field in a large JSON string 只有一个 String 字段的模型类 - Model class with only one String field 使用Jackson将名为“class”的字段反序列化为POJO - Deserializing a field named “class” into a POJO using Jackson
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM