3

So I tried to parse a json file and when I parse it as per the syntax, it gives me an error that cannot change the string to an array of dictionary, but when I fix the problem, it generates nil. Can anyone give an opinion

func jsonFour() {
    let string = "[{\"address\": 7023000630,\"reportStatus\": \"Retrieved\",\"currentLocation\": {\"latitude\": 29.8529, \"longitude\": 73.99332,\"timestamp\": \"2019-01-07T16:35:25.079+05:30\"} }, {\"address\": 7290098339, \"reportStatus\": \"Retrieved\", \"currentLocation\": {\"latitude\": 21.628569, \"longitude\": 72.996956,\"timestamp\": \"2019-01-07T16:35:25.079+05:30\" } } ]"

    let data = string.data(using: .utf8)!
    do {
        if let jsonArray = try JSONSerialization.jsonObject(with: data, options : JSONSerialization.ReadingOptions.mutableContainers) as? [[ String : Any ]]
        {
            print(jsonArray) // use the json here
            let address = jsonArray["address"] as! [[String:Any]]
            if let timestamp = address["timestamp"] as? [String]{print(timestamp)}
        } else {
            print("bad json")
        }
    } catch let error as NSError {
        print(error)
    }

}

When I remove the double brackets from "String : Any", it runs fine, but does not give any value but nil.

And when I proceed with this way, it skips the if statement and just prints "bad json".

What am I doing wrong here?

Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
vgvishesh23113
  • 309
  • 3
  • 12

4 Answers4

2

Since there is Codable, I would strongly recommend you to using it instead of JSONSerialization.

So start with declaring your structs to match with your JSON struct

struct Model: Codable {
    var address: Int
    var reportStatus: String
    var currentLocation: Location
}

struct Location: Codable {
    var latitude, longitude: Double
    var timestamp: String
}

Now just decode your JSON using JSONDecoder

do {
    let data = string.data(using: .utf8)!
    let models = try JSONDecoder().decode([Model].self, from: data)
} catch {
    print(error)
}

... now models is array of Model objects and you can work with it.

Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
1

For sure you should use Codable for this , but for your code to run use

let string = "[{\"address\": 7023000630,\"reportStatus\": \"Retrieved\",\"currentLocation\": {\"latitude\": 29.8529, \"longitude\": 73.99332,\"timestamp\": \"2019-01-07T16:35:25.079+05:30\"} }, {\"address\": 7290098339, \"reportStatus\": \"Retrieved\", \"currentLocation\": {\"latitude\": 21.628569, \"longitude\": 72.996956,\"timestamp\": \"2019-01-07T16:35:25.079+05:30\" } } ]"

let data = string.data(using: .utf8)!

do {
    if let jsonArray = try JSONSerialization.jsonObject(with: data) as? [[ String : Any ]]
    {
        jsonArray.forEach {

            if  let add = $0["address"] as? Int  , let currentLocation = $0["currentLocation"] as? [String:Any], let timestamp = currentLocation["timestamp"] as? String
            {
                print("address is : ", add ,"timestamp is : " , timestamp)
            }
        }

    } else {
        print("bad json")
    }
} catch  {
    print(error)
}

Your obvious problem is here

let address = jsonArray["address"] as! [[String:Any]]

jsonArray is an array that you can't subscript with ["address"]

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
1

An array is a list. It can contain multiple items. You have to use a loop to iterate an array (like in your previous question)

  • The value for key address is Int (no double quotes).
  • The value for key timestamp is String and is in the dictionary for key currentLocation in the array

    let data = Data(string.utf8)
    do {
        if let jsonArray = try JSONSerialization.jsonObject(with: data) as? [[String : Any]]
        {
            print(jsonArray) // use the json here
            for item in array {
               let address = item["address"] as! Int
               let currentLocation = item["currentLocation"] as! [String:Any]
               let timestamp = currentLocation["timestamp"] as! String
               print(timestamp)
            }
        } else {
            print("bad json")
        }
    } catch {
        print(error)
    }
    

Never use .mutableContainers in Swift. It's pointless.

vadian
  • 274,689
  • 30
  • 353
  • 361
1

In your code snippet jsonArray is an array and array can't subscript a value of type [[String: Any]], so instead you should parse like,

func jsonFour(){
    let string = "[{\"address\": 7023000630,\"reportStatus\": \"Retrieved\",\"currentLocation\": {\"latitude\": 29.8529, \"longitude\": 73.99332,\"timestamp\": \"2019-01-07T16:35:25.079+05:30\"} }, {\"address\": 7290098339, \"reportStatus\": \"Retrieved\", \"currentLocation\": {\"latitude\": 21.628569, \"longitude\": 72.996956,\"timestamp\": \"2019-01-07T16:35:25.079+05:30\" }}]"

    let data = string.data(using: .utf8)!
    do {
        if let jsonArray = try JSONSerialization.jsonObject(with: data, options : JSONSerialization.ReadingOptions.mutableContainers) as? [[String: Any]]
        {
            print(jsonArray) // print the json here
            for jsonObj in jsonArray {
                if let dict = jsonObj as? [String: Any] {
                    if let address = dict["address"] {
                        print(address)
                    }
                    if let location = dict["currentLocation"] as? [String: Any], let timeStamp = location["timestamp"]  {
                        print(timeStamp)
                    }
                }
            }
        } else {
            print("bad json")
        }
    } catch let error as NSError {
        print(error)
    }
}
Bappaditya
  • 9,494
  • 3
  • 20
  • 29