Representing JSON Data in the Form of Swift Dictionaries

The project which I’m currently working on involves working with a lot of JSON data in an iOS mobile app which I plan on sending to a database using the Alamofire http networking library/wrapper. When working with JSON in Swift using Alamofire, you need to create your JSON objects in the form of Swift Dictionaries. This has been pretty confusing for me, so I wanted to whip up this quick blog post for my own personal reference demonstrating how to represent JSON data in the form of a Swift Dictionary so that it can later be sent over the internet to a backend server using Alamofire:
exampledata.json

{
    "location": [
        {
            "latitude": 30.00000001,
            "longitude": 30.0000005,
            "timestamp": 2342342342
        },
        {
            "latitude": 30.00000029,
            "longitude": 30.00000023,
            "timestamp": 2342342343
        }
    ],
    "altitude": [
        {
            "pressure": 90.00000001,
            "timestamp": 2342342345
        },
        {
            "pressure": 91.00000029,
            "timestamp": 2342342347
        }
    ]
}

 
exampledictionary.swift

import Foundation
// REFERENCE: https://dzone.com/articles/all-you-need-to-know-about-json-with-swift
var myDictionary = [
    "location": [
        [
            "latitude": 30.00000001,
            "longitude": 30.0000005,
            "timestamp": 2342342342
        ],
        [
            "latitude": 30.00000029,
            "longitude": 30.00000023,
            "timestamp": 2342342343
        ]
    ],
    "altitude": [
        [
            "pressure": 90.00000001,
            "timestamp": 2342342345
        ],
        [
            "pressure": 91.00000029,
            "timestamp": 2342342347
        ]
    ]
] as [String:Any]
print(myDictionary)

 
REFERENCE: All You Need to Know About JSON With Swift

 

topherPedersen