Search code examples
iosobjective-cjsonnsdictionarynsjsonserialization

JSON to NSDictionary literal


I need to build an NSDictionary that I can then parse with NSJSONSerialization in order to get a nice JSON formatted string.

The final output of my JSON needs to be something like:

"Viewport":{
        "BottomRight":{
            "Latitude":1.26743233E+15,
            "Longitude":1.26743233E+15
        },
        "TopLeft":{
            "Latitude":1.26743233E+15,
            "Longitude":1.26743233E+15
        }
    }

I figured it might be easiest to use NSDictionary literals, but how would I create multi-level nested dictionaries doing it this way?

Also, I will need to be setting the lat and long values when creating the dictionary.


Solution

  • You can arbitrarily nest liberals:

    @{@"Viewport":@{
        @"BottomRight":@{
            @"Latitude":@(1267...),
            @"Longitude":@(126...)
        },
        ...