今天我們來實踐 JSON 組裝對應資料
比如 :
-----------------------------------------------------------------------------------------------------------------
| 
Name | 
NickName | 
Brithday | 
| 
Tom | 
Apple | 
1988/01/01 | 
| 
John | 
Cat | 
1989/02/02 | 
-----------------------------------------------------------------------------------------------------------------
我們先抓對應的Key : Name、NickName、BrithDay
// Tom 的資料
JSONObject json_1 = new JSONObject();
json_1.put("Name", "Tom");
json_1.put("NickName", "Apple");
json_1.put("BrithDay", "1988/01/01");
// John 的資料
JSONObject json_2 = new JSONObject();
json_2.put("Name", "John");
json_2.put("BrithDay", "Cat");
json_2.put("action", "1989/02/02");
// 將兩筆資料組合
JSONArray jsonArray = new JSONArray();
jsonArray.put(json_1);
jsonArray.put(json_2);
// 將整合好了資料放在 JSONObject 並設置相對應的 Key
JSONObject json = new JSONObject();
json.put("Vlaue", jsonArray);
System.out.println("json : " + json.toString());
輸出 :
{
    "Vlaue":[
        {
            "Name":"Tom",
            "BrithDay":"1988/01/01",
            "NickName":"Apple"
        },
        {
            "Name":"Cat",
            "BrithDay":"1989/02/02",
            "NickName":"John"
        }
    ]
}
如果我們要存成字串在轉回來要怎辦 ?
沒有留言:
張貼留言