2017-08-24 60 views
1

我想在swift 3中创建这种类型的字典在数组中,我做了它,而不是大括号它来方括号。我想在大括号中的字典。我想在swift 3中创建这样的数组类型3

[{ “interest_id”:1},{ “interest_id”:2},{ “interest_id”:3}]

+0

你需要的是JSON字典和数组的表示。那不一样。 – Larme

+0

是的,我想要相同的JSON表示。 –

+0

寻找'JSONSerialization'。 – Larme

回答

0

你可以这样做:

let dictArray = [ [ "interest_id":1 ] , ["interest_id":2], ["interest_id":3]] 

    do { 
     let jsonData = try JSONSerialization.data(withJSONObject: dictArray, options: .prettyPrinted) 
     let json = NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue)! 
     print("json data: ", json) 
    } 
    catch { 
     print("error: ", error.localizedDescription) 
    }