SWIFT Json check if object title exists - json

My code checks if code: "this data" is not empty, how can I check also if code itself exists. Some responses might give me almost an empty JSON with just time stamp. So the var code won't be there.
Or is there a better way altogether to do this? as My JSON is
"Variable Based on text input" which leads to "code" which might not be there which will have "some info" or ""
Top of script:
struct Variables: Decodable {
var code: String
}
typealias DecoderX = [String: Variables]
Previous function sets inputs from user text which are cross checked with the database so GetInfo will only be called if UserDefault inputs are set.
func GetInfo() {
let Input1 = UserDefaults.standard.string(forKey: “UserInput1”) ?? “”
let Input2 = UserDefaults.standard.string(forKey: “UserInput2”) ?? “”
let Input3 = UserDefaults.standard.string(forKey: “UserInput3”) ?? “”
print(“Input Check 1: \(Input1) \(Input2) \(Input3)”)
// URL INFO With API key hidden
let jsonTask = URLSession.shared.dataTask(with: requestURL) { data, response, error in
if response == response {
if let data = data, let body = String(data: data, encoding: .utf8) {
do {
let json = try? decoder.decode(DeocderX.self, from: data);
DispatchQueue.main.async {
print(“Input Check 2: \(json![Input1]!.code) \(json![Input2]!.code) \(json![Input3]!.code)”)
if json?[Input1]?.code != nil {
print("Good Information 1")
} else {
print("Found Nothing 1")
}
if json?[Input2]?.code != nil {
print("Good Information 2")
} else {
print("Found Nothing 2")
}
if json?[Input3]?.code != nil {
print("Good Information 3")
} else {
print("Found Nothing 3")
}
}
// rest of code not applicable

You can use SwiftyJSON library. You can find it at this link
if let json = try? JSON(data: response.data!){
if json["yourKey"]["otherKey"].exists() {
print("exists")
}
}
Hope it helps...

Related

SwiftUI: Waiting for JSON to be decoded before

I'm having an issue with displaying a deserialised JSON object in a view. The problem seems to be that my view is trying to unwrap a value from a published variable before anything is assigned to it by the function that gets the JSON object.
Here is the code that calls the api
class ViewModel : ObservableObject {
#Published var posts : first?
init(subReddit : String){
fetch(sub: subReddit)
}
func fetch(sub : String) {
guard let url = URL(string: "https://www.reddit.com/r/" + sub + "/top.json?t=day") else {
return
}
let task = URLSession.shared.dataTask(with: url) { [weak self] data, _, error in
guard let data = data, error == nil else {return}
do{
let retVal = try JSONDecoder().decode(first.self, from:data)
DispatchQueue.main.async {
self?.posts = retVal
}
}
catch{
print(error)
}
}
task.resume()
}
}
and here is the code for my view:
struct SubRedditView: View {
#StateObject var viewModel = ViewModel(subReddit: "all")
var body: some View {
NavigationView{
List{
ForEach((viewModel.posts?.data.children)!) {post in//at runtime I get a nil unwrap error here
Text(post.data.title)
Text(post.data.url_overridden_by_dest ?? "No Value")
}
}
.navigationTitle("Posts")
}
}
}
If only the object representing the children is relevant declare the published object as empty array of this type
#Published var posts = [TypeOfChildren]()
Then assign the children to the array
self?.posts = retVal.data.children
This makes the code in the view easier and safe.
ForEach(viewModel.posts) { post in
Text(post.title)
Text(post.url_overridden_by_dest ?? "No Value")

Swift 3 - How can i extract elements from a json?

Hello i want to extract certain elements from a json .
CODE: .
var datas:[Usuario]?
struct Usuario : Codable {
let correo: String?
let contrasena: String?
}
let urlString = "http://localhost:8080/swiftdb/logear.php"
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!.localizedDescription)
}
guard let data = data else { return }
//Implement JSON decoding and parsing
do {
//Decode retrived data with JSONDecoder and assing type of Article object
let articlesData = try JSONDecoder().decode([Usuario].self, from: data)
//Get back to the main queue
DispatchQueue.main.async {
}
} catch let jsonError {
print(jsonError)
}
}.resume()
What i want to do, is compare what it is in the json with a textfield ,so it can go to a different view.
Json Contains:
[{"correo":"unknown11#hotmail.com","contrasena":"12345"},{"correo":"lalo123#hotmail.com","contrasena":"12121"},{"correo":"kokunz#hotmail.com","contrasena":"11111"},{"correo":"a","contrasena":"a"}]
Textfield Contains: {Something which the user writes in in. txtEmail.text Ex.}
Before the answer, you can check the Apple Reference about JSON and parsing . https://developer.apple.com/swift/blog/?id=37
if let dictionary = netoxJsonVariable as? [String: Any] {
for (key, value) in dictionary {
// access all key / value pairs in dictionary
// I am not sure that you can reach key with string but I think you are searching for this.
//if(value == "searchedMailAddress"){//then}
//if(key == "correoLikeKey"){//then}
}
}
I used a for statement so
let {newVar} = self.{yourJson}
for item in {newVar}! {
let {anotherVar} = item.{field}
let {anotherVar2} = item.{field2}

Swift JSON Parsing with a filter in url

I am trying to parse JSON, everything works fine, but when I try to give a filter with the url, it says found nil while unwrapping an optional value. I was wondering if I should give the filters in some other way. PS.. The url works fine when I use it in the browser
this is how the url with filter looks like:
https://start.jamespro.nl/v4/api/json/tasks/?filter=[{"column":"Date","operator":"=","value":"2017-08-04"}, {"column":"UserId","operator":"=","value":"2"}]
and this is my whole code:
func apiRequestTasks(url: String) {
apiRequestHeader(userName: "*******", passWord: "******")
var running = false
let urlProjects = NSURL(string: url)
let task = session?.dataTask(with: urlProjects! as URL) {
( data, response, error) in
if let taskHeader = response as? HTTPURLResponse {
print(taskHeader.statusCode)
}
if error != nil {
print("There is an error!!!")
print(error ?? "")
} else {
if let content = data {
do {
let dictionary = try JSONSerialization.jsonObject(with: content) as! [String:Any]
print(dictionary)
}
catch {
print("Error: Could not get any data")
}
}
}
running = false
}
running = true
task?.resume()
while running {
print("waiting...")
sleep(1)
}
}
I think the problem is the way you create your URL, try something like this:
let filters = "[\"column\":\"Date\",\"operator\":\"=\",\"value\":\"2017-08-04\"}, {\"column\":\"UserId\",\"operator\":\"=\",\"value\":\"2\"}]"
if var url = URLComponents(string: "https://start.jamespro.nl/v4/api/json/tasks") {
url.query = "filter=:\(filters)"
print ("url", url.string ? "invalid url")
}
I just encoded the filter part of my url and it worked. But thanx for the reactions !!!
let filter = "[{\"column\":\"Date\",\"operator\":\"=\",\"value\":\"2017-08-04\"}, {\"column\":\"UserId\",\"operator\":\"=\",\"value\":\"2\"}]"
let encodedFilter = filter.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
let baseUrl = "https://start.jamespro.nl/v4/api/json/tasks/?filter="
let url = baseUrl + encodedFilter!
apiRequestTasks(url: url)

NSUserdefault to same data from JSON

Good morning, I have this block of code that I am using to get exchange rates from an API JSON file. I use this function to convert a result to different currencies. I am trying to add functionality to store the data so, when offline, it still shows a result with the last downloaded JSON file data.
I was thinking maybe use NSUserdefault? but not too sure where...
I am already using this to check if online or not.
if Reachability.isConnectedToNetwork(){
// connected do something}
else{
// do something else }
Thank you for any pointer and any kind of help that could send me in the right direction.
func exchangeRatefunc (mycurrency: String, mapaye: Double) {
let apiEndPoint = "https://api.fixer.io/latest?base=USD"
guard let url = NSURL(string: apiEndPoint) else {
self.infoLabel.text = NSLocalizedString("Error getting exchange rates.", comment: "")
print("Url is not valid")
return
}
let urlRequest = NSURLRequest(url: url as URL)
let session = URLSession.shared
let task = session.dataTask(with: urlRequest as URLRequest) { (data, response, error) in
guard error == nil else {
print(error?.localizedDescription ?? 0)
return
}
guard let data = data else {
print("No data received")
self.infoLabel.text = NSLocalizedString("You appear to be offline!", comment: "")
return
}
do {
guard let exchangeDict = try JSONSerialization.jsonObject(with: data, options: []) as? [String: AnyObject] else {
print("Could not convert JSON to dictionary")
self.infoLabel.text = NSLocalizedString("No conversion Available.", comment: "")
return
}
print(exchangeDict.values)
if let rates = exchangeDict["rates"] {
let currencies = [mycurrency]
for currency in currencies {
if let rate = rates[currency] as? Double {
self.exchangeRates.append("1 USD = \(rate) " + currency)
let myprefcurdisplay = mapaye * rate
OperationQueue.main.addOperation({
self.myPreferedCurrencyPayLabel.updateWithText("\(myprefcurdisplay.roundTo(places: 2)) \(currency)")
if let date = exchangeDict["date"] as? String {
self.infoLabel.text = "\(currency)/USD" + NSLocalizedString(" rates updated on" + " \(date)", comment: "")
}
})
}
}
}
}
catch {
print("Error trying to convert JSON to dictionary")
}
}
task.resume()
}

Casting AnyObject to Double

I am trying to parse JSON with the following code:
func ltchandler(response: NSURLResponse!, data : NSData!, error : NSError!) { //Is passed the results of a NSURLRequest
if ((error) != nil) {
//Error Handling Stuff
} else {
if (NSString(data:data, encoding:NSUTF8StringEncoding) == "") {
//Error Handling Stuff
} else {
var data = NSData(data: data);
// Define JSON string
var JSONString = "\(data)"
// Get NSData using string
if let JSONData = JSONString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
// Parse JSONData into JSON object
var parsingError: NSError?
if let JSONObject = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &parsingError) as? [String: AnyObject] {
// If the parsing was successful grab the rate object
var rateObject: Double! = JSONObject["price"]?.doubleValue
// Make sure the rate object is the expected type
if let rate = rateObject as? Double! { // THIS IS NOT WORKING!!!
//Do stuff with data
} else {
println("Parsing Issue")
}
}
}
}
}
}
The line marked THIS IS NOT WORKING!!! is not being called.
From what I can tell, it cannot cast rateObject as a double - why not? It is not showing any errors.
To clarify, the expected behavior is that a double is created from the JSON object.
To strictly answer your question have you tried printing the rateObject. Also why are you casting to Double! rather than just Double in the problematic line?
Personally I don't use ! in almost all cases. You are better off using either non-optionals or proper optionals.
In the relevent section I would write:
// Make sure the rate object is the expected type
if let rate = JSONObject["price"]?.doubleValue {
//Do stuff with rate
} else {
print("Parsing Issue")
}
Of course if the JSONObject["price"] is not something with a doubleValue method or the method returns nil you will end up with nil and the else case being taken.
the code worked for me, try this code:
// if the value equals nil or any String, the instruction abort the if
// SWIFT 2.0 in xcode beta 5
if let rate = Double((JSONObject["price"] as? String)!){
// insert you code here
} else {
print("error message")
}