Parsing data from Dictionary - ios

Need to parse only the keys. Here keys are (10, 12, 2, 26). All these keys are changeable. Also, need to parse the fields array. Not getting the data(keys -10, 12, 2, 26) and can not access the fields array.
{
data = {
10 = {
fields = (
{
text = Activity;
value = 0;
},
{
text = Duration;
value = "0 hrs";
},
{
text = Achievement;
value = "0 hrs";
}
);
indicator = "+0%";
"indicator_color" = "#0ebb39-";
title = "Negotiation (Office)";
};
12 = {
fields = (
{
text = Activity;
value = 0;
},
{
text = Duration;
value = "0 hrs";
},
{
text = Achievement;
value = "0 hrs";
}
);
indicator = "+0%";
"indicator_color" = "#0ebb39-";
title = "Proposal Preparation";
};
2 = {
fields = (
{
text = Activity;
value = 0;
},
{
text = Duration;
value = "0 hrs";
},
{
text = Achievement;
value = "0 hrs";
}
);
indicator = "+0%";
"indicator_color" = "#0ebb39-";
title = "Departmental Work";
};
26 = {
fields = (
{
text = Activity;
value = 0;
},
{
text = Duration;
value = "0 hrs";
},
{
text = Achievement;
value = "0 times";
}
);
indicator = "+0%";
"indicator_color" = "#0ebb39-";
title = "External Meeting";
};
indicator = "+0%";
"indicator_color" = "#0ebb39-";
title = "Pre-sales (Office)";
};
};
"error_code" = 0;
message = "Dashboard Summary";
status = success;
success = 1;
}
Here is some of my code where I am trying to parse it using Alamofire:
Alamofire.request(url, method: .get, parameters: parameters,headers:headers).responseJSON { response in
print(parameters)
switch response.result{
case.success(let data):
print("Success")
print(response)
//response.result.value
if let dataDict = data as? [String: Any] {
print("Here is my dataDict -\(dataDict)")
guard let rootData = dataDict["data"] as? [String: Any] else {return}
}
case .failure(let error)://let error
print("dddd",error.localizedDescription)
}
}

Related

IOS/Swift/SwiftyJSON: Parse Nested JSON from IBM Watson/Bluemix API

I am receiving some JSON from IBM Watson's tone analyzer API in the format shown below for a piece of text. What I would like to do is capture JSON in an object with a property and value for the main tones, eg. anger:.218 disgust:2.20 etc. for each section of text analyzed. As I analyze more pieces of text I want to add them to an array of these objects.
At this stage, I just want to do something with the api using SwiftyJSON but am confused by the syntax:
The following code, just prints out as nil:
let anger = JSON(value)["results"][0]["anger"].array?.map { json in
json["anger"].stringValue
}
print (anger)
Would appreciate guidance on how to actually grab the tones and their values.... Thanks in advance for any suggestions.
JSON looks like this:
{
"document_tone" = {
"tone_categories" = (
{
"category_id" = "emotion_tone";
"category_name" = "Emotion Tone";
tones = (
{
score = "0.218727";
"tone_id" = anger;
"tone_name" = Anger;
},
{
score = "0.210102";
"tone_id" = disgust;
"tone_name" = Disgust;
},
{
score = "0.060026";
"tone_id" = fear;
"tone_name" = Fear;
},
{
score = "0.076444";
"tone_id" = joy;
"tone_name" = Joy;
},
{
score = "0.176849";
"tone_id" = sadness;
"tone_name" = Sadness;
}
);
},
{
"category_id" = "language_tone";
"category_name" = "Language Tone";
tones = (
{
score = 0;
"tone_id" = analytical;
"tone_name" = Analytical;
},
{
score = 0;
"tone_id" = confident;
"tone_name" = Confident;
},
{
score = 0;
"tone_id" = tentative;
"tone_name" = Tentative;
}
);
},
{
"category_id" = "social_tone";
"category_name" = "Social Tone";
tones = (
{
score = "0.02278";
"tone_id" = "openness_big5";
"tone_name" = Openness;
},
{
score = "0.340597";
"tone_id" = "conscientiousness_big5";
"tone_name" = Conscientiousness;
},
{
score = "0.541852";
"tone_id" = "extraversion_big5";
"tone_name" = Extraversion;
},
{
score = "0.545246";
"tone_id" = "agreeableness_big5";
"tone_name" = Agreeableness;
},
{
score = "0.743194";
"tone_id" = "emotional_range_big5";
"tone_name" = "Emotional Range";
}
);
}
);
};
}
You can quickly and easily accomplish this with Codable structs in swift 4 and URLSession.
Here is a pure swift solution to your problem. The struct properties map to your json tree, and you might need to change the structs to match the json you're receiving. The json you posted above is in an invalid format otherwise I could have mapped it correctly.
struct Tone: Codable {
let score: Double
let tone_id: String
let tone_name: String
}
struct Category: Codable {
let category_id: String
let category_name: String
let tones: [Tone]
}
struct DocumentTone: Codable {
let toneCategories: [Category]
}
final class ToneService {
static func fetchTone(toneUrl: URL, _ completion: #escaping([DocumentTone]) -> Void) {
URLSession.shared.dataTask(with: toneUrl) { (data, response, error) in
guard let data = data else { return }
do {
let decoder = JSONDecoder()
// If your json returns a single object, use DocumentTone.self instead of [DocumentTone].self, as well change the completion
let tone = try decoder.decode([DocumentTone].self, from: data)
completion(tone)
} catch let error {
print(error.localizedDescription)
}
}.resume()
}
}

Filter an array using value from another array

I need to filter Schedules[ ] Array using Dates[ ] Array where "date_id" == "schedule_date_id".
I need only those elements where date_id matches schedule_date_id and then reload the table. I need to show Schedules according to dates.
What is the Swift code for this?
Below is the JSON I have:
{
dates = (
{
"date_date" = "2017-11-04";
"date_id" = 4;
"date_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedule-days/day_1.png";
"date_title" = "Day 1";
},
{
"date_date" = "2017-11-05";
"date_id" = 5;
"date_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedule-days/day_2.png";
"date_title" = "Day 2";
}
);
schedules = (
{
"schedule_date" = "2017-11-04";
"schedule_date_id" = 4;
"schedule_description" = "Schedule 7";
"schedule_end_time" = "14:00:00";
"schedule_id" = 7;
"schedule_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedules/day_1.png";
"schedule_location" = Mumbai;
"schedule_start_time" = "11:00:00";
},
{
"schedule_date" = "2017-11-04";
"schedule_date_id" = 4;
"schedule_description" = "Schedule 8";
"schedule_end_time" = "14:00:00";
"schedule_id" = 8;
"schedule_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedules/day_1.png";
"schedule_location" = Mumbai;
"schedule_start_time" = "11:00:00";
},
{
"schedule_date" = "2017-11-04";
"schedule_date_id" = 4;
"schedule_description" = "Schedule 9";
"schedule_end_time" = "14:00:00";
"schedule_id" = 9;
"schedule_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedules/day_1.png";
"schedule_location" = Mumbai;
"schedule_start_time" = "11:00:00";
},
{
"schedule_date" = "2017-11-05";
"schedule_date_id" = 5;
"schedule_description" = "Schedule 10";
"schedule_end_time" = "14:00:00";
"schedule_id" = 10;
"schedule_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedules/day_2.png";
"schedule_location" = Mumbai;
"schedule_start_time" = "11:00:00";
}
);
}
Use like this:
Swift 4
let filteredSchedules = schedules.filter { schedule in
dates.contains(where: { $0["date_id"] as! Int == schedule["schedule_id"] as! Int }
)}
print(filteredSchedules)
Swift 3
let filteredSchedules = schedules.filter { schedule in
dates.contains({ $0["date_id"] as! Int == schedule["schedule_id"] as! Int })
}
print(filteredSchedules)
To filter schedules array using date_id from datesArray
let dateId = 4
let filteredSchedules = schedules.filter { schedule in
dates.contains(where: { dateId as! Int == schedule["schedule_id"] as! Int }
)}
print(filteredSchedules)
To assign dates to collectionView cell
cell.titleLabel.text = datesArray[indexPath.row]["datesArray"]
Try with below code
let newArray = schedulesArray.filter { (schedulesDic) -> Bool in
return datesArray.contains(where: { (datesDic) -> Bool in
if schedulesDic["schedule_id"] as! Int == datesDic["date_id"] as! Int{
return true
}
else {
return false
}
})
}
print(newArray)
Or short form of above code
let newArray = schedulesArray.filter { (schedulesDic) -> Bool in
datesArray.contains(where: { $0["date_id"] as! Int == schedulesDic["schedule_id"] as! Int })
}
print(newArray)

Swift 3: Accessing nested dictionary from returns nil?

I'm having a problem trying to access a nested Dictionary, which seems to return nil.
Here's the output my info of type Dictionary<String, Any>:
info = ["list_order": 1, "name": Some Text, "id": 1, "menu_items": {
1 = {
"food_name" = "String";
"food_picture" = "link";
"food_price" = "2.00";
};
2 = {
"food_name" = "String";
"food_picture" = "link";
"food_price" = "5.00";
id = 2;
};
}]
The output of info["menu_items"]:
info["menu_items"] = {
1 = {
"food_name" = "String";
"food_picture" = "link";
"food_price" = "2.00";
id = 1;
};
2 = {
"food_name" = "String";
"food_picture" = "link";
"food_price" = "5.00";
id = 2;
};
}
However, the following assigning produces a nil in test:
let test = info["menu_items"] as? Dictionary<Int, Any>
Is there something not obvious or am I not understanding basic fundamentals?
If your key is not Int type then most probably it is of String type, try once using [String: Any].
if let menuItems = info["menu_items"] as? [String: Any] {
print(menuItems)
}

Get index of object from array of dictionary with key value

I have one array with dictionary.
Now i want to get index number of object with particular key value.
Like key = "xyz" and value = "abc".
I need index of object having above matching in dictionary.
{
Id = 20085;
IsNew = 1;
Title = Circular;
},
{
Id = 20088;
IsNew = 0;
Title = Query;
},
{
Id = 20099;
IsNew = 1;
Title = Blog;
},
{
Id = 20104;
IsNew = 1;
Title = News;
},
{
Id = 20172;
IsNew = 1;
Title = AssignTask;
},
{
Id = 20183;
IsNew = 1;
Title = Gallery;
},
{
Id = 20204;
IsNew = 1;
Title = Poll;
},
{
Id = 20093;
IsNew = 1;
Title = Assignment;
},
{
Id = 20209;
IsNew = 1;
Title = Activity;
},
{
Id = 20130;
IsNew = 1;
Title = Behaviour;
},
{
Id = 20180;
IsNew = 1;
Title = Result;
}
now i need index of object with having key = "Title" and value = "result"
You can use indexOf(_:) for this:
let index = array.indexOf{ $0["key"] == value }
In Swift 3.0, it's been renamed to index(Where:):
let index = array.index{ $0["key"] == value }
You can see this in action here.
Here is what you should be doing after using a json parser
let array:NSArray = [
[
"Id": 20130,
"IsNew": 1,
"Title":"Behaviour"
],
[
"Id": 20180,
"IsNew": 1,
"Title":"Result"
]]
let k = array as Array
let index = k.indexOf {
if let dic = $0 as? Dictionary<String,AnyObject> {
if let value = dic["Title"] as? String
where value == "Result"{
return true
}
}
return false
}
print(index) // index
I'd start with mapping the id values to an array, then getting the index of the id you're looking for.
func getIndexOf(itemID: Int) -> Int {
//Map out all the ids from the array of dictionaries into an array of Integers
let keysArray = dictionaries.map { (dictionary) -> Int in
return dictionary.value(forKey: "Id")
}
//Find the index of the id you passed to the functions
return keysArray.index(of: itemID)
}
other mainstream way to do this
func getIndex(of key: String, for value: String, in dictionary : [[String: Any]]) -> Int{
var count = 0
for dictElement in dictionary{
if dictElement.keys.contains(key) && dictElement[key] as! String == value{
return count
}
else{
count = count + 1
}
}
return -1
}
var sampleDict : [[String:Any]] = [
[
"Id" : 20130,
"IsNew" : 1,
"Title" : "Behaviour"
],
[
"Id" : 20130,
"IsNew" : 1,
"Title" : "Result"
],
]
let index = getIndex(of: "Title", for: "Result", in: sampleDict)
print(index)
This will print 1.
This answer is for the Swift 5.2 and above versions.
arr.append(["Title":titleName,"ID":1,"Result":true])
arr.append(["Title":titleName,"ID":2,"Result":true])
arr.append(["Title":titleName,"ID":3,"Result":true])
arr.append(["Title":titleName,"ID":4,"Result":true])
This is an array of dictionary format. And if you want to find an index of an object where ID == 3
func findIndex(Id : Int ) -> Int? {
guard let index = arr.firstIndex(where: {
if let dic = $0 as? Dictionary<String,AnyObject> {
if let value = dic["ID"] as? Int, value == Id {
return true
}
}
return false
}) else { return nil }
return index
}
let myIndex = findIndex(3)
self.arr.insert( ["Title":titleName,"ID":10,"Result":true]) at: myIndex)

How to check value exists in array of dictionary as value?

{
QueueId = 27;
SongId = 38;
artWorkURL = "<null>";
duration = 58258;
"stream_url" = "https://api.soundcloud.com/tracks/233301835/stream";
title = Magenta;
trackID = 233301835;
userAvatar = "https://i1.sndcdn.com/avatars-000188204071-llusgk-large.jpg";
userName = Agiv;
},
{
QueueId = 27;
SongId = 39;
artWorkURL = "<null>";
duration = 79000;
"stream_url" = "https://api.soundcloud.com/tracks/233301833/stream";
title = Nino;
trackID = 233301833;
userAvatar = "https://i1.sndcdn.com/avatars-000157591669-eva3mg-large.jpg";
userName = "SWR Umwelt und Ern\U00e4hrung";
}
My array of dictionary format is as above and multiple tracks i want to check 27 is already there or not ?
You can do this with the filter function
let queueID27Exists = !array.filter({$0["QueueId"] as? Int == 27}).isEmpty
This answer is for your previous JSON object!
if let results : NSDictionary = post.objectForKey("data") as? NSDictionary {
let array:NSArray = (results.valueForKey("track") as! NSArray)
if "25" == array[0].valueForKey("trackID") as? String {
NSLog("YES")
} else {
NSLog("NO")
}
}
var found = false
for (_, data) in post {
let track = data["track"]
if track["trackID"] == "25" {
found = true
}

Resources