Parsing Json object within a Json Object Swift 5

swift parse json array of dictionaries
complex json parsing in swift 4
swift json parsing example
swift decode json array
alamofire json parsing swift 4
swift create json object
json to dictionary swift
alamofire json parsing swift 5

I'm trying to parse some json in Xcode that is basically a bunch of objects in an object. The json looks like this below.

{"TDOC": {
"assetType": "EQUITY",
"assetMainType": "EQUITY",
"cusip": "87918A105",
"symbol": "TDOC"}}

I am parsing it using the code below in Xcode using swift5

do {            
   if let json = try JSONSerialization.jsonObject(with: jData, options: []) as? [String: Any] {
      if let pr = json["TDOC"] as? Array<Dictionary<String, Any>> {
         for p in pr {
             print(p["assetType"] ?? "")
         }
      }
   }
} catch let err {
   print(err.localizedDescription)
}

I'm trying to get the assetType value but when I run the code, nothing prints out to the console. When I debug, it seems like Xcode just skips right over my for in loop for that prints the assetType for some reason. Any help on this is appreciated.

You can't treat TDOC object as an Array. As it is a dictionary object, you can take it as Dictionary directly.

You can do it like this.

do {

    if let json = try JSONSerialization.jsonObject(with: jData, options: []) as? [String: Any] {

        if let pr = json["TDOC"] as? Dictionary<String, Any> {
            print(pr["assetType"])
        }
    }

} catch let err {

    print(err.localizedDescription)
}

Working with JSON in Swift - Swift Blog, The JSONSerialization class method jsonObject(with:options:) returns a value of type Any and throws an error if the data couldn't be parsed. To parse the JSON, we first need to define suitable data models for storing the parsed data later on. Our data model we start with is the one we use for the entries’ content, the image data. We use a struct containing an imageUR L and uploaderName property for this.

Try this.

"TDOC" key corresponds to a dictionary value, not an array. No for loop needed as well.

do {

    if let json = try JSONSerialization.jsonObject(with: jData, options: []) as? [String: Any] {

        if let pr = json["TDOC"] as? [String: Any] {

                print(pr["assetType"])
        }
    }

} catch let err {

    print(err.localizedDescription)
}

Hope this helps.

How to Parse JSON in Swift 5. Parsing JSON is one the most , To be able to call API for JSON data you need to register on the News API site and get your key. JSON objects are represented in key:value pairs� As an iOS developer, parsing JSON is undoubtably something that you will have to do. With the growing number of APIs available, the ability to parse JSON and use that data in your app has never been more useful. I will show you how to parse JSON data and use it within your app in a few easy steps. Setting up the structure (with standard types)

1. Make a reflecting class of your data and make it conform to Codable protocol

    import Foundation

    // MARK: - Welcome
    struct MyObject: Codable {
        let tdoc: Tdoc

        enum CodingKeys: String, CodingKey {
            case tdoc = "TDOC"
        }
    }

    // MARK: - Tdoc
    struct Tdoc: Codable {
        let assetType, assetMainType, cusip, symbol: String
    }

1. Parse it using JSONDecoder:

    do {

           let myObject = try JSONDecoder().decode(MyObject.self, from: jsonData)
           print(myObject.tdoc.assetType)

    } catch {
           print(error)
    }

heres an apple doc for a broader info about Encoding, Decoding and CodingKeys protocols

https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types

Hope this helps :)

How to parse JSON using JSONSerialization, In the example below, I create a dummy piece of JSON that contains three gets sent to JSONSerialization (by converting it into a Data object,� Since the release of iOS 5, the iOS SDK has already made it easy for developers to fetch and parse JSON data. It comes with a handy class called NSJSONSerialization, which can automatically convert JSON formatted data to objects. Later in this chapter, I will show you how to use the API to parse some sample JSON formatted data, returned by a

JSON Parsing in Swift explained with code examples, JSON Parsing and decoding in Swift is really easy with the default API. "title": " Optionals in Swift explained: 5 things you should know", "url": a JSON array in Swift is almost just as easy as decoding a single JSON object. Swift Jan 21, 2020 Jan 22, 2020 • 5 min read JSON Parsing in Swift explained with code examples. JSON parsing in Swift is a common thing to do. Almost every app decodes JSON to show data in a visualized way. Parsing JSON is definitely one of the basics you should learn as an iOS developer.

Working With JSON In Swift With SwiftyJSON – LearnAppMaking, In this article you learn how to work with JSON in Swift with the SwiftyJSON library. JSON stands for JavaScript Object Notation, and it's a standardized file format A JSON parser doesn't care about those niceties, so you might as well and learn how to build great iOS 13 apps with Swift 5 and Xcode 11. Sep 12, 2016. Working with JSON in Swift If your app communicates with a web application, information returned from the server is often formatted as JSON.You can use the Foundation framework’s JSONSerialization class to convert JSON into Swift data types like Dictionary, Array, String, Number, and Bool.

Simple JSON Parsing with Swift |, For iOS, there has long been ample JSON support through third-party libraries and, since iOS 5, It allows the transmission of simple object graphs in an easy to read form (both for developers and code). “line 1” : “1234 5th Avenue”, You can also create a custom implementations of these if you need something different like XML. The rest of this blog post will focus on JSON parsing because that is the most relevant to most iOS developers. The Basics. If your JSON structure and objects have similar structure, then your work is really easy. Here’s an example JSON document

Comments
  • Glad to help! Please upvote & mark as accepted, so it would be helpful to others facing a similar issue