Parsing JSON response in Swift/iOS - json

I have the following JSON.
[{"chatId":"1","user1_id":"1212","user2_id":"8543211123","user1_name":"dave","user2_name":"daveee","user1_profile_pic":"http:\/\/graph.facebook.com\/1212\/picture?type=large","user2_profile_pic":"https:\/\/scontent-waw1-1.xx.fbcdn.net\/v\/t1.0-9\/1212.jpg?oh=c288ac7b31a61aee751e8ddafb05e78a&oe=57DC702E","message":{"1":{"message_id":"24242241","sender":"1212","chatId":"1","text":"hello i am","timestamp":"2016-05-24 17:13:08"},"2":{"message_id":"421421","sender":"1212","chatId":"1","text":"great","timestamp":"2016-05-24 17:15:08"}}},{"chatId":"2","user1_id":"23413524635","user2_id":"1212","user1_name":"Leo","user2_name":"dave","user1_profile_pic":"https:\/\/scontent-fra3-1.xx.fbcdn.net\/v\/l\/t1.0-1\/1212.jpg?oh=1212&oe=579AE3AE","user2_profile_pic":"http:\/\/graph.facebook.com\/1212\/picture?type=large","message":{"1":{"message_id":"21321213","sender":"1212","chatId":"2","text":"yes, hi","timestamp":"2016-05-25 15:46:57"}}}]
I want to loop through the message and for each counter, extract the values. This is my code:
for anItem in jsonArray as! [Dictionary<String, AnyObject>]
var chat_messages : [Message]? = nil
var count_messages = 0;
if let dataArray = anItem["message"] as? [Int : Dictionary<String, AnyObject> ] {
for onemessage in dataArray as! [Dictionary<String, AnyObject>] {
let curr_message = Message()
if let messageid = onemessage["message_id"] as? String {
curr_message.id = messageid
}
if let messagedate = onemessage["timestamp"] as? NSDate {
curr_message.date = messagedate
}
if let messagesender = onemessage["sender"] as? String {
curr_message.sender = messagesender
}
if let messagetext = onemessage["text"] as? String {
curr_message.text = messagetext
}
chat_messages![count_messages] = curr_message
count_messages = count_messages + 1
}
}
The problem is that the line if let dataArray = anItem["message"] as? [Int : Dictionary<String, AnyObject> ] { always fails and the if condition is never entered.

If you are using NSJSONSerialization, then every dictionary is of the type: [String : AnyObject]. So this line:
if let dataArray = anItem["message"] as? [Int : Dictionary<String, AnyObject> ] {
should be:
if let dataArray = anItem["message"] as? [String : AnyObject] {
And to loop through the message dictionaries inside dataArray you could replace:
for onemessage in dataArray as! [Dictionary<String, AnyObject>] {
with:
for (_, messageDictionary) in dataArray {
if let onemessage = messageDictionary as? [String : AnyObject] {
and the rest of your code to get the various values out of the onemessage dictionary should work as you have it written now, except that this line will crash:
chat_messages![count_messages] = curr_message
Because you are forced unwrapping chat_messages, which you initialized as nil:
var chat_messages : [Message]? = nil
instead of as an empty array:
var chat_messages = [Message]()

Related

Read Json in swift

Hi the question is i want to read the rows in this JSON is an Array, data is the headboard
>data: {..}
>rows [..]
-dataA: "Hi"
-dataB: "Hello"
im using :
if let json = try? JSONSerialization.jsonObject(with: data, options: []) {
if let information = json as? [String : Any]{
let data = information["data"] as? [String : Any]
let rowsN = information["rows"] as? [[String : Any]]
for datab in rowsN{
let dataA = datab ["dataA"] as? String ?? ""
let dataB = datab ["dataB"] as? String ?? ""
print(dataA, dataB)
}
Why is not working?, i think is cause rows is not a headboard, in that case , How could I do it?
let rowsN = information["rows"] as? [[String : Any]] - seems to be the buggy line of code.
As per your example, rows are within data. So you should be trying data["rows"] and not information["rows"].
let rowsN = data["rows"] as? [[String : Any]]
Hope this clarifies
let str = """
{
"data": {
"rows": [
{"dataA": "Hi"},
{"dataB": "Hello"}
]
}
}
"""
let data = Data(str.utf8)
if let json = try? JSONSerialization.jsonObject(with: data, options: []) {
if let information = json as? [String : Any] {
if let data = information["data"] as? [String : Any] {
if let rowsN = data["rows"] as? [[String : Any]] {
for datab in rowsN {
let dataA = datab ["dataA"] as? String ?? ""
let dataB = datab ["dataB"] as? String ?? ""
print(dataA)
print(dataB)
}
} else {
print("could not load rows")
}
} else {
print("could not load data")
}
} else {
print("could not load json as map")
}
} else {
print("could not load json as map")
}
I personally would use a Model class and use JSON Decoder in swift for this. The API you are using is the older Objective C based one.

Swift JSON Data

I received the JSON data as shown below, I want to get the "player_name" & "player_country" from this data. How do I get this?**enter image description here
In case, the image does not load, please refer the below output:
["team_key": 2611, "team_name": Leicester, "team_badge": https://apiv2.apifootball.com/badges/2611_leicester.png, "players": <__NSArrayI 0x600003331e00>(
{
"player_age" = 33;
"player_country" = Denmark;
"player_goals" = 0;
"player_key" = 140150332;
"player_match_played" = 30;
"player_name" = "Schmeichel Kasper";
"player_number" = 1;
"player_red_cards" = 0;
"player_type" = Goalkeepers;
"player_yellow_cards" = 2;
},
{
"player_age" = 23;
"player_country" = England;
"player_goals" = 3;
"player_key" = 2242127097;
"player_match_played" = 24;
"player_name" = "Chilwell Benjamin";
"player_number" = 3;
"player_red_cards" = 0;
"player_type" = Defenders;
"player_yellow_cards" = 2;
},
{.....}
I tried the following to get required data from this:
var mainArray = [[String: Any]]()
var tryArray = [[String]]()
for i in jsonArray {
if i["team_key"] as! String == teamID {
self.mainArray.append(i)
}
}
for i in self.mainArray {
self.tryArray.append(i["player_name"] as! [String])
}
I got this error with the above code:
Fatal error: Unexpectedly found nil while unwrapping an Optional value
let playerArray:NSDictionary = json as! NSDictionary
for var player in playerArray {
self.tryArray.append(player.objectForKey("player_name") as! String)
self.tryArray.append(player.objectForKey("player_country") as! String)
}
Thank you all for your response. I have tried this and it works.
var mainArray = NSArray()
var tryArray = [[String: Any]]()
for i in jsonArray {
if i["team_key"] as! String == teamID {
self.mainArray = i["players"] as! NSArray
}
}
for i in self.mainArray {
tryArray.append(i as! [String : Any])
}
What I did was I made mainArray as NSArray instead of [[String: Any]] and then copy it to tryArray which is of actually [[String: Any]] type and used tryArray for my work.
Thanks.

Parsing JSON in Swift and accessing values?

I have successfully parsed JSON for:
birthday = "04/10/1986";
id = 202038339983;
location = {
city = Jupiter;
country = "United States";
state = FL;
};
My question is when part of the JSON is:
submissions = {
data = (
{
"created_time" = "2018-02-16T05:11:56+0000";
id = "131448394823824_167398094382256";
viewer = "Any random string and/or emojis";
},
{
"created_time" = "2018-02-14T23:36:41+0000";
id = "809809871824_8908987486899";
message = "vday \Ud83d\Udda4\U2665\Ufe0f";
});}
How am I supposed to access created_time, id, viewer, and message?
I have been able to print the whole submissions JSON response to the console with this code :
guard let jsonD = responseFromServer as? [String : Any] else {return}
let subs1 = (jsonD["submissions"] as? [String : Any])
let accessSubs1 = theSubs1
guard let parsedPost = theSubs1 else {
return
}
My console will display:
["data": <__NSArrayI 0x6040001a86c0>(
{
"created_time" = "2018-02-16T05:11:56+0000";
id = "131448394823824_167398094382256";
viewer = "Any random string and/or emojis";
},
{
"created_time" = "2018-02-14T23:36:41+0000";
id = "809809871824_8908987486899";
message = "vday \Ud83d\Udda4\U2665\Ufe0f";
})]
My question is how should I parse the JSON so I can access the created_time inside submissions?
Here is the HTTP Request:
struct XClass: RequestProtocol {
var Path = "/User"
var parameters: [String : Any]? = ["stuff": "id, birthday, location, submissions"]
var aToken = aToken.current
var httpMethod: RequestHTTPMethod = .GET
var apiVersion: APIVersion = .defaultVersion
struct Response: ResponseProtocol {
var id = String()
var birthday = String()
var city = String()
var state = String()
var country = String()
var viewSubs = [String : Any]()
init(XResponse: Any?) {
guard let jsonD = XResponse as? [String : Any] else {return}
id = (jsonD["id"] as? String)!
birthday = (jsonD["birthday"] as? String)!
let XArr = (jsonD["location"] as? [String : String])
city = XArr!["city"]!
country = XArr!["country"]!
state = XArr!["state"]!
let subs1 = (jsonD["submissions"] as? [String : Any])
let accessSubs1 = theSubs1
guard let parsedPost = theSubs1 else {
return
}
viewSubs = theSubs1
}}}
func getXData(){
let connection = RequestConnection()
connection.add(XClass()) { response, result in
switch result {
case .success(let response):
print("Request Succeeded: \(response)\n\n\n")
case .failed(let error):
print("Request Failed: \(error)")
}}
connection.start()
}
Create a struct
struct Data: Decodable {
var created_time : String
var id : String
var viewer : String
}
call to the api url from URLSession
guard let url = URL(string: "your api url")
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error.localizedDescription)
} else {
guard let data = data else {return}
var data: [Data]() = JSONDecoder().decode(Data.self, data)
for dat in data{
print(dat.created_time)
print(dat.id)
print(dat.viewer)
}
}
If you are not using Decodable from Swift 4, or still in Swift 3,
then you can specify that the data in "submissions" is an array of dictionaries (double brackets) then you can iterate that.
Change
let subs1 = (jsonD["submissions"] as? [String : Any])
To
let subs1 = (jsonD["submissions"] as? [[String : Any]])
for sub in subs1 {
let time = sub["created_time "] as? [String : Any]
...
}

How to print out custom JSON object values after they have be downloaded?

I have a custom object:
class SampleJSON {
var costDictionary: [String: Any]
var airbnbDetails: [String: Any]
var airbnbUS: Int
var airbnbLocal: Int
init(costDictionary: [String: Any], airbnbDetails: [String: Any], airbnbUS: Int, airbnbLocal: Int){
self.costDictionary = costDictionary
self.airbnbDetails = airbnbDetails
self.airbnbUS = airbnbUS
self.airbnbLocal = airbnbLocal
}
init(resultsDictionary:[String: Any]){
costDictionary = (resultsDictionary["cost"] as? [String: Any])!
airbnbDetails = (costDictionary["airbnb_median"] as? [String: Any])!
airbnbUS = (airbnbDetails["USD"] as? Int)!
airbnbLocal = (airbnbDetails["CHF"] as? Int)!
}
I'm trying to print out the value of the objects after they have been downloaded:
static func downloadAllTableViewData(urlExtension:String) -> [SampleJSON] {
var sampleJSON = [SampleJSON]()//array of custom object
let usm = UrlSessionNetworkManager.sharedManager
if let jsonDictionary = usm.parseJSONFromData(urlExtension:urlExtension)
{
let resultDictionaries = jsonDictionary["result"] as! [[String : Any]]
for resultsDictionary in resultDictionaries {// enumerate through dictionary
let nomadInfo = SampleJSON(resultsDictionary: resultsDictionary)
sampleJSON.append(nomadInfo)
}
} else {
print("Error: Cannot retrieve JSON Data")
}
print(sampleJSON) //Print Data here
return sampleJSON
}
}
I'm trying to print out all the values of the custom object but when I try use print(sampleJSON) all I get printed out to console is
MyProject.SampleJSON
Any help is appreciated!
Add a description variable to your SampleJSON class, and make it conform to CustomStringConvertible. For example:
class SampleJSON: CustomStringConvertible {
var costDictionary: [String: Any]
var airbnbDetails: [String: Any]
var airbnbUS: Int
var airbnbLocal: Int
init(costDictionary: [String: Any], airbnbDetails: [String: Any], airbnbUS: Int, airbnbLocal: Int){
self.costDictionary = costDictionary
self.airbnbDetails = airbnbDetails
self.airbnbUS = airbnbUS
self.airbnbLocal = airbnbLocal
}
init(resultsDictionary:[String: Any]){
costDictionary = (resultsDictionary["cost"] as? [String: Any])!
airbnbDetails = (costDictionary["airbnb_median"] as? [String: Any])!
airbnbUS = (airbnbDetails["USD"] as? Int)!
airbnbLocal = (airbnbDetails["CHF"] as? Int)!
}
var description: String {
return "SampleJSON(cost: \(self.costDictionary), airbnb: \(self.airbnbDetails), airbnb US: \(self.airbnbUS), airbnb local: \(self.airbnbLocal))"
}
}

TableView Json Swift

I currently developing an app which list user object by making an request to a webservice, which send a response in JSON in this format :
{
"0":
{
"id":"30",
"title":"galaxys6",
"price":"550",
"description":"neuf",
"addedDate":"2015-07-16 15:04:24",
"user_id":"2",
"user_name":"",
"user_zipCode":"69003",
"category_id":"1",
"category_label":"PHONE",
"subcategory_id":"1",
"subcategory_label":"Phone",
"picture":"",
"bdd":{},
"picture_url":"http:\/\/jdl-barreme-orange.dyndns.org\/WEBSERVICE\/pictures\/galaxy s6.JPG"
},
"1":
{
"id":"31",
"title":"iphone4",
"price":"570",
"description":"neuf",
"addedDate":"2015-07-16 15:14:54",
"user_id":"2",
"user_name":"",
"user_zipCode":"69003",
"category_id":"1",
"category_label":"PHONE",
"subcategory_id":"1",
"subcategory_label":"Phone",
"picture":"",
"bdd":{},
"picture_url":"http:\/\/jdl-barreme-orange.dyndns.org\/WEBSERVICE\/pictures\/iphone.JPG"
},
}
For each object my webservice create a dictionary (0;1;2;3....)
I search a method to retrieve for each dictionary the value title and price and put them in a tableView.
Code I used (tableviewcontroller) :
if let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as? NSDictionary{
// 4
if let resp = jsonData["1"] as? [NSDictionary] {
NSLog("%#", resp)
// 5
for item in resp {
repositories.append(Repository(jsonData: item))
}
repository controller :
class Repository {
var name: String?
var description: String?
var html_url: String?
init(jsonData: NSDictionary) {
self.name = jsonData["id"] as? String
self.description = jsonData["description"] as? String
self.html_url = jsonData["title"] as? String
}
}
But it doesn't work, I put a breakpoint, and xcode stop to interpret here :
if let resp = jsonData["1"] as? [NSDictionary] {
NSLog("%#", resp)
What am I doing wrong?
Thank you.
Here's how to get the title and price for your JSON:
if let json = NSJSONSerialization.JSONObjectWithData(urlData!, options: nil, error: nil) as? [String:AnyObject] {
for (_, value) in json {
if let dict = value as? [String:AnyObject] {
if let title = dict["title"] as? String {
println(title)
}
if let price = dict["price"] as? String {
println(price)
}
}
}
}
This can also be used to init your Repository classes if you want:
class Repository {
var name: String?
var description: String?
var html_url: String?
init(jsonData: [String:AnyObject]) {
self.name = jsonData["id"] as? String
self.description = jsonData["description"] as? String
self.html_url = jsonData["title"] as? String
}
}
var repos = [Repository]()
if let json = NSJSONSerialization.JSONObjectWithData(urlData!, options: nil, error: nil) as? [String:AnyObject] {
for (_, value) in json {
if let dict = value as? [String:AnyObject] {
let repo = Repository(jsonData: dict)
repos.append(repo)
}
}
}
for repo in repos {
println(repo.name)
println(repo.description)
println(repo.html_url)
}
In the loop I'm ignoring the key: for (_, value) in json but you can use it if needed of course:
for (key, value) in json {
println(key) // "1", "2", ...
// ...
}
UPDATE:
Following your comment asking how to use this answer if your data format is different: if you want an array of dictionaries, change the typecast of the NSJSONSerialization result to reflect that: [[String:AnyObject]]. Next you can iterate over your array to get each dictionary properties:
if let jsonArray = NSJSONSerialization.JSONObjectWithData(urlData!, options: nil, error: nil) as? [[String:AnyObject]] {
for dict in jsonArray {
if let title = dict["title"] as? String {
println(title)
}
}
}
You are making a mistake here
if let resp = jsonData["1"] as? [NSDictionary]
This should be a NSDictionary not [NSDictionary], (which would be an array of dictionaries).
Also this conditional block
if let reposArray = jsonData["items"] as? [NSDictionary]
will never be executed because jsonData does not contain a key "items".
I guess it is the [NSDictionary]
if let resp = jsonData["1"] as? [NSDictionary]
[NSDictionary] is array of NSDictionary same as Array<NSDictionary>
just remove the brackets [] and change to
if let resp = jsonData["1"] as? NSDictionary