Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java jackson json序列化列表的列表的特定属性值_Java_Json_Jackson - Fatal编程技术网

Java jackson json序列化列表的列表的特定属性值

Java jackson json序列化列表的列表的特定属性值,java,json,jackson,Java,Json,Jackson,大家好,, 我想删除从另一个对象(作为属性)返回时列表中的对象的属性和属性名 我有以下课程: public class Base { protected String type; public String getType() { return type; } public void setType(String type) { this.type = type; } } public class A extends

大家好,, 我想删除从另一个对象(作为属性)返回时列表中的对象的属性和属性名

我有以下课程:

public class Base {
    protected String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

public class A extends Base {
    private double[] value;

    public A() { type = "A"; }

    public A(double[] value) {
        this();
        setValue(value);
    }

    public double[] getValue() {
        return value;
    }

    public void setValue(double[] value) {
        this.value = value;
    }
}

public class B extends Base {
    private List<A> value = new ArrayList<A>();

    public B() { type = "B"; }

    public B(List<A> value) {
        this();
        setValue(value);
    }

    public List<A> getValue() { return value; }

    public void setValue(List<A> value) {
        this.value = value;
    }
}

public class C extends Base {
    private List<B> value = new ArrayList<B>();

    public C() { type = "C"; }

    public List<B> getValue() {
        return value;
    }

    public void setValue(List<B> value) {
        this.value = value;
    }
}
但我想让它像:

=== com.example.A ===
{
  "type" : "A",
  "value" : [ 1.0, 2.0 ]
}
=== com.example.B ===
{
  "type" : "B",
  "value" : [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
}
=== com.example.C ===
{
  "type" : "C",
  "value" : [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ]
}
当从B返回A的属性“type”和从C返回B的属性“type”时,我可以使用以下方法删除A的属性“type”:

    @JsonIgnoreProperties( { "type" })
    public List<A> getValue() {
        return value;
    }
...
    @JsonIgnoreProperties( { "type" })
    public List<B> getValue() {
        return value;
    }

我可以使用什么注释来获得所需的输出?或者我必须重新设计我的类吗?

修改B类和C类,如下所示:

在value中添加了
@JsonIgnore
注释,以避免打印值,并创建了一个新方法
getValueForJson()
,返回类型为
List
,其输出是带有类
a
值的深度解析结果。此新方法添加了注释
@JsonProperty
,其值为
“value”
,以便将其视为名为
value
的成员

class B extends Base {
    @JsonIgnore
    private List<A> value = new ArrayList<A>();

    public B() {
        type = "B";
    }

    public B(List<A> value) {
        this();
        setValue(value);
    }

    public List<A> getValue() {
        return value;
    }

    public void setValue(List<A> value) {
        this.value = value;
    }

    @JsonProperty("value")
    public List<double[]> getValueForJson(){
        List<double[]> res = new ArrayList<double[]>();
        value.forEach(a -> res.add(a.getValue()));
        return res;
    }
}

class C extends Base {
    @JsonIgnore
    private List<B> value = new ArrayList<B>();

    public C() {
        type = "C";
    }

    public List<B> getValue() {
        return value;
    }

    public void setValue(List<B> value) {
        this.value = value;
    }

    @JsonProperty("value")
    public List<double[]> getValueForJson(){
        List<double[]> res = new ArrayList<double[]>();
        value.forEach(b -> b.getValue().forEach(a -> res.add(a.getValue())));
        return res;
    }
}
B类扩展了基础{
@杰索尼奥雷
私有列表值=新的ArrayList();
公共图书馆B(){
type=“B”;
}
公共B(列表值){
这个();
设置值(值);
}
公共列表getValue(){
返回值;
}
公共无效设置值(列表值){
这个值=值;
}
@JsonProperty(“值”)
公共列表getValueForJson(){
List res=new ArrayList();
value.forEach(a->res.add(a.getValue());
返回res;
}
}
C类扩展基础{
@杰索尼奥雷
私有列表值=新的ArrayList();
公共C(){
type=“C”;
}
公共列表getValue(){
返回值;
}
公共无效设置值(列表值){
这个值=值;
}
@JsonProperty(“值”)
公共列表getValueForJson(){
List res=new ArrayList();
forEach(b->b.getValue().forEach(a->res.add(a.getValue()));
返回res;
}
}
公共A类扩展基础{
私人双[]值;
公共A(){
type=“A”;
}
公共A(双[]值){
这个();
设置值(值);
}
public double[]getValue(){
返回值;
}
公共无效设置值(双[]值){
这个值=值;
}
}
公共类B扩展了基本类{
私有列表值=新的ArrayList();
公共图书馆B(){
type=“B”;
}
公共B(列表值){
这个();
设置值(值);
}
@JsonIgnoreProperties({“type”})
公共列表getValue(){
返回值;
}
@JsonGetter(“价值”)
@JsonIgnoreProperties({“type”})
公共列表getA(){
List res=new ArrayList();
对于(A:值){
res.add(a.getValue());
}
返回res;
}
公共无效设置值(列表值){
这个值=值;
}
@凌驾
公共字符串toString(){
返回“”+值;
}
}
公共类C扩展了基类{
私有列表值=新的ArrayList();
公共C(){
type=“C”;
}
@JsonGetter(“价值”)
@JsonIgnoreProperties({“type”})
公共列表getB(){
List res1=new ArrayList();
对于(B:值){
List res2=new ArrayList();
对于(A:b.getValue()){
res2.add(a.getValue());
}
res1.添加(res2);
}
返回res1;
}
公共列表getValue(){
返回值;
}
公共无效设置值(列表值){
这个值=值;
}
}

Hi-shi,谢谢,看起来很有希望,但是B和C的“值”变成了字符串(用“{”type“:“B”,“value:“[[3.0,4.0],[5.0,6.0]]”}{type:“C”,“value:”[[7.0,8.0],[9.0,10.0]],[[11.0,12.0]]]。}嗨,帕万·库马尔,谢谢,它确实得到了结果。只是想问问有没有更有效的方法?没有在getValueForJson中创建新的ArrayList?嗨,Luiz,你确定C输出吗?这与期望的不一样,大括号问题。啊,是的,C的分组不一样。很抱歉。
    @JsonIgnoreProperties( { "type" })
    public List<A> getValue() {
        return value;
    }
...
    @JsonIgnoreProperties( { "type" })
    public List<B> getValue() {
        return value;
    }
=== com.example.A ===
{
  "type" : "A",
  "value" : [ 1.0, 2.0 ]
}
=== com.example.B ===
{
  "type" : "B",
  "value" : [ {
    "value" : [ 1.0, 2.0 ]
  }, {
    "value" : [ 3.0, 4.0 ]
  } ]
}
=== com.example.C ===
{
  "type" : "C",
  "value" : [ {
    "value" : [ {
      "value" : [ 1.0, 2.0 ]
    }, {
      "value" : [ 3.0, 4.0 ]
    } ]
  }, {
    "value" : [ {
      "value" : [ 5.0, 6.0 ]
    } ]
  } ]
}
class B extends Base {
    @JsonIgnore
    private List<A> value = new ArrayList<A>();

    public B() {
        type = "B";
    }

    public B(List<A> value) {
        this();
        setValue(value);
    }

    public List<A> getValue() {
        return value;
    }

    public void setValue(List<A> value) {
        this.value = value;
    }

    @JsonProperty("value")
    public List<double[]> getValueForJson(){
        List<double[]> res = new ArrayList<double[]>();
        value.forEach(a -> res.add(a.getValue()));
        return res;
    }
}

class C extends Base {
    @JsonIgnore
    private List<B> value = new ArrayList<B>();

    public C() {
        type = "C";
    }

    public List<B> getValue() {
        return value;
    }

    public void setValue(List<B> value) {
        this.value = value;
    }

    @JsonProperty("value")
    public List<double[]> getValueForJson(){
        List<double[]> res = new ArrayList<double[]>();
        value.forEach(b -> b.getValue().forEach(a -> res.add(a.getValue())));
        return res;
    }
}
public class A extends Base {
    private double[] value;

    public A() {
        type = "A";
    }

    public A(double[] value) {
        this();
        setValue(value);
    }

    public double[] getValue() {
        return value;
    }

    public void setValue(double[] value) {
        this.value = value;
    }
}

public class B extends Base {

    private List<A> value = new ArrayList<A>();

    public B() {
        type = "B";
    }

    public B(List<A> value) {
        this();
        setValue(value);
    }

    @JsonIgnoreProperties({"type"})
    public List<A> getValue() {
        return value;
    }

    @JsonGetter("value")
    @JsonIgnoreProperties({"type"})
    public List<double[]> getA() {
        List<double[]> res = new ArrayList<>();
        for (A a : value) {
            res.add(a.getValue());
        }
        return res;
    }

    public void setValue(List<A> value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "" + value;
    }
}

public class C extends Base {
    private List<B> value = new ArrayList<B>();

    public C() {
        type = "C";
    }

    @JsonGetter("value")
    @JsonIgnoreProperties({"type"})
    public List<List<double[]>> getB() {
        List<List<double[]>> res1 = new ArrayList<>();            
        for (B b : value) {
            List<double[]> res2 = new ArrayList<>();
            for (A a : b.getValue()) {
                res2.add(a.getValue());
            }
            res1.add(res2);
        }
        return res1;
    }

    public List<B> getValue() {
        return value;
    }

    public void setValue(List<B> value) {
        this.value = value;
    }
}