I have data which type [AnyHashable] given in below;
"options": [
{
"index": 0,
"label": "Choice 1"
},
{
"index": 1,
"label": "Choice 2"
},
{
"index": 2,
"label": "Choice 3"
}
],
I want to get all "label" values an the other [AnyHashable] . I cant find the best way. How can I do it?
You can define related models and decode JSON to your model like this:
let jsonString = """
{
"options": [
{
"index": 0,
"label": "Choice 1"
},
{
"index": 1,
"label": "Choice 2"
},
{
"index": 2,
"label": "Choice 3"
}
]
}
"""
struct Option: Codable {
let index: Int
let label: String
}
struct Model: Codable {
let options: [Option]
}
let data = jsonString.data(using: .utf8)!
let decoder = JSONDecoder()
let model = try! decoder.decode(Model.self, from: data)
let labels = model.options.map(\.label)
print(labels)
//["Choice 1", "Choice 2", "Choice 3"]
Thanks for your useful recommendation. I think it is some weirds but works.
func options(for question: [String : Any?]) -> [AnyHashable] {
var qoptions: [String] = []
for val in question["options"] as! [[String:Any]] {
let value = val["label"] as! String
qoptions.append(value)
}
return qoptions as! [AnyHashable]
}
Related
How would I decode this json file with JSONDecoder().decode
{
"text": "pear",
"parsed": [
{
"food": {
"foodId": "food_bq6stkiaxkwhxia9q4v7wanjnew0",
"label": "Pear",
"nutrients": {
"ENERC_KCAL": 57,
"PROCNT": 0.36,
"FAT": 0.14,
"CHOCDF": 15.23,
"FIBTG": 3.1
},
"category": "Generic foods",
"categoryLabel": "food",
"image": "https://www.edamam.com/food-img/65a/65aec51d264db28bbe27117c9fdaaca7.jpg"
}
}
],
"hints": [
{
"food": {
"foodId": "food_bq6stkiaxkwhxia9q4v7wanjnew0",
"label": "Pear",
"nutrients": {
"ENERC_KCAL": 57,
"PROCNT": 0.36,
"FAT": 0.14,
"CHOCDF": 15.23,
"FIBTG": 3.1
},
"category": "Generic foods",
"categoryLabel": "food",
"image": "https://www.edamam.com/food-img/65a/65aec51d264db28bbe27117c9fdaaca7.jpg"
},
"measures": [
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_unit",
"label": "Whole",
"qualified": [
{
"qualifiers": [
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Qualifier_large",
"label": "large"
}
]
},
{
"qualifiers": [
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Qualifier_small",
"label": "small"
}
]
},
{
"qualifiers": [
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Qualifier_medium",
"label": "medium"
}
]
}
]
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_serving",
"label": "Serving"
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_half",
"label": "Half"
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_slice",
"label": "Slice"
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_gram",
"label": "Gram"
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_ounce",
"label": "Ounce"
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_pound",
"label": "Pound"
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_kilogram",
"label": "Kilogram"
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_cup",
"label": "Cup"
}
The file goes on and on.
I try to do [String] but it says it expects a dictionary,
So I put in a dictionary, then it says it expects another dictionary,
This repeats until I have this:
let posts = try! JSONDecoder().decode([String: [String: [String: String]]].self, from: data!)
Then I get the error
"Expected to decode Dictionary<String, Any> but found a string/data instead.", underlyingError: nil))
Though when I go back one to do a string/data I get this error
"Expected to decode String but found a dictionary instead.", underlyingError: nil))
Help?
As mentioned in the comments you can use a quick parser, however
you need to understand why is this happening, the issue here is that you're passing [String: [String: [String: String]]] which is not valid skeleton for this key you can start breaking down each layer alone to understand first level for example [String, Any] and then start exporting each key and decode that stand alone, using the above link your models should be something like that.
// MARK: - Foo
struct Foo: Codable {
let text: String
let parsed: [Parsed]
let hints: [Hint]
}
// MARK: - Hint
struct Hint: Codable {
let food: Food
let measures: [Measure]
}
// MARK: - Food
struct Food: Codable {
let foodID, label: String
let nutrients: Nutrients
let category, categoryLabel: String
let image: String
enum CodingKeys: String, CodingKey {
case foodID = "foodId"
case label, nutrients, category, categoryLabel, image
}
}
// MARK: - Nutrients
struct Nutrients: Codable {
let enercKcal: Int
let procnt, fat, chocdf, fibtg: Double
enum CodingKeys: String, CodingKey {
case enercKcal = "ENERC_KCAL"
case procnt = "PROCNT"
case fat = "FAT"
case chocdf = "CHOCDF"
case fibtg = "FIBTG"
}
}
// MARK: - Measure
struct Measure: Codable {
let uri: String
let label: String
let qualified: [Qualified]?
}
// MARK: - Qualified
struct Qualified: Codable {
let qualifiers: [Qualifier]
}
// MARK: - Qualifier
struct Qualifier: Codable {
let uri: String
let label: String
}
// MARK: - Parsed
struct Parsed: Codable {
let food: Food
}
And you can use it simply like this:
let foo = try? newJSONDecoder().decode(Foo.self, from: jsonData)
I'm learning swift and objective-c. I want to extract the "show_name" in my Json for all shows.
This is my json :
{
"data": {
"12 Monkeys": {
"air_by_date": 0,
"anime": 0,
"cache": {
"banner": 1,
"poster": 1
},
"indexerid": 272644,
"language": "en",
"network": "Syfy",
"next_ep_airdate": "",
"paused": 0,
"quality": "SD",
"show_name": "12 Monkeys",
"sports": 0,
"status": "Continuing",
"subtitles": 1,
"tvdbid": 272644
},
"2 Broke Girls": {
"air_by_date": 0,
"anime": 0,
"cache": {
"banner": 1,
"poster": 1
},
"indexerid": 248741,
"language": "en",
"network": "CBS",
"next_ep_airdate": "2016-10-10",
"paused": 0,
"quality": "SD",
"show_name": "2 Broke Girls",
"sports": 0,
"status": "Continuing",
"subtitles": 0,
"tvdbid": 248741
},
"American Horror Story": {
"air_by_date": 0,
"anime": 0,
"cache": {
"banner": 1,
"poster": 1
},
"indexerid": 250487,
"language": "en",
"network": "FX (US)",
"next_ep_airdate": "2016-09-14",
"paused": 0,
"quality": "SD",
"show_name": "American Horror Story",
"sports": 0,
"status": "Continuing",
"subtitles": 0,
"tvdbid": 250487
},
This is my code and I use swiftyJson :
class ViewController: UIViewController {
let baseURL = "http://xxx.xxx.xxx.xxx:8083/api/api/?cmd=shows&sort=name&paused=0"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
getJSON()
}
func getJSON(){
let url = NSURL(string: baseURL)
let request = NSURLRequest(URL: url!)
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let task = session.dataTaskWithRequest(request)
{ (data, response, error) -> Void in
if error == nil {
let swiftyJSON = JSON(data: data!)
print(swiftyJSON)
let title = swiftyJSON["data"]["Sense8"]["show_name"]
print(title)
}else {
print("there was an error")
}
}
task.resume()
}
}
Your swiftyJSON is an array of JSON, (i.e. [JSON]), therefore you are not able to access it by subscript.
You can parse it as follow :
if let dataArray = swiftyJSON.array{
for json in dataArray {
// data information
let showName = json["show_name"].stringValue
let status = json["status"].stringValue
let subtitles = json["subtitles"].stringValue
print(showName)
print(status)
print(subtitles)
}
}
First lets see the json data.
[
{
"id": "244",
"name": "PIZZAS",
"image": "",
"coupon": "1",
"icon": "",
"order": "1",
"aname": "",
"options": "2",
"subcategory": [
{
"id": "515",
"name": "MARGARITA",
"description": "Cheese and Tomato",
"image": "",
"icon": "",
"coupon": "1",
"order": "1",
"aname": "",
"options": "2",
"item": [
{
"id": "1749",
"name": "9 Inch Thin & Crispy Margarita",
"description": "",
"price": "3.40",
"coupon": "1",
"image": "",
"options": "2",
"order": "1",
"addon": "495",
"aname": "",
"icon": ""
}]
}]
}]
I have used Alamofire and getting response through this code below:
Alamofire.request(.GET, myUrl, parameters:nil , encoding: .JSON)
.validate()
.responseString { response in
print("Response String: \(response.result.value)")
}
.responseJSON { response in
print("Response JSON: \(response.result.value)")
if let jsonResult = response as? Array<Dictionary<String,String>> {
let Name = jsonResult[0]["name"]
let ID = jsonResult[0]["id"]
let Order = jsonResult[0]["order"]
print("JSON: Name: \(Name)")
print("JSON: ID: \(ID)")
print("JSON: Order: \(Order)")
}
}
But after getting response data I am not able to get any value. Here I want to fetch all data like name,id and subcategory - how to implement this?
You have more than one problem there.
First response is of type Response<Anyobject, NSError>, it's not the parsed object you're looking for, instead you should use response.result.value as you did for the log.
Second even if you tried to cast response.result.value to Array<Dictionary<String,String>> it will not pass because in your json data you have ann inner array subcategory which cannot be casted to Dictionary<String, String>
This code should work for you:
Alamofire.request(.GET, myUrl, parameters:nil , encoding: .JSON)
.validate()
.responseString { response in
print("Response String: \(response.result.value)")
}
.responseJSON { response in
print("Response JSON: \(response.result.value)")
let array = response.result.value as! Array<NSDictionary>
for item in array
{
let Name = item["name"]
let ID = item["id"]
let Order = item["order"]
let Subcategories = item["subcategory"] as! Array<NSDictionary>
for subCategory in Subcategories
{
let subId = subCategory["id"]
}
}
}
And here is the results in the playground:
Cheers.
How can I access the original response json data without using object mapping. I have the followingresponse data. it contains a nextSyncToken which used to execute the query and a collection of items (within the items session).
I created a object mapping of the Item object which represent the contents in items. However, I also need the nextSyncToken field. How can I access it without object mapping. Since the syncToken has no relationship with object mapping. How can i deal with this.
{
"kind": "calendar#events",
"nextSyncToken": "COib8eSw78gCEOib8eSw78gCGAU=",
"items": [
{
"id": "_74rk4cpg84o42b9k8or3gb9k74s34b9p8ks34b9m851kac9m64rk4ci36g",
"created": "2010-04-16T11:09:31.000Z",
"updated": "2010-04-16T11:10:27.487Z",
"summary": "iCal test 1",
"start": {
"dateTime": "2010-03-16T21:00:00+08:00"
},
"end": {
"dateTime": "2010-03-16T22:00:00+08:00"
}
},
{
"id": "_752j2h1j6cq4cba68csjeb9k8p33eba1692k4ba284qj8ea688rj2chh6c",
"status": "confirmed",
"created": "2011-10-18T09:36:02.000Z",
"updated": "2011-10-18T09:36:02.000Z",
"summary": "New Event",
"start": {
"dateTime": "2011-10-18T03:45:00+08:00"
},
"end": {
"dateTime": "2011-10-18T08:15:00+08:00"
}
}
]
}
My code of mapping:
let eventMapping = RKEntityMapping(forEntityForName: "SGEvent", inManagedObjectStore: managedObjectStore)
eventMapping.addAttributeMappingsFromDictionary([
"id": "identifier",
"summary": "summary",
"created": "createdAt",
"updated": "updatedAt",
"location": "location",
"description": "notes",
"start.date": "allDayStart",
"end.date": "allDayEnd"
])
let startTimeMapping = RKAttributeMapping(fromKeyPath: "start.dateTime", toKeyPath: "startTime")
startTimeMapping.valueTransformer = self.googleDateTransformer
eventMapping.addPropertyMapping(startTimeMapping)
let endTimeMapping = RKAttributeMapping(fromKeyPath: "end.dateTime", toKeyPath: "endTime")
endTimeMapping.valueTransformer = self.googleDateTransformer
eventMapping.addPropertyMapping(endTimeMapping)
eventMapping.identificationAttributes = ["identifier"]
let responseDescriptor = RKResponseDescriptor(mapping: eventMapping, method: .GET,
pathPattern: "calendars/:calendarId/events", keyPath: "items",
statusCodes: RKStatusCodeIndexSetForClass(RKStatusCodeClass.Successful))
objectManager.addResponseDescriptor(responseDescriptor)
My request operation:
objectManager.getObjectsAtPath("calendars/\(identifier)/events",
parameters: [self.ACCESS_TOKEN: accessToken], success: { (operation, results) -> Void in
callback?(results: nil, error: nil)
}) { (_, error) -> Void in
print(error)
}
Generally you would add other response descriptors with appropriate mappings to deal with this issue.
When using objectManager.getObjectsAtPath you can get the raw data, assuming that you have some other response descriptor which will result in the success block being called, by navigating to the response data in the HTTP operation (which you can then unpack however you see fit):
operation.HTTPRequestOperation.responseData
(or use responseString instead of responseData).
if let dict = try? NSJSONSerialization.JSONObjectWithData(operation.HTTPRequestOperation.responseData, options: .AllowFragments) as? [String: AnyObject],
let nextSyncToken = dict?["nextSyncToken"] as? String{
print(nextSyncToken)//get the nextSyncToken
}
Problem: Attempting to consume a JSON payload which contains a dynamic Object name. Since the names of these objects are not sequential or derived from a pattern, I'm unable to travers the payload with SwiftyJSON. A sample of the dynamic objects looks like this denoted in bold:
"180A": {
"id": "180A",
"label": "Oceanside Gate",
"path": "North",
"index": "1"
},
"195C": {
"id": "195C",
"label": "Dune Beach Gate",
"path": "North",
"index": "2"
},
"211F": {
"id": "211F",
"label": "Sunset Harbor Gate",
"path": "North",
"index": "3"
}
Sample JSON Payload:
{
"Direction": {
"NorthGates": {
"180A": {
"id": "180A",
"label": "Oceanside Gate",
"path": "North",
"index": "1"
},
"195C": {
"id": "195C",
"label": "Dune Beach Gate",
"path": "North",
"index": "2"
},
"211F": {
"id": "211F",
"label": "Sunset Harbor Gate",
"path": "North",
"index": "3"
}
}
}
}
Using SwiftyJSON I'm able to successfully print the label of a known Object, such as "180A":
DataManager.getTopAppsDataFromFileWithSuccess { (data) -> Void in
let json = JSON(data: data)
if let gateLabel = json["Direction"]["NorthGates"]["180A"]["label"].stringValue {
//output: "Oceanside Gate"
println("NSURLSession: \(gateLabel)")
}
}
Since the dynamic object names are not static, I'm unable to use the pre-defined object names in the code above to locate the label value. The following attempts return nil values:
//stringValue = nil
if let gateLabel = json["Direction"]["NorthGates"][0].stringValue {
println("NSURLSession: \(gateLabel)")
}
//stringValue = nil
if let gateLabel = json["Direction"]["NorthGates"][0]["label"].stringValue {
println("NSURLSession: \(gateLabel)")
}
This is the solution:
if let gates:[String: JSON] = json["Direction"]["NorthGates"].dictionaryValue {
for item in gates {
println("Dynamic Object Gate Name: \(item.0)") //Gate Name
println(item.1["label"].stringValue) //Gate Label
println(item.1["path"].stringValue) //Gate Path
println(item.1["index"].stringValue) //Gate Index
}
}