使用Jackson创建一个有效的JSON,该JSON包含一个数组和一个自定义字段



我下面的代码使用Jackson生成JSON,就数据而言是可以的,但它不是JSON

生成的JSON不会传递JSONLint,因为它的方括号周围有引号,逗号周围有引号。它还反斜杠,我从中删掉了-但我很确定我在下面做的事情有问题

以下是我要做的:

JSON应该如下所示(除了skip字段,我在这里添加了这个字段,以强调它在序列化时将从Object中省略):

{
    "users": [{
        "foo": "abc1",
        "bar": "def1",
        "skip": "this field is skipped"
    }, {
        "foo": "abc2",
        "bar": "def2",
        "skip": "this field is skipped"
    }],
    "uri": "/users"
}

usersKey是一个users数组,上面显示了2个元素。跳过字段不应该是最终json的一部分,而是每个"用户"对象的一个部分

添加了URI字段

我的代码在下面。它成功地跳过了"跳过"字段,并且如果消除了奇怪的格式,它成功地构建了一个几乎是JSON的字符串。但我承认这个代码很可怕,它可以更好(尽管我不知道怎么做,因为我是新手)

你问的奇怪格式是什么?

  1. 反斜杠(您可以看到我使用hackey regex消除了反斜杠)
  2. 围绕[和]的报价
  3. 引号,(逗号)

代码:

get("/users", (request, response) -> {
    //this is the array of objects
    Object[] allUsers = listenUp.get_all_users();
    //Ignore this field per ListenUpUser object
    String[] ignorableFieldNames = { "skip" };
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    mapper.addMixIn(Object.class, PropertyFilterMixIn.class);
    FilterProvider filters = new SimpleFilterProvider()
            .addFilter("filter properties by name",
                    SimpleBeanPropertyFilter.serializeAllExcept(
                            ignorableFieldNames));
    ObjectWriter writer = mapper.writer(filters);
    ArrayNode array = mapper.createArrayNode();
    for(int i = 0; i < allUsers.length; i++) {
        array.add(writer.writeValueAsString(allUsers[i]));
    }
    JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
    ObjectNode child = mapper.createObjectNode();
    child.put("users", array.toString());
    child.put("uri", "/users");
    response.status(200);
    response.type("application/json");
    String a = child.toString().replaceAll("\\", "");
    return a;
});

这已在文件顶部定义(针对跳过字段逻辑)

@JsonFilter("filter properties by name")
class PropertyFilterMixIn {}

我认为您可以使用Hashmap<String, Object>。Jackson将了解如何将filter应用于数组中的Object,并将跳过它找到的任何其他对象(String/array)。这是一个演示,适用于我:

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import org.json.JSONException;
import java.io.IOException;
import java.util.HashMap;
public class test12 {
    public static void main(String[] args) throws IOException, JSONException {
        Object[] allUsers = get_all_users();
        String[] ignorableFieldNames = {"skip"};
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.addMixIn(Object.class, PropertyFilterMixIn.class);
        FilterProvider filters = new SimpleFilterProvider()
                .addFilter("filter properties by name",
                        SimpleBeanPropertyFilter.serializeAllExcept(
                                ignorableFieldNames));
        mapper.setFilterProvider(filters);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("users", allUsers);
        map.put("uri", "/users");
        String result = mapper.writeValueAsString(map);
        System.out.println(result);
    }
    @JsonFilter("filter properties by name")
    public static class PropertyFilterMixIn {
    }
    private static Object[] get_all_users() {
        User user1 = new User();
        user1.foo = "abc1";
        user1.bar = "def1";
        user1.skip = "this field is skipped";
        User user2 = new User();
        user2.foo = "abc2";
        user2.bar = "def2";
        user2.skip = "this field is skipped";
        return new Object[]{user1, user2};
    }
    public static class User {
        public String foo;
        public String bar;
        public String skip;
    }
}

结果:

{
  "users" : [ {
    "foo" : "abc1",
    "bar" : "def1"
  }, {
    "foo" : "abc2",
    "bar" : "def2"
  } ],
  "uri" : "/users"
}