1

只是想知道是否有办法做到这一点 - 我有一堂课,比如

class MyClass {
    private String name;
    private String address;
    private String number;
}

当我使用杰克逊将它序列化为 Json 时,我想将字符串变量包装在一起,所以它看起来像

{
    "Strings": {
         "name" : "value",
         "address" : "value"
    }
 }

如果不将这些变量包装在 MyClass 内的 List 或 Map 类中......这可能吗?

4

5 回答 5

2

您还可以在 POJO 类中添加“Strings”、“Intigers”等的其他 getter。这些方法应返回Map's 作为结果。考虑下面的代码:

import java.util.LinkedHashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonProgram {

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        MyClass myClass = new MyClass();
        myClass.setAddress("New York, Golden St. 1");
        myClass.setName("James Java");
        myClass.setNumber("444");

        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(myClass));
    }
}

class MyClass {

    private String name;
    private String address;
    private String number;

    @JsonIgnore
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @JsonIgnore
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @JsonIgnore
    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    @JsonProperty(value = "Strings")
    public Map<String, String> getStrings() {
        Map<String, String> map = new LinkedHashMap<String, String>();
        map.put("name", getName());
        map.put("address", getAddress());
        map.put("number", getNumber());
        return map;
    }
}

结果:

{
  "Strings" : {
    "name" : "James Java",
    "address" : "New York, Golden St. 1",
    "number" : "444"
  }
}

这可能不是您可以使用的最优雅的解决方案,但它很简单。

于 2013-07-11T23:16:19.597 回答
1

Jackson 使用您的类结构来决定 json 结构。如果您需要将类属性包装在另一个属性中,那么您需要考虑包装。如果不需要,则无需创建集合。只需将名称和地址放在不同的类中,然后根据需要为其包装器命名该类即可。然后在 MyClass 中添加包装类的实例。

于 2013-07-10T09:48:30.877 回答
0

I use JsonSerializer to do this for me with Spring, it is very simple.

you create a class that extends JsonSerializer

like this one:

 public class ServerResposeSerializer extends JsonSerializer<ServerResponse> {
@Override
public void serialize(ServerResponse objectWrapper, JsonGenerator generator,
        SerializerProvider provider) throws IOException,
        JsonProcessingException {
    generator.writeStartObject();
    generator.writeNumberField("http_status", objectWrapper.getStatus().value());
    generator.writeStringField("message",objectWrapper.getMessage());
    generator.writeArrayFieldStart("objects");      
    List<?> objects = objectWrapper.getObjects();       
    for (Object object: objects)
    {
        generator.writeObject(object);
    }       
    generator.writeEndArray();
    generator.writeEndObject();
}

}

Annotate ServerResponse with the

@JsonSerialize (using = ServerResposeSerializer.class)

Now you can format any class you want to produce any kind of JSON String

于 2013-07-11T23:25:23.370 回答
0

一个有点老的问题,但是在寻找相同问题的解决方案时,我遇到了这个答案

基本上使用函数创建一个ObjectWriterfrom ,然后为映射设置,即ObjectMapperwriter()rootName

MyClass myclass = new MyClass()
myclass.setName("a");
myclass.setAddress("b");
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer().withRootName("Strings");
String json = writer.writeValueAsString(myclass);

输出将是

{
   "Strings": {
     "name" : "a",
     "address" : "b"
    }  
}

笔记:

ObjectMapper 是一个线程安全的,文档说它应该尽可能重用。

于 2015-09-10T11:38:11.817 回答
0
@Configuration
public class JacksonConfiguration {

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
        mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
        //mapper.setSerializationInclusion(Include.NON_DEFAULT);
        return mapper;
    }

    
}
于 2018-03-14T12:56:31.370 回答