我下面的代码使用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"
}
用户密钥是用户数组-上面显示了2个元素。跳过字段不应该是最终json的一部分,而应是每个“用户”对象的一部分
URI字段已添加
我的代码如下。如果消除了怪异的格式,它会成功跳过“ skip”字段,并成功构建几乎是JSON的String。但是我承认这段代码是可怕的,它可以做得更好(尽管自从我是新手以来我不知道如何)
您问什么奇怪的格式?
反斜杠(您可以看到我已经使用hackey regex消除了)
[和]周围的引号
左右的引号,(逗号)
码:
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>
。杰克逊将了解如何将过滤器应用于数组中的Object
,并且将跳过其找到的任何其他对象(字符串/数组)。这是演示,对我有用:
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"
}