3

I Have a Dictionary of Dictionaries that I need to convert to Json.

[
    Dict1:1, 
    test: A Value, 
    NestedDict1:[
        city: A City Name, 
        address: An Address, 
        NestedDict2: [
            1: 1, 
            39: 2
        ],
        favorite1: 2, 
        favorite3: Chocolate
    ]
]

When I use the

NSJSONSerialization.dataWithJSONObject(myJsonDict, options: NSJSONWritingOptions.PrettyPrinted, error: nil)

it only encodes the outer most dictionary. So my output looks something like this:

{
    "Dict1":"1", 
    "test": "A Value", 
    "NestedDict1":"[
        city: A City Name, 
        address: An Address, 
        NestedDict2: [
            1: 1, 
            39: 2
        ],
        favorite1: 2, 
        favorite3: Chocolate
    ]"
}

How do I JSON the inner dictionaries as well?

nhgrif
  • 61,578
  • 25
  • 134
  • 173
Big J
  • 41
  • 1
  • 3
  • Try not passing `.PrettyPrinted` for the `options:` argument? – nhgrif Jun 10 '15 at 00:16
  • http://stackoverflow.com/questions/8356842/how-to-use-nsjsonserialization – kbgn Jun 10 '15 at 00:38
  • Why did someone vote it down? Looks a very valid question for me!!! – Icaro Jun 10 '15 at 00:56
  • @ nhgrif tried without pretty print first, data ended up the same, just the outer gets encoded – Big J Jun 10 '15 at 01:30
  • @kbgn27 That link is actually for an array of dictionaries and doesnt seem the help with my problem of a dictionary of dictionaries – Big J Jun 10 '15 at 16:02
  • @BigJ Will the solution provided in this site help? http://blog.mailcloud.com/technology/apple-swift-code-strong-type-object-serialization-to-json/ – kbgn Jun 12 '15 at 01:59

2 Answers2

6

Swift 3

let myJsonDict : [String: Any] = [
    "Dict1": "1",
    "test": "A Value",
    "NestedDict1":[
        "city": "A City Name",
        "address": "An Address",
        "NestedDict2": [
            "1": "1",
            "39": "2"
        ],
        "favorite1": "2",
        "favorite3": "Chocolate"
    ]
]
let jsonObject = try? JSONSerialization.data(withJSONObject: myJsonDict, options: [])

if let jsonString = String(data: jsonObject!, encoding: .utf8) {
    print(jsonString)
}

Output

{"test":"A Value","Dict1":"1","NestedDict1":{"favorite1":2,"city":"A City Name","NestedDict2":{"1":"1","39":"2"},"favorite3":"Chocolate","address":"An Address"}}

Gagandeep Gambhir
  • 4,225
  • 1
  • 29
  • 34
3

I think the problem is more with your representation of the data.

If you can get away with changing all the keys to Strings, then it can be a dictionary, since Strings conform to Hashable. Otherwise, it would be defined as Any, and can't really be a Dictionary key.

Doing this allows the following:

let myJsonDict : [String:AnyObject] = [
    "value": 1123,
    "test": "some string",
    "NestedDict1": [
        "city": "A City Name",
        "address": "An Address",
        "NestedDict2": [
            "1": 1,
            "39": 2
        ],
        "favorite1": 2,
        "favorite3": "Chocolate"
    ]
]

var jsonObject = NSJSONSerialization.dataWithJSONObject(myJsonDict, options: NSJSONWritingOptions.PrettyPrinted, error: nil)


println(NSString(data: jsonObject!, encoding: NSUTF8StringEncoding)!)

which gives the following output:

{
  "test" : "some string",
  "NestedDict1" : {
    "city" : "A City Name",
    "address" : "An Address",
    "favorite3" : "Chocolate",
    "NestedDict2" : {
      "1" : 1,
      "39" : 2
    },
    "favorite1" : 2
  },
  "value" : 1123
}
Mark
  • 12,359
  • 5
  • 21
  • 37