2017-01-11 1 views
-2

어디에도 json 읽기 유형을 만드는 방법을 찾을 수 없습니다. 이 가정 { "X": 5, "B": 6} 내가 너무JSON을 신속하게 변환하는 방법 3

func convertToDictionary(text: String) -> [String: Any]? { 
if let data = text.data(using: .utf8) { 
    do { 
     return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] 
    } catch { 
     print(error.localizedDescription) 
    } 
} 
return nil 
} 

를하고있는 중이 야

을 알 수없는

{ 
"x":5 
}, 
{ 
"b":6 
} 

키로 좀하고 싶습니다 온다 :

["x": 5, "b": 6] 
+2

[스위프트 3 올바르게 구문 분석 JSON]의 사용 가능한 복제 (HTTP : // stackoverfl ow.com/questions/39423367/correctly-parsing-json-in-swift-3) – Fogmeister

+0

JSON의 부모 개체 (발췌 본)에 크게 의존합니다. 단지 ** ** ** 사전이 있습니다. – vadian

답변

0
var tempJson : NSString = "" 
userCredentials = //pass your dictionary here 

    do 
    { 

     let jsonData = try JSONSerialization.data(withJSONObject: userCredentials, options: JSONSerialization.WritingOptions.prettyPrinted) 
     tempJson = NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue)! 
print(tempJson) 

    } 
    catch let error as NSError 
    { 
     print(error.description) 

    } 
관련 문제