iT邦幫忙

0

Android 開發,請問如何把 SQLite 的資料寫成 Json 格式

最近小弟我遇到一個問題需要把手機SQLite 的資料 寫成Json 傳給API

假設查詢的資料是下方兩筆!請問該如何去撰寫才能變成 JSON格式。

我目前是使用 陣列存好!再想辦法把它變成JSON格式,不知道這樣流程對不對!正在亂試中! 但是接下來就不知道怎麼把它變成 JSON格式。

Table member
id name
1 Victor
2 Leo

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
小碼農米爾
iT邦高手 1 級 ‧ 2017-11-27 17:03:20
[
    {
        id: "1",
        name: "Victor"
    },
    {
        id: "2",
        name: "Leo"
    }
]
Victor iT邦新手 2 級 ‧ 2017-11-27 17:05:54 檢舉

hi! 剛剛 有查了一下 是使用 JSONObject 的方式把查詢出來的資料 寫成 Json 嗎?

對阿!
http://blog.csdn.net/aa4790139/article/details/8587208
您如果是陣列轉 JSON,可以這樣使用

//People 的欄位對應您的 Table member
People[] peopleArray = new People[];
//...
//將 SQL 查出來的資料填入 People 陣列
//...
JSONArray jsonArray = JSONArray.fromObject(peopleArray);
String json = jsonArray.toString();
Victor iT邦新手 2 級 ‧ 2017-12-01 14:41:00 檢舉

謝謝大大!

0
石頭
iT邦高手 1 級 ‧ 2017-11-27 17:26:14

Json 是一種資料傳輸格式

可以參考網路上種多使用Android操作Json的範例
Android使用Json

Victor iT邦新手 2 級 ‧ 2017-12-01 14:40:46 檢舉

謝謝大大!

0
攻城屍
iT邦新手 4 級 ‧ 2017-11-28 11:11:40

你可以考慮用Gson

如何使用Gson

Victor iT邦新手 2 級 ‧ 2017-12-01 14:40:33 檢舉

謝謝大大! 我終於嘗試出來了!

我要發表回答

立即登入回答