-1

Nowhere I can not find how to make a json readable type. Suppose there { "x": 5, "b": 6} I would like to get as

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

Keys are not known

I am doing so

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
}

Comes:

["x": 5, "b": 6]
user233428
  • 177
  • 9
  • 2
    Possible duplicate of [Correctly Parsing JSON in Swift 3](http://stackoverflow.com/questions/39423367/correctly-parsing-json-in-swift-3) – Fogmeister Jan 11 '17 at 11:19
  • It strongly depends on the parent object of the JSON (excerpt). There are just **two** dictionaries. – vadian Jan 11 '17 at 13:58

1 Answers1

2
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)

    }
Amit Verma
  • 1,393
  • 1
  • 8
  • 7