gpt4 book ai didi

Java Jackson 序列化为数组

转载 作者:行者123 更新时间:2023-12-01 12:55:54 29 4
gpt4 key购买 nike

我怎样才能消除以下类以获得像{“params”:[[],[],1,2 ....]}这样的值。任何帮助,将不胜感激。

    public class Search{
public List<Double> minLatLng=new ArrayList<Double>();
public List<Double> maxLatLng=new ArrayList<Double>(); //These are bounding co-ordinates
public List<Integer> ids=new ArrayList<Integer>();
public List<Integer> owner=new ArrayList<Integer>();
public float minScore;
public float maxScore;
public long minLength;
public long maxLength;
public String flag;
public String language;
public String keywords;
public List<Integer> groupIds=new ArrayList<Integer>();
public List<Integer> characteristic=new ArrayList<Integer>();
public List<Integer> theme=new ArrayList<Integer>();
public boolean acitivity;
public List<Integer> activityOwner=new ArrayList<Integer>();
public boolean event;
public List<Integer> eventOwner=new ArrayList<Integer>();
public List<Integer> favouriteUser=new ArrayList<Integer>();
}

The out put should be look like { "params":[[],[],1,2....]} how can i use jackson for getting this value

最佳答案

您可以添加用@JsonValue注释的方法您的类的注释将从反射中获取字段值并以数组映射的形式返回它们。

这是一个例子:

public class JacksonBeanToArray {
public static class Search {
public List<Double> minLatLng=new ArrayList<Double>();
public List<Double> maxLatLng=new ArrayList<Double>(); //These are bounding co-ordinates
public List<Integer> ids=new ArrayList<Integer>();
public List<Integer> owner=new ArrayList<Integer>();
public float minScore;
public float maxScore;
public long minLength;
public long maxLength;
public String flag;
public String language;
public String keywords;
public List<Integer> groupIds=new ArrayList<Integer>();
public List<Integer> characteristic=new ArrayList<Integer>();
public List<Integer> theme=new ArrayList<Integer>();
public boolean acitivity;
public List<Integer> activityOwner=new ArrayList<Integer>();
public boolean event;
public List<Integer> eventOwner=new ArrayList<Integer>();
public List<Integer> favouriteUser=new ArrayList<Integer>();

@JsonValue
public Map<String, Object[]> getAsParamMap() throws IllegalAccessException {
Object[] result = new Object[getClass().getFields().length];
for (int i = 0; i < result.length; i++) {
result[i] = getClass().getFields()[i].get(this);
}
return Collections.singletonMap("params", result);
}
}

public static void main(String[] args) throws JsonProcessingException {
Search search = new Search();
search.acitivity = true;
search.maxLatLng = Arrays.asList(0.44, 5.);
search.maxScore = 4;
search.language = "EN";
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(search));
}

}

输出:

{
"params" : [ [ ], [ 0.44, 5.0 ], [ ], [ ], 0.0, 4.0, 0, 0, null, "EN", null, [ ], [ ], [ ], true, [ ], false, [ ], [ ] ]
}

关于Java Jackson 序列化为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23903575/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com