-4

let result = try?NSJSONSerialization.JSONObjectWithData(data,options:.MutableContainers)

How to take values from given JSON in swift2.3

({
       FirstName:"sample"
       LastName:"Data"
    },
    {
       FirstName:"sample1"
       LastName:"Data1"
 })

How can I take value from first name and add to array swift 2.3 and Xcode8

Senthil
  • 75
  • 10

2 Answers2

0

Ok, try this with json is the json dictionary

let firstname = json["FirstName"] as! String
Hieu Dinh
  • 692
  • 5
  • 18
0

The following code converts your given json into string then uses NSJSONSerialization to parse your json and finally print the values of FirstName and Lastname

let jsonString = "[{\"FirstName\":\"sample\",\"LastName\":\"Data\"},{\"FirstName\":\"sample1\",\"LastName\":\"Data1\"}]"
if let serializedJsonArray = try? NSJSONSerialization.JSONObjectWithData(jsonString.dataUsingEncoding(NSUTF8StringEncoding)!, options: .MutableContainers),
let parsedJsonArray = serializedJsonArray as? [[String:AnyObject]]{
for jsonArray in parsedJsonArray {
    if let firstName = jsonArray["FirstName"] as? String{
        print(firstName)
    }
    if let lastName = jsonArray["LastName"] as? String{
        print(lastName)
    }
}
}

Hope this helps.

nishith Singh
  • 2,968
  • 1
  • 15
  • 25