Swift의 Json 객체 및 Json 문자열

1990 단어 Swift 기반
하나.입력
1. Json 문자열
let jsonStr = "{\"name\":\"wall\",\"age\":\"22\",\"hobby\":\"ball\"}"

2. Json 객체
    let jsonStr = "{\"name\":\"wall\",\"age\":\"22\",\"hobby\":\"ball\"}"
    let jsonData=jsonStr.data(using: String.Encoding.utf8, allowLossyConversion: false)
    let json=try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)

둘.출력
1. 제이슨 대상
{
  "name" : "wall",
  "age" : "22",
  "hobby" : "ball"
}

2. Json 문자열
{"name":"wall","age":"22","hobby":"ball"}

셋.상호 변환
1. Json 문자열에서 Json 객체 변환
    let jsonStr = "{\"name\":\"wall\",\"age\":\"22\",\"hobby\":\"ball\"}"
    let jsonData=jsonStr.data(using: String.Encoding.utf8, allowLossyConversion: false)
    let json=try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)

2. Json 객체 전환 Json 문자열
    let data = try? JSONSerialization.data(withJSONObject: json, options: [])
    let jsonStr = String(data: data, encoding: String.Encoding.utf8)
3.Json 문자열 변환 사전
    let jsonStr = "{\"name\":\"wall\",\"age\":\"22\",\"hobby\":\"ball\"}"
    let jsonData=jsonStr.data(using: String.Encoding.utf8, allowLossyConversion: false)
    let json=try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
    if json !=nil {
        let  dict=json as! NSDictionary
    }

4. 사전 전환 Json 문자열
    let data = try? JSONSerialization.data(withJSONObject: dict, options: [])
    let jsonStr = String(data: data, encoding: String.Encoding.utf8)

좋은 웹페이지 즐겨찾기