parse nested JSON attributes in swift - ios

Having trouble which is probably so minor but my searches turn up nothing. I have a json model as follows :
//quick syntax to give you an idea of the model
{user:
{"name": "",
"password": "",
"medium" : {
{"title":"",
{"description":""}}}
I'm getting the above data model from a GET request to user and it returns all the info above but when i try to parse the "medium" information such as "title" & "description" I'm having no luck. I get these responses in Xcode that say
"Value of object 'AnyObject' not unwrapped, did you mean to use ! or ?"
and then when i click on the round red Xcode message to fix it it places !s and ?s everywhere but the error remains. Here is my parse method which worked perfectly fine when I was parsing only from the "medium". Any idea what I'm doing wrong?
a portion of the parse method where i get the same error for each attribute:
all lines with the same error indicated by *
// Parse JSON data
let jsonMedium = jsonResult?["user"] as! [AnyObject] //media where user is
for jsonMedia in jsonMedium {
let media = Media()
*media.title = jsonMedia["medium"]["title"] as! String
*media.description = jsonMedia["medium"]["description"] as! String
*media.category = jsonMedia["medium"]["category"] as! String
*media.image = jsonMedia["medium"]["image"] as! String
*if let IDer = jsonMedia["medium"]["id"] as? Int{
var stringIder = String(IDer)
media.identifierString = stringIder
}
Still no luck with anything. I don't understand why it works with regular JSON but Xcode won't accept anything when I try to obtain nested. All of your help has been appreciated. In the meantime here's the full method if it helps any further
func parseJsonData(data: NSData) -> [Media] {
var medium = [Media]()
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data,
options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
// Parse JSON data
let jsonMedium = jsonResult?["media"] as! [AnyObject]
for jsonMedia in jsonMedium {
let media = Media()
media.title = jsonMedia["medium"]["title"] as! String
media.description = jsonMedia["medium"]["description"] as! String
media.category = jsonMedia["medium"]["category"] as! String
media.image = jsonMedia["medium"]["image"] as! String
if let IDer = jsonMedia["medium"]["id"] as? Int{
var stringIder = String(IDer)
media.identifierString = stringIder
}
medium.append(media)
}
} catch {
print(error)
}
return medium
}

let json = [
"user" : [
"name" : "My Name",
"password" : "My Password",
"medium" : [
"title" : "My Title",
"description" : "My Description"
]
]
]
if let userJson = json["user"] as? [String : AnyObject] {
if let name = userJson["name"] as? String {
print("name: \(name)")
}
if let password = userJson["password"] as? String {
print("password: \(password)")
}
if let mediumJson = userJson["medium"] as? [String : AnyObject] {
if let title = mediumJson["title"] as? String {
print("title: \(title)")
}
if let description = mediumJson["description"] as? String {
print("description: \(description)")
}
}
}

Maybe it helps
let request : ASIFormDataRequest = ...your request
if request.responseString() != nil {
var jsonResponse : Dictionary<String, AnyObject>?
do{
jsonResponse = try NSJSONSerialization.JSONObjectWithData(request.responseData(), options: NSJSONReadingOptions.AllowFragments) as? Dictionary<String, AnyObject>
} catch _ {
//some error
}
}

FIXED IT! Took an entire day of deep thought and google/youtube/stack/brainstorming and your help but it was one changed line that got the whole thing going
// Parse JSON data
let jsonMedium = jsonResult?["user"]!["medium"] as? [AnyObject]
for jsonMedia in jsonMedium! {
let media = Media()
media.title = jsonMedia["title"] as! String
media.description = jsonMedia["description"] as! String
instead of :
let jsonMedium = jsonResult?["user"] as! [AnyObject] //media where user is
for jsonMedia in jsonMedium {
let media = Media()
*media.title = jsonMedia["medium"]["title"] as! String
*media.description = jsonMedia["medium"]["description"] as! String

Related

manually entered data in firebase issue

I am manually entering in data into my database and the only variable not getting passed from my database is the author and I do not know where I am going wrong.
func getAllArticles(handler: #escaping (_ articles: [Article])-> ()){
var articleArray = [Article]()
REF_ARTICLES.observeSingleEvent(of: .value) { (articleMessageSnapshot) in
guard let articleMessageSnapshot = articleMessageSnapshot.children.allObjects as? [DataSnapshot] else {return}
for article in articleMessageSnapshot {
let content = article.childSnapshot(forPath: "content").value as? String ?? "no content"
let author = article.childSnapshot(forPath: "author").value as? String ?? "no author"
let twitterHandle = article.childSnapshot(forPath: "twitterHandle").value as? String ?? "none"
let articleTitle = article.childSnapshot(forPath: "articleTitle").value as? String ?? "no title"
let date = article.childSnapshot(forPath: "date").value as? String ?? "no date"
let article = Article(content: content, author: author, twitterHandle: twitterHandle, ArticleTitle: articleTitle, date: date)
articleArray.append(article)
}
handler(articleArray)
}
}
Please check out below code
var articleArray = [Article]()
//REF_ARTICLES
let ref = Database.database().reference().child(“articles”)
ref.observe(.childAdded, with: { (snapshot) in
print(snapshot)
guard let dictionary = snapshot.value as? [String : AnyObject] else {
return
}
let articleObj = Article()
articleObj.Content = dictionary["content"] as? String
articleObj.Author = dictionary["author"] as? String
articleObj.Twitterhandle = dictionary["twitterHandle"] as? String
articleObj.Title = dictionary["articleTitle"] as? String
articleObj.Date = dictionary["date"] as? String
self. articleArray.append(articleObj)
}, withCancel: nil)
}
I am also working on similar app where i am storing data to firebase and retrieving. Below approach i used to fetch the data from firebase database. Please try once.
func getAllArticles(handler: #escaping (_ articles: [Article])-> ()) {
Database.database().reference().child("Articles").observe(.childAdded, with: { (snapshot) in
print("articles = \(snapshot)")
if let dict = snapshot.value as? [String: Any] {
let article = Article()
article.articleTitle = dict["articleTitle"] as? String
article.author = dict["author"] as? String
article.twitterHandle = dict["twitterHandle"] as? String
article.date = dict["date"] as? String
article.content = dict["content"] as? String
self.articleArray.append(article)
}
handler(articleArray)
}, withCancel: nil)
}
im not sure what the underlying issue was, but i fixed it by deleting "author" from the firebase tree and then adding it back

Could not cast value of type '__NSSingleObjectArrayI' to 'NSString'

When the iOS app trigger an API event named "addtocart" the app crashes returning below error.
url https://example.com/index.php/?route=api/cart/addToCart params
["product_id": "12098", "option": "{\n\n}", "quantity": "0", "token":
"2f42lqjie21cbzr1r0gsgphqtl", "width": "750.000000"] {
error = 1;
message = (
"Color required!"
); } Could not cast value of type '__NSSingleObjectArrayI' (0x1b5bee3b0) to 'NSString' (0x1b5bfaad8). 2018-08-02
19:50:28.493678+0300 Ishtari[35479:3846514] Could not cast value of
type '__NSSingleObjectArrayI' (0x1b5bee3b0) to 'NSString'
(0x1b5bfaad8). warning: could not execute support code to read
Objective-C class data in the process. This may reduce the quality of
type information available. (lldb)
The product has option called color so before adding to cart the user should choose the color of the item to checkout. The app is return the option name "Color" but not giving me the values to pick from ("option": "{\n\n}") which causing the app to crash giving above error when callingHTTPApi.
func callingHttppApi(){
DispatchQueue.main.async{
NetworkManager.sharedInstance.showLoader()
let sessionId = self.defaults.object(forKey:"ic_token");
let width = String(format:"%f", SCREEN_WIDTH * UIScreen.main.scale)
self.view.isUserInteractionEnabled = false
if self.whichApiToProcess == "addtocart"{
var requstParams = [String:String]();
requstParams["width"] = width
requstParams["product_id"] = self.productId
requstParams["ic_token"] = sessionId as? String
requstParams["quantity"] = self.quantityValue.text
do {
let jsonSortData = try JSONSerialization.data(withJSONObject: self.optionDictionary, options: .prettyPrinted)
let jsonSortString:String = NSString(data: jsonSortData, encoding: String.Encoding.utf8.rawValue)! as String
requstParams["option"] = jsonSortString
}
catch {
print(error.localizedDescription)
}
NetworkManager.sharedInstance.callingHttpRequest(params:requstParams, apiname:"cart/addToCart", cuurentView: self){success,responseObject in
if success == 1{
let dict = responseObject as! NSDictionary;
NetworkManager.sharedInstance.dismissLoader()
if dict.object(forKey: "fault") != nil{
let fault = dict.object(forKey: "fault") as! Bool;
if fault == true{
self.loginRequest()
}
}else{
print(dict)
self.view.isUserInteractionEnabled = true
let dict = responseObject as! NSDictionary
if dict.object(forKey: "error") as! Int == 0{
NetworkManager.sharedInstance.showSuccessSnackBar(msg: dict.object(forKey: "message") as! String)
let data = dict.object(forKey: "total") as! String
self.tabBarController!.tabBar.items?[3].badgeValue = data.components(separatedBy: " ")[0]
self.navigationCart(cartCount:data.components(separatedBy: " ")[0])
if self.goToBagFlag == true{
self.tabBarController!.selectedIndex = 3
}
}else{
NetworkManager.sharedInstance.showWarningSnackBar(msg: dict.object(forKey: "message") as! String)
}
}
}else if success == 2{
NetworkManager.sharedInstance.dismissLoader()
self.callingHttppApi()
}
}
}
When it crashes the console directs me to this line
}else{
NetworkManager.sharedInstance.showWarningSnackBar(msg: dict.object(forKey: "message") as! String)
}
Any help please!
The error is clear:
Could not cast value of type '__NSSingleObjectArrayI' (0x1b5bee3b0) to 'NSString' (0x1b5bfaad8)
The left side shows the actual type (Array), the right side is what you are doing wrong (String)
NetworkManager.sharedInstance.showWarningSnackBar(msg: dict.object(forKey: "message") as! [String])
If msg is supposed to be String join the strings
let messageArray = dict.object(forKey: "message") as! [String]
NetworkManager.sharedInstance.showWarningSnackBar(msg: messageArray.joined(separator: ", "))
By the way use Swift native types and key subscription
let dict = responseObject as! [String:Any]
...
NetworkManager.sharedInstance.showWarningSnackBar(msg: dict["message"] as! [String])
And – sorry – this syntax is horrible:
let jsonSortString:String = NSString(data: jsonSortData, encoding: String.Encoding.utf8.rawValue)! as String
Please replace it with
let jsonSortString = String(data: jsonSortData, encoding: .utf8)!

How do I reference an object within an array within an object in xcode 8?

I'm looking to try and reference all "titles" within this json (link here) in xcode 8. The issue is there's an object and array that need to be referenced (i believe) before I can pull the title data, and I'm not sure how to do that.
So far this is what i've got:
func fetchFeed(){
let urlRequest = URLRequest(url: URL(string: "http://itunes.apple.com/us/rss/topalbums/limit=10/json")!)
let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in
if error != nil {
print(error)
return
}
self.artists = [Artist]()
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]
if let feedFromJson = json["feed"]?["entry"] as? [[String : AnyObject]] {
for feedFromJson in feedsFromJson {
let feed = Feed()
if let entry = feedFromJson["entry"] as? String, let author = feedFromJson["domain"] as? String {
feed.entry = entry
article.headline = title
}
self.articles?.append(article)
}
}
DispatchQueue.main.async {
self.tableview.reloadData()
And thank you for your help in advance!
I'm working hard to try to understand what you need. If you want to get an Article array where the headline is the title label for the entry, here is how I cheated it out.
func articles(from json: Any?) -> [Article] {
guard let json = json as? NSDictionary, let entries = json.value(forKeyPath: "feed.entry") as? [NSDictionary] else {
return []
}
return entries.flatMap { entry in
guard let title = entry.value(forKeyPath: "title.label") as? String else {
return nil
}
var article = Article()
article.headline = title
return article
}
}
you call it as such
self.articles = articles(from: json)
NSDictionary has the method value(forKeyPath:) that is near magic. Calling json.value(forKeyPath: "feed.entry") returns an array of dictionaries. Each dictionary is an "entry" object in the json. Next, I map each entry to call entry.value(forKeyPath: "title.label") which returns a string.
If this is something more than a quick solution, then I would consider adding SwiftyJSON to your project.
func articles(from json: Any?) -> [Article] {
return JSON(json)["feed"]["entry"].arrayValue.flatMap { entry in
guard let title = entry["title"]["label"].string else {
return nil
}
var article = Article()
article.headline = title
return article
}
}
There is two kinds of titles.
the "feed" and the "entry".
if let entry = feedFromJson["entry"] as? String, let author = feedFromJson["domain"] as? String {
The practice of iOS is not this.
feedFromJson["entry"] is nil ,not a string . I guess you try to get the "feed" title.
if let entry = (json["feed"] as Dictionary)?["title"]
To get the "entry" title. Just traverse the array, and get the title.
let titleDict = feedFromJson["title"] as? Dictionary
let title = titleDict["title"] as? String
article.headline = title
Better to know the structure of the JSON data.
It's too quick.
if let feedFromJson = json["feed"]?["entry"] as? [[String :
AnyObject]] {
You should step by step.
if let feedFromJson = (json["feed"] as Dictionary)?["entry"] as? [[String : AnyObject]] {

Convert JSON nested objects

I am getting the following JSON from Foursquare API and I have been struggling with extracting the data:
{
"meta":{
"code":200,
"requestId":"58122e59498e5506a1b23580"
},
"response":{
"venues":[
{
"id":"4d56c381a747b60cd4a12c2b",
"name":"Sports Circle",
"contact":{},
"location":{
"lat":31.9,
"lng":35.9,
"labeledLatLngs":[
{
"label":"display",
"lat":31.9,
"lng":35.90
}
],
],
"confident":true
}
}
}
I want to get the name in venues in addition to the lat and lng values. I have tried this so far but it gets out of the second if statement at JVenues because it is nil:
func parseData (JSONData: Data){
do {
var readableJson = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! [String:AnyObject]
if let JResponse = readableJson ["response"] as? [String:AnyObject] {
if let JVenues = JResponse["venues"] as? [String:AnyObject]{
if let JName = JVenues["name"] as? String{
NSLog(JName)
}
}
}
} catch {
print(error)
}
}
This is what the other answers are getting at. Will probably make more sense if you can see it all laid out...
if let JResponse = readableJson ["response"] as? [String : AnyObject] {
if let JVenues = JResponse["venues"] as? [[String : AnyObject]] {
if let JName = JVenues.first?["name"] as? String {
NSLog(JName)
}
}
}
Note this only gets the FIRST name in the array of venues.
EDIT:
I prefer something like this. Define a struct and convert your dictionaries to the struct:
struct Venue {
var name: String?
var venueId: String?
init(_ venueDictionary: [String : AnyObject]) {
self.name = venueDictionary["name"] as? String
self.venueId = venueDictionary["id"] as? String
}
}
In your class create a property such as:
var venues = [Venue]()
From your JSON map the dictionaries to the venue array. I renamed variables that start with a capital for convention.
if let response = readableJson ["response"] as? [String : AnyObject] {
if let responseVenues = response["venues"] as? [[String : AnyObject]] {
self.venues = responseVenues.map({ Venue($0)) })
}
}
Use anywhere in your class like:
let venue = self.venues.first
print(venue?.name)
Or:
if let venue = self.venues.find({ $0.name == "Sports Circle" }) {
print("found venue with id \(venue.venueId)")
}

ParseJson data from array located in another array

Hello I'm trying to Parse parseJson data from an array. My problem is that I do not know how to access my response, because it is an array located in another array.
This is my response:
{
"finalResult": [
[
{
"Id":2,"latitude":"49.993820099043","longitude":"14.660662520439","usrId":2,"timeCreated":"2016-08-06 18:31:32"
}
],
[
{
"Id":3,"latitude":"49.993900553360","longitude":"14.660832398337","usrId":2,"timeCreated":"2016-08-06 18:29:37"
}
]
]
}
This is part of code I need help with:
if let parseJson = json {
if let events = parseJson["finalResult"] as? [AnyObject] {
for event in events {
for single in (event as? [AnyObject])! {
let Id = single["Id"] as! String
let latitude = single["latitude"] as! String
let longitude = single["longitude"] as! String
let usrId = single["usrId"] as! String
print(id)
print(latitude)
print(longitude)
print(usrId)
}
}
}
}
for event in events {
if let customObjects = event as? [AnyObject] {
for customObject in customObjects {
let Id = customObject["Id"] as! String
let latitude = customObject["latitude"] as! String
let longitude = customObject["longitude"] as! String
let usrId = customObject["usrId"] as! String
print(id)
print(latitude)
print(longitude)
print(usrId)
}
}
}

Resources