How to get value from JSON api into class variables - json

I am new to swift 3 and alamofire.
Need an help to parse a JSON response to a variable.
func alamofireGet() {
let todoEndpoint: String = "http://54.244.108.186:4000/api/item_upc"
Alamofire.request(todoEndpoint)
.responseJSON { response in
guard response.result.error == nil else {
// got an error in getting the data, need to handle it
print("error calling POST on /todos/1")
print(response)
print(response.result.error!)
return
}
// make sure we got some JSON since that's what we expect
guard let json = response.result.value as? [String: Any] else {
print("didn't get todo object as JSON from API")
print("Error: \(response.result.error)")
return
}
// get and print the title
guard let todoTitle = json["ITEM"] as? String else {
print(json)
print("Could not get todo title from JSON")
return
}
print("The title is: " + todoTitle)
}
}
Below is the response of the json
["item_upc": <__NSArrayI 0x170245ee0>(
{
ITEM = 458698;
UPC = 14721632;
},
{
ITEM = 458766;
UPC = 14721649;
},
{
ITEM = 458782;
UPC = 14724862;
},
{
ITEM = 458800;
UPC = 14723070;
}
)
, "Error": 0, "Message": Success]
Can some one help how i can get value of item and upc into a local variable.

You can get values from JSON like this:-
//Response from server
guard let json = response.result.value as? [String: Any] else {
print("didn't get todo object as JSON from API")
print("Error: \(response.result.error)")
return
}
// here you will get array of itemupc
let itemUPC = json?["item_upc"] as? [[String: Any]] ?? []
// then you can get values as per need can take all values
//inside array or whatever you needed can get with index number
let item = itemUPC?[0]["ITEM"] as? Int ?? 0
let upc = itemUPC?[0]["UPC"] as? Int ?? 0
print("\(item) with upc \(upc)")
Output: -
458698 with upc 14721632

Related

parsing json array in label swift 4

This is my JSON array
[
{
"dollar":"15000",
"date":"1397-12-12"
}
]
i want to show dollar and date values in two different label but i have this type mismatch error :
typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))
this is my Struct :
struct Dollar_Date : Codable {
let dollar : String
let date : String
}
and this is my code in view controller :
func dollarLabel() {
let urlString = DOLLAR_DATE_BASE_URL
guard let url = URL(string: urlString) else { return }
// 2
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!.localizedDescription)
}
guard let data = data else { return }
do {
// 3
//Decode data
let JSONData = try JSONDecoder().decode(Dollar_Date.self, from: data)
// 4
print(JSONData.dollar)
//Get back to the main queue
DispatchQueue.main.async {
self.main_Price.text = JSONData.dollar
self.data_Label.text = JSONData.date
}
} catch let jsonError {
print(jsonError)
}
// 5
}.resume()
}
Try this:
guard let jsonArray = JSONData as? [[String: Any]] else {
return
}
print(jsonArray)
//Now get title value
guard let title = jsonArray[0]["title"] as? String else { return } print(title)
You get an error because your data contains an array of JSON (dictionary as well), not JSON itself.
So you can't decode with
let JSONData = try JSONDecoder().decode(Dollar_Date.self, from: data)
But you have to do it manually:
EDIT:
guard let json = data as? [[String:Any]] else{return}
Now you can retrieve dollars and date from all the elements of your array:
// These are because you have to access an element of the array
guard let dollar = json[0]["dollar"] as? String else {return}
guard let date = json[0]["date"] as? String else {return}
DispatchQueue.main.async {
self.main_Price.text = dollar
self.data_Label.text = date
}
if your data can contain multiple value (dollars-date couples) you have to loop your array. By the previous example, you should do:
for element in json{
guard let dollar = element["dollar"] as? String else {return}
guard let date = element["date"] as? String else {return}
// do some stuff with each value
}

Accessing JSON data with Swift

I have an array of JSON data from the following call:
guard let json = (try? JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [Any] else {
print("Not containing JSON")
return
}
when I run print(json) I get the following in the output:
[{
"CREATED_BY" = "DOMAIN\\USER";
"CREATED_DATE" = "2016-11-28T08:43:59";
STATUS = U;
"WIDGET_NUMBER" = K11;
"UPDATED_BY" = "<null>";
"UPDATED_DATE" = "<null>";
}, {
"CREATED_BY" = "DOMAIN\\USER";
"CREATED_DATE" = "2016-05-09T08:46:23";
STATUS = U;
"WIDGET_NUMBER" = 89704;
"UPDATED_BY" = "<null>";
"UPDATED_DATE" = "<null>";
}]
I am trying to get all of the WIDGETNUMBER values in the array of JSON data. The json variable is a Any type and I have not been able to convert to a struct so far. Is there an easy way to get the elements from the JSON objects?
It looks like you have an array of dictionaries
for item in json {
if let item = item as? [String: Any], let widgetNo = item["WIDGET_NUMBER"] {
print(widgetNo)
}
}
Your content is array of Dictionary, so that you must convert each element Dictionary to Json
for dic in content {
do {
let jsonData = try JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted)
print(jsonData)
} catch {
print(error.localizedDescription)
}
}
Or you can read value of WIDGET_NUMBER direct from Dictionary
for dic in content {
print(dic["WIDGET_NUMBER"] ?? "Not found")
}
Joakim's answer is spot on for getting the widget number. For your struct, be sure to add something like this as an initializer to map your object.
let widgetNumber: Int
let user: String
init?(json:[String:Any]) {
guard let widgetNumber = json["WIDGET_NUMBER"] as? Int,
let user = json["CREATED_BY"] as? String else { return nil }
self.widgetNumber = widgetNumber
self.user = user
}
If you just want an array of widget numbers you could use the reduce function which iterates the dictionaries in the array and extracts the widget numbers:
Using your data I put this in a storyboard:
let json = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as! [[String: Any]]
let widgetNumbers = json?.reduce(into: [String]()){ (accum, dict) in
guard let widget = dict["WIDGET_NUMBER"] as? String else { return }
accum.append(widget)
}
widgetNumbers // -> ["K11", "89704"]

Error parsing JSON in swift and loop in array

I have an api which return a JSON and i want to parse this JSON and use it in my application.
I have tried the get method from this: swift JSON login REST with post and get response example
Code:
func makeGetCall() {
// Set up the URL request
let todoEndpoint: String = "my link"
guard let url = URL(string: todoEndpoint) else {
print("Error: cannot create URL")
return
}
let urlRequest = URLRequest(url: url)
// set up the session
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
// make the request
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET on /public/api/services")
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
do {
guard let todo = try JSONSerialization.jsonObject(with: responseData, options: [])
as? [String: Any] else {
print("error trying to convert data to JSON")
return
}
// now we have the todo
// let's just print it to prove we can access it
print("The todo is: " + todo.description)
// the todo object is a dictionary
// so we just access the title using the "title" key
// so check for a title and print it if we have one
guard let todoTitle = todo["name"] as? String else {
print("Could not get todo title from JSON")
return
}
print("The title is: " + todoTitle)
} catch {
print("error trying to convert data to JSON")
return
}
}
task.resume()
}
And i got as an output: error trying to convert data to JSON..
My JSON IS:
[
{
"id": 1,
"name": "Services 1",
"description": "This is a description of Services 1. This is a description of Services 1 This is a description of Services 1. ",
"created_at": null,
"updated_at": null
},
{
"id": 2,
"name": "Services 2",
"description": "This is a description of Services 2. This is a description of Services 2 This is a description of Services 2. ",
"created_at": null,
"updated_at": null
}
]
Why i got error parsing the JSON?
Also, how to loop for the array and print each item?
For example:
service 1 description is: This is a description of Services 1. This is
a description of Services 1 This is a description of Services 1.
service 2 description is: This is a description of Services 2. This is
a description of Services 2 This is a description of Services 2.
Please read the JSON carefully. The root object is clearly an array ([])
guard let todos = try JSONSerialization.jsonObject(with: responseData) as? [[String: Any]] else {
print("error trying to convert data to JSON")
return
}
for todo in todos {
print(todo["name"] as? String ?? "n/a")
}
However I recommend to use the Decodable protocol. Declare this struct outside the class
struct Service : Decodable {
let id : Int
let name, description : String
let createdAt : String?
let updatedAt : String?
}
and decode the JSON this way
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let todos = try decoder.decode([Service].self, from: responseData)
for todo in todos {
print(todo.name)
}
} catch { print(error) }
Side note:
The line guard let responseData = data else { will never reach the else clause. If error is nil – which has been checked already – then it's guaranteed that data has a value.
I think you're making small mistake, you have a list of todo, parsing won't give you the todo itself. It will give you the Array of todo
In Swift4:
//assume that you have the JSON String as Data
guard let data = data else {
return
}
let json = try? JSONSerialization.jsonObject(with: response.data!, options: [])
if let array = json as? [[String: Any]] {
for todo in array {
// parse todo component
if let name = todo["name"] as? String {
print("Name : \(name)")
}
// other properties parse in the same way
}
}

filtering JSON data in swift

I am trying to parse my JSON data and append only those objects into array which meets specified condition. At the moment I have commented out code which fetch all objects from the API and add them into array. However, I would like to limit it so that it only appends objects with "wger.de" value for "license_author" key.
However I am getting error on line:
if eachExercise["license_author"] == "wger.de"
Binary operator '==' cannot be applied to operands of type 'Any?' and 'String'.
However I still wants to keep it as Any object because I would like to fetch both strings and integers data from my API.
This is the code for my parseData() function:
func parseData() {
fetchedExercise = []
let urlPath = "https://wger.de/api/v2/exercise/?format=json&language=2&status=2"
let url = URL(string: urlPath)!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print("Error while parsing JSON")
}
else {
do {
if let data = data,
let fetchedData = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? [String:Any],
let exercises = fetchedData["results"] as? [[String: Any]] {
// WORKING CODE
/*
for eachExercise in exercises
{
let name = eachExercise["name"] as! String
let description = eachExercise["description"] as! String
self.fetchedExercise.append(Exercise(name: name, description: description))
}
*/
// TESTING
for eachExercise in exercises {
if eachExercise["license_author"] == "wger.de" {
let name = eachExercise["name"] as! String
let description = eachExercise["description"] as! String
let id = eachExercise["id"] as! Int
self.fetchedExercise.append(Exercise(name: name, description: description))
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
catch {
print("Error while parsing data.")
}
}
}
task.resume()
}
Use the where clause and optional downcast Any to String
for eachExercise in exercises where eachExercise["license_author"] as? String == "wger.de" { ...
You need cast it to String.
if eachExercise["license_author"] as? String == "wger.de" {
}

Parse Alamofire json response

I am trying to parse the response from Alamofire but I can't figure out how to do it.
This is the JSON Response I get (I want to parse out "result") how is this done?
JSON: {
result = 887957;
status = 0;
}
Swift 3
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
You just need to specify the type of response is Dictionary and then use subscript with dictionary to get value of result.
if let dictionary = response.result.value as? [String: Int] {
let result = dictionary["result"] ?? 0
print(result)
}
if let JSON = response.result.value as? [String : Any] {
let result = JSON["result"] as? Int
let status = JSON["status"] as? Int
print("Result \(result) Status \(status)")
}
As per latest Almofire Lib and Swift 3.0 with proper validation:
case .success(_):
if ((response.result.value) != nil) {
var responseData = JSON(response.result.value!)
//Userdefaults helps to store session data locally just like sharedpreference in android
if (response.response ? .statusCode == 200) {
let result: Int = responseData["result"].int!
let status: Int = responseData["status"].int!
}
}
case .failure(_):
print(response.result)
}