Parsing JSON in Swift 4

Swift 4 မှာ JSON အတွက် JSON အတွက် Encoder , Decoder ပါလာပါပြီ။ ကျွန်တော်တို့ Swift 3 မှာ JSON ကနေ Object ကို ပြန်ပြောင်းဖို့ အတွက် code မှာ ပြန်ရေးရပါတယ်။ Code က သာ Array ဖြစ်ခဲ့ရင် ရလာသည့် Array ကို Loop ပြန်ပတ်ပြီးတော့ Object ပြန်ဆောက်ရပါတယ်။ အခု Swift 4 မှာတော့ အောက်ကလို အသုံးပြုနိုင်ပါတယ်။

Decoder

Decoder ကတော့ JSON String ကနေ Codable object ကို ပြောင်းဖို့ အတွက် အသုံးပြုပါတယ်။

JSON Array to Object Array

import UIKit

struct Todo: Codable {
    var id: Int
    var text: String
    var done: Int
}

let res = "[{\"id\":3,\"text\":\"Hello World\",\"done\":0},{\"id\":2,\"text\":\"cool\",\"done\":0},{\"id\":1,\"text\":\"hello\",\"done\":1}]"


let resData = res.data(using: .utf8)!

let decoder = JSONDecoder()
do {
    let todoList = try decoder.decode([Todo].self, from: resData)
}
catch {
    print("error trying to convert data to JSON")
    print(error)
}

အခု code မှာ let decoder = JSONDecoder() ကို အသုံးပြုပြီးတော့ decoder.decode([Todo].self, from: resData) ဆိုရင် ရပါတယ်။ Array ဖြစ်နေသည့် အတွက်ကြောင့် [Todo] ကို အသုံးပြုထားတာပါ။

JSON Object to Object

ဒါမှမဟုတ် object တစ်ခုပဲ ဆိုရင်တော့ အောက်ကလို အသုံးပြုနိုင်ပါတယ်။

let res2 = "{\"id\":3,\"text\":\"Hello World\",\"done\":0}"

let resData2 = res2.data(using: .utf8)!

let decoder = JSONDecoder()
do {
    let todo = try decoder.decode(Todo.self, from: resData2)
}
catch {
    print("error trying to convert data to JSON")
    print(error)
}

အခု Code မှာ ဆိုရင်တော့ Object က တစ်ခု တည်းဖြစ်သည့် အတွက်ကြောင့် Todo.self သုံးထားတာကို တွေ့နိုင်ပါတယ်။

Customizing Key Names

အကယ်၍ JSON string နဲ့ ကျွန်တော်တို့ ဆောက်ထားသည့် object အနည်းငယ် ကွာဟာမှု ရှိခဲ့လျှင်လည်း အောက်ကလို ပြန်ပြင်နိုင်ပါတယ်။

struct Todo: Codable {
    var id: Int
    var text: String
    var done: Int
    
    enum CodingKeys : String, CodingKey {
        case id
        case text = "value"
        case done
    }
}

let res = "[{\"id\":3,\"value\":\"Hello World\",\"done\":0},{\"id\":2,\"value\":\"cool\",\"done\":0},{\"id\":1,\"value\":\"hello\",\"done\":1}]"


let resData = res.data(using: .utf8)!

let decoder = JSONDecoder()
do {
    let todoList = try decoder.decode([Todo].self, from: resData)
}
catch {
    print("error trying to convert data to JSON")
    print(error)
}

ကျွန်တော်တို့တွေ အနေနဲ့ ဘယ် key နဲ့ ချိတ်ထားလဲ ဆိုတာကို enum CodingKeys : String, CodingKey မှာ ဖြည့်ပေးဖို့ လိုပါတယ်။

enum CodingKeys : String, CodingKey {
        case id
        case text = "value"
        case done
}

အခု code မှာဆိုရင် text က value ဆိုသည့် key ကို အသုံးပြုမယ်လို့ ဆိုထားပါတယ်။

Encoder

Encoder ကတော့ Codable object ကနေ json string ကို ပြောင်းဖို့ အတွက် အသုံးပြုပါတယ်။

let t = Todo(id: 10, text: "Sample", done: 0)

let encoder = JSONEncoder()

do {
    let json = try encoder.encode(t)
    if let jsonString = String(data: json, encoding: .utf8) {
        print(jsonString)
    }
}
catch {
    print("error trying to convert data to JSON String")
    print(error)
}


အခု Code က sample code ဖြစ်လို့ ရိုးရိုးရှင်းရှင်းလေးတွေ ပါပဲ။ သို့ပေမယ် တကယ့် project တွေမှာ သုံးသည့် JSON တွေ က အလွန်ကို ရှုပ်ထွေးပြီးတော့ အဆင့်တွေများစွာ ပါတတ်ပါတယ်။

ဒါကြောင့် Codable အကြောင်း အသေးစိတ်ကို http://swiftjson.guide မှာ သွားရောက် ဖတ်နိုင်ပါတယ်။

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.