将数据库详细信息转换为JSON对象 [英] Converting the Database details to JSON object

查看:373
本文介绍了将数据库详细信息转换为JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中有列

recordID,recordName,titleFeild,titleIDMap,titleId,titleStartDate,titleEndDate,languageId

recordID, recordName , titleFeild, titleIDMap, titleId, titleStartDate, titleEndDate, languageId

现在,我已将上面各列中的数据转换为如下所示的JSON对象数据

Now I have convert the data from above columns to the JSON object data which looks like below

{
  "recordId" :10,
  "recordName" : "RECORDS",
  "records" : [ {
    "titleField" : 1,
    "titleIDMap" : null,
    "titleId" : 500,
    "titleStartDate" : "2013-12-22T00:00:00.000+0000",
    "titleEndDate" : "2013-12-03T00:00:00.000+0000",
    "languageId" : 20
  }]
}

请注意,记录是一个列数组(titleFeild,titleIDMap,titleId,titleStartDate,titleEndDate,languageId)

Please note that records is an array of columns ( titleFeild,titleIDMap,titleId,titleStartDate,titleEndDate,languageId)

到目前为止,我开发的代码是

The code so far I have developed is

    List<Object[]> objList = dao.getStatus();
    Integer result = null;
     JSONObject jsonData = new JSONObject();
     JSONArray jsonDataArray = new JSONArray();
    if(objList!=null && objList.size()>10000)
        {
            for (Object[] nameObj : objList) {

                jsonData.put("", nameObj.get(arg0) );


            }
        }

如何从列数据构造JSON对象?

推荐答案

您可以通过 google-gson 库.简单来说,您将必须创建几个 Pojos (参考引用列表中的另一个包含内容).

You can easily achieve this with google-gson library. In simple terms you would have to create a couple of Pojos (with reference to another containin a list of references).

RecordIDRecordName视为元数据.

创建一个表示此信息的pojo:

Create a pojo representing this information:

public class DbMetaPojo {

    private int recordID;
    private String recordName;
    private List<Record> records;

    public List<Record> getRecords() {
        return records;
    }

    public void setRecords(List<Record> records) {
        this.records = records;
    }

    public String getRecordName() {
        return recordName;
    }

    public void setRecordName(String recordName) {
        this.recordName = recordName;
    }

    public int getRecordID() {
        return recordID;
    }

    public void setRecordID(int recordID) {
        this.recordID = recordID;
    }

}

使用实际的Record字段创建另一个pojo:

Create another pojo with the actual Record fields:

public class Record {

    public int getTitleFeild() {
        return titleFeild;
    }

    public void setTitleFeild(int i) {
        this.titleFeild = i;
    }

    public String getTitleIDMap() {
        return titleIDMap;
    }

    public void setTitleIDMap(String titleIDMap) {
        this.titleIDMap = titleIDMap;
    }

    public int getTitleId() {
        return titleId;
    }

    public void setTitleId(int titleId) {
        this.titleId = titleId;
    }

    public String getTitleStartDate() {
        return titleStartDate;
    }

    public void setTitleStartDate(String titleStartDate) {
        this.titleStartDate = titleStartDate;
    }

    public String getTitleEndDate() {
        return titleEndDate;
    }

    public void setTitleEndDate(String titleEndDate) {
        this.titleEndDate = titleEndDate;
    }

    public int getLanguageId() {
        return languageId;
    }

    public void setLanguageId(int languageId) {
        this.languageId = languageId;
    }

    private int titleFeild;
    private String titleIDMap;
    private int titleId;
    private String titleStartDate;
    private String titleEndDate;
    private int languageId;

}

现在只是一种用相关数据 (将硬编码逻辑替换为数据检索)填充POJOs的方法:

Now just a method to populate your POJOs with the relevant data (replace the hardcoding logic with your data retrieve):

public static void main(String... main) {
        DbMetaPojo obj = new DbMetaPojo();

        obj.setRecordID(10);
        obj.setRecordName("RECORDS");

        Record record = new Record();

        record.setLanguageId(20);
        record.setTitleEndDate("2013-12-22T00:00:00.000+0000");
        record.setTitleFeild(1);
        record.setTitleId(500);
        record.setTitleIDMap("SOME NULL");
        record.setTitleStartDate("2013-12-22T00:00:00.000+0000");

        List<Record> list = new ArrayList<Record>();
        list.add(record);
        obj.setRecords(list);

        Gson gson = new Gson();

        String json = gson.toJson(obj);

        System.out.println(json);
    }

输出是您形成的JSON:

{
    "recordID": 10,
    "recordName": "RECORDS",
    "records": [
        {
            "titleFeild": 1,
            "titleIDMap": "SOME NULL",
            "titleId": 500,
            "titleStartDate": "2013-12-22T00:00:00.000+0000",
            "titleEndDate": "2013-12-22T00:00:00.000+0000",
            "languageId": 20
        }
    ]
}

要与您的代码保持一致,您可能需要执行以下操作:

To align to your code, you might want to do something like:

List<Object> objList = dao.getStatus();
        List<DbMetaPojo> metaList = new ArrayList<DbMetaPojo> ();
        if (objList != null && objList.size() > 10000) {
            for (Object nameObj : objList) {
                DbMetaPojo meta = new DbMetaPojo();
                meta.setRecordID(nameObj[0]);
                meta.setRecordName(nameObj[0]);
                ...
                ...
                ...

                metaList.add(meta);
            }
        }

这篇关于将数据库详细信息转换为JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆