2017-01-04 1 views
-1

나는 사전 [[String : Any]]의 배열을 가지고 있는데, 이것을 JSON 문자열로 변환하고 싶다. 그러나 나는 그것으로 시작하는 방법을 모른다. JSONSerialization.data (withJSONObject : array, options : .prettyPrinted)를 시도하고 메소드에 배열을 전달했지만 오류가 발생했습니다. 어떤 해결책이라도 아래에 의견을 남기십시오. 감사.스위프트 3 배열을 JSON으로 변환

+0

당신은 당신의 코드를 포함 할 수? –

+0

빠른 질문 인 경우 관련 태그를 추가하십시오. 또한 시도가 표시하는 특정 오류를 제공해야합니다. –

+0

[배열을 신속하게 JSON 문자열로 변환] 가능한 복제본 (http://stackoverflow.com/questions/28325268/convert-array-to-json-string-in-swift) –

답변

0

다음 코드로 시도 ...

do { 

    //Convert to Data 
    let jsonData = try JSONSerialization.data(withJSONObject: dictionaryArray, options: JSONSerialization.WritingOptions.prettyPrinted) 

    //Do this for print data only otherwise skip 
    if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) { 
     print(JSONString) 
    } 

    //In production, you usually want to try and cast as the root data structure. Here we are casting as a dictionary. If the root object is an array cast as [AnyObject]. 
    var json = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String: AnyObject] 


    } catch { 
     print(error.description) 
    } 
관련 문제