0

我在 Java 中使用 Jackson 解析器,我想从POJO的 Java 对象生成 JSON 字符串,格式如下。我怎样才能做到这一点?

{"TOTAL":1,"CURRENTPAGE":1,"TOTALRECORDS":6,"ROWS":[{"pagename":"调用的 getter 方法:测试","pagestatus":"测试状态","id": 4},{"pagename":"调用getter方法:调用getter方法:测试","pagestatus":"测试状态","id":4},{"pagename":"调用getter方法:调用getter方法: Test","pagestatus":"Test Status","id":4},{"pagename":"Called getter Method : Called getter Method : Test","pagestatus":"Test Status","id":4 }]}

如果我确实喜欢下面的事情,那么它不起作用并给我错误的输出

    Tblselectablecolumnjqgrid selCol = new Tblselectablecolumnjqgrid();
    selCol.setPageid(4);
    selCol.setPagename("Test");
    selCol.setPagestatus("Test Status");

    selList.add(selCol);
    selList.add(selCol.clone());
    selList.add(selCol.clone());
    selList.add(selCol.clone());

    ObjectMapper om = new ObjectMapper();

    String writeValueAsString = om.writeValueAsString(selList);

    System.out.println("Result:    " + writeValueAsString);

    JsonFactory jsf = new JsonFactory();

    JsonTreeWriter tree = new JsonTreeWriter();

它给了我以下输出

[{"pagename":"调用的 getter 方法:测试","pagestatus":"测试状态","id":4},{"pagename":"调用的 getter 方法:调用的 getter 方法:测试","pagestatus" :"Test Status","id":4},{"pagename":"Called getter Method : Called getter Method : Test","pagestatus":"Test Status","id":4},{"pagename" :"调用getter方法:调用getter方法:测试","pagestatus":"测试状态","id":4}]

我应该在代码中进行哪些更改以获得所需的输出?

我的 POJO 课

package com.pojo;

import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonCachable;

/**
 * @author bhavik.ambani
 *
 *         Useful for storing the current status of the grid in the table.
 */
public class Tblselectablecolumnjqgrid implements java.io.Serializable, Cloneable {

    private static final long serialVersionUID = 1L;

    @JsonProperty("id")
    private int pageid;

    private String pagename;

    private String pagestatus;

    public Tblselectablecolumnjqgrid() {
    }

    public Tblselectablecolumnjqgrid(int pageid, String pagename) {
        this.pageid = pageid;
        this.pagename = pagename;
    }

    public Tblselectablecolumnjqgrid(int pageid, String pagename,
            String pagestatus) {
        this.pageid = pageid;
        this.pagename = pagename;
        this.pagestatus = pagestatus;
    }

    public int getPageid() {
        return this.pageid;
    }

    public void setPageid(int pageid) {
        this.pageid = pageid;
    }

    public String getPagename() {
        System.out.println("Called Getter Method");
        return "Called getter Method : " + this.pagename;
    }

    public void setPagename(String pagename) {
        this.pagename = pagename;
    }

    public String getPagestatus() {
        return this.pagestatus;
    }

    public void setPagestatus(String pagestatus) {
        this.pagestatus = pagestatus;
    }

    @Override
    public Tblselectablecolumnjqgrid clone() throws CloneNotSupportedException {
        Tblselectablecolumnjqgrid selBean = (Tblselectablecolumnjqgrid) super
                .clone();
        selBean.setPageid(getPageid());
        selBean.setPagename(getPagename());
        selBean.setPagestatus(getPagestatus());

        return selBean;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + pageid;
        result = prime * result
                + ((pagename == null) ? 0 : pagename.hashCode());
        result = prime * result
                + ((pagestatus == null) ? 0 : pagestatus.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Tblselectablecolumnjqgrid other = (Tblselectablecolumnjqgrid) obj;
        if (pageid != other.pageid)
            return false;
        if (pagename == null) {
            if (other.pagename != null)
                return false;
        } else if (!pagename.equals(other.pagename))
            return false;
        if (pagestatus == null) {
            if (other.pagestatus != null)
                return false;
        } else if (!pagestatus.equals(other.pagestatus))
            return false;
        return true;
    }
}

主要方法类

package com.jackson;

import java.io.IOException;
import java.util.ArrayList;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.pojo.Tblselectablecolumnjqgrid;

public class JacksonParser {

    public static void main(String args[]) throws JsonGenerationException,
            JsonMappingException, IOException, CloneNotSupportedException {

        ArrayList<Tblselectablecolumnjqgrid> selList = new ArrayList<Tblselectablecolumnjqgrid>();

        Tblselectablecolumnjqgrid selCol = new Tblselectablecolumnjqgrid();
        selCol.setPageid(4);
        selCol.setPagename("Test");
        selCol.setPagestatus("Test Status");

        selList.add(selCol);
        selList.add(selCol.clone());
        selList.add(selCol.clone());
        selList.add(selCol.clone());

        ObjectMapper om = new ObjectMapper();

        String writeValueAsString = om.writeValueAsString(selList);
        System.out.println("Result:    " + writeValueAsString);
    }
}

预期产出

{"TOTAL":1,"CURRENTPAGE":1,"TOTALRECORDS":6,"ROWS":[{"pagename":"调用的 getter 方法:测试","pagestatus":"测试状态","id": 4},{"pagename":"调用getter方法:调用getter方法:测试","pagestatus":"测试状态","id":4},{"pagename":"调用getter方法:调用getter方法: Test","pagestatus":"Test Status","id":4},{"pagename":"Called getter Method : Called getter Method : Test","pagestatus":"Test Status","id":4 }]}

输出获取

[{"pagename":"调用的 getter 方法:测试","pagestatus":"测试状态","id":4},{"pagename":"调用的 getter 方法:调用的 getter 方法:测试","pagestatus" :"Test Status","id":4},{"pagename":"Called getter Method : Called getter Method : Test","pagestatus":"Test Status","id":4},{"pagename" :"调用getter方法:调用getter方法:测试","pagestatus":"测试状态","id":4}]

4

1 回答 1

2

写一个像这样的包装器

public class Wrapper {
  private int TOTAL;
  private int CURRENTPAGE;
  private int TOTALRECORDS;
  private List<Row> ROWS;

  //getters setters

}

public class Row {
  private String pagename;
  private String pagestatus;
  private int id;

  //getters setters
}

在你的主要课程中

//populate Wrapper
Wrapper wrap = new Wrapper();
//set attributes and Rows
...
String writeValueAsString = om.writeValueAsString(wrap);
于 2012-09-05T05:33:07.600 回答