0

I have a JSON file which contains list of array objects. I'm unable to parse that json file

I have tried this code but it didn't work

   if let tempResult = json[0]["sno"].string{
        print("Temp Result is \(tempResult)")
    }
    else {
        print(json[0]["sno"].error!) 
        print("Temp Result didn't worked")
    }

Here is my JSON File

[
  {
  "sno": "21",
  "title": "title 1",
  "tableid": "table 1"
  },
  {
  "sno": "19",
  "title": "title 222",
  "tableid": "table 222"
  },
  {
   "sno": "3",
   "title": "title 333",
   "tableid": "table 333"
   }
]
6
  • 1
    First off, make a model out of your JSON data/file. Then you can now use SwiftyJSON with that. So if you have a model class named: "MyModel". Then you can have an array of MyModel objects, since your data/file is an array. Commented Jan 9, 2019 at 6:16
  • Thanks @Glenn for your answer. I'm a beginner in iOS so a sample code or a reference link will be helpful. Commented Jan 9, 2019 at 6:20
  • 1
    If you are a beginner then I suggest you look at Swift built in json support, google “Swift codable”. It is the most used and very good so it will be much easier to get help and find information about it. Commented Jan 9, 2019 at 6:24
  • 1
    show your tried code Commented Jan 9, 2019 at 6:34
  • 1
    I tried your code and it works. Can you explain what you mean by "does not work" and also show how you got the JSON and how you parsed it? Commented Jan 9, 2019 at 6:41

2 Answers 2

2

Actually it would be better to define a struct for object in Array.

public struct Item {

  // MARK: Declaration for string constants to be used to decode and also serialize.
  private struct SerializationKeys {
    static let sno = "sno"
    static let title = "title"
    static let tableid = "tableid"
  }

  // MARK: Properties

  public var sno: String?
  public var title: String?
  public var tableid: String?

  // MARK: SwiftyJSON Initializers
  /// Initiates the instance based on the object.
  ///
  /// - parameter object: The object of either Dictionary or Array kind that was passed.
  /// - returns: An initialized instance of the class.
  public init(object: Any) {
    self.init(json: JSON(object))
  }

  /// Initiates the instance based on the JSON that was passed.
  ///
  /// - parameter json: JSON object from SwiftyJSON.
  public init(json: JSON) {
    sno = json[SerializationKeys.sno].string
    title = json[SerializationKeys.title].string
    tableid = json[SerializationKeys.tableid].string
  }
}

And you need to map your array of JSON to Item objects.

var items = [Item]()
if let arrayJSON = json.array
  items = arrayJSON.map({return Item(json: $0)})
}
Sign up to request clarification or add additional context in comments.

Comments

2

Ditch SwiftyJSON and use Swift's built-in Codable with model objects instead:

typealias Response = [ResponseElement]

struct ResponseElement: Codable {
    let sno, title, tableid: String
}

do {
   let response = try JSONDecoder().decode(Response.self, from: data)
}
catch {
   print(error)
}

where data is the raw JSON data you got from your API.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.