Parse JSON with no title Swift 3 - json

I am pulling down a json stream? From a phant server I can pull the data down parse it and print it in xcode. I need to pull out specific values but the json does not have a title and I can not seem to figure it out.
My JSON Data
(
{
lat = "36.123450";
long = "-97.123459";
timestamp = "2017-04-26T05:55:15.106Z";
},
My Current Code in Swift
let url = URL(string: "https://data.sparkfun.com/output/5JDdvbVgx6urREAVgKOM.json")
let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
if error != nil {
print("error")
} else {
if let content = data {
do {
// JSONArray
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
print(myJson)
let Coordinates = myJson["lat"] as! [[String:Any]]
print(Coordinates)
} catch {
}
}
}
}
task.resume()
}

Please read the JSON. [] represents an array, {} a dictionary.
The JSON is an array of dictionaries. All keys and values are String.
let url = URL(string: "https://data.sparkfun.com/output/5JDdvbVgx6urREAVgKOM.json")
let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
if error != nil {
print("error: ", error!)
} else {
do {
let coordinateArray = try JSONSerialization.jsonObject(with: data!) as! [[String:String]]
for coodinate in coordinateArray {
let lat = coodinate["lat"] ?? "n/a"
let long = coodinate["long"] ?? "n/a"
let timestamp = coodinate["timestamp"] ?? "n/a"
print("latitude: \(lat), longitude: \(long), timestamp: \(timestamp)")
}
} catch {
print (error)
}
}
}
task.resume()
As always, .mutableContainers has no effect in Swift but the tutorials which suggests that will never die off.

It looks like your JSON data is an array of objects, so myJson will be an array of dictionaries.
I would try something like this:
if let myJson = try JSONSerialization.jsonObject(with: content, options: []) as? [AnyObject] {
print(myJson)
for obj in myJson {
if let dict = obj as? [String:AnyObject] {
print(dict)
if let lat = dict["lat"] as? String,
let lng = dict["long"] as? String,
let time = dict["timestamp"] as? String {
// do something with stuff
}
}
}
}

Related

Fetching data from multi level JSON

I am having issue fetching products "name" from a json file.
and here is my code so far. and it prints complete json. I just want to fetch name of the products from this file.
guard let url = URL(string: "https://URL/get_products.php") else {return}
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data,
error == nil else {
print(error?.localizedDescription ?? "Response Error")
return }
do{
let jsonResponse = try JSONSerialization.jsonObject(with: dataResponse, options: [])
print(jsonResponse)
} catch let parsingError {
print("Error", parsingError)
}
}
task.resume()
The structure is pretty clear, values next to string keys are dictionaries, next to ordinal numbers are arrays:
if let jsonResponse = try JSONSerialization.jsonObject(with: dataResponse) as? [String:Any],
let data = jsonResponse["data"] as? [[String:Any]] {
for anItem in data {
if let products = anItem["products"] as? [[String:Any]] {
for product in products {
if let name = product["name"] as? String {
print(name)
}
}
}
}
}
A better way is to parse the JSON into structs with JSONDecoder

Unable to parse JSON from link - no errors, but the function returns after trying to access the URL

I am new to Swift and trying basic JSON parsing by following tutorials. I want to print a field of a JSON file, but it is not working.
Although the link exists, and I am using the same link I used for a previous tutorial, it returns rather than moved on to accessing the JSON.
I understand there is an "easier" way to do it in Swift4 using Decoder, but I received an error when I did it that way.
Here is the structure I am using:
struct Tester {
var userId: Int
var id: Int
var title: String
var body: String
init(json: [String: Any]){
userId = json["userId"] as? Int ?? -10
id = json["id"] as? Int ?? -400
title = json["title"] as? String ?? ""
body = json["body"] as? String ?? ""
}
}
And here is the code that is trying to access the JSON entries
#IBAction func printIDTitle(_ sender: Any) {
guard let url = URL(string: "https://jsonplaceholder.typicode.com/posts") else { return }
let session = URLSession.shared
session.dataTask(with: url) { (data, response, error) in
if let response = response {
print(response)
}
guard let data = data else { return }
do {
print("here 0\n")
guard let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] else {
print(error)
return
}
print("here 0.5\n")
print("here 1\n")
let d = Tester(json: json)
print(d.id)
print(d.title)
print("here 2\n")
} catch let error {
print(error)
}
}.resume()
}
The "here 0" is the only print that shows up.
What could be my issue?
The root is an array so change
guard let json = try JSONSerialization.jsonObject(with: data, options:[]) as? [[String: Any]] else {
print(error)
return
}
Or better
let res = try! JSONDecoder().decode([Root].self, from:data)
struct Root: Codable {
let userId, id: Int
let title, body: String
}

Trouble getting data from TheMovieDB API with Swift JSON

I am brand new to using JSON and wanted to get started with a simple app to provide a movie overview when you type in a title. My below code returns everything in one big string. How do I get just one piece of information like the overview or year?
With my below attempt, print(obj["overview"] as Any)) prints "nil" and print(obj) looks like this:
{
page = 1;
results = (
{
adult = 0;
"backdrop_path" = "/A0aGxrCGRBuCrDltGYiKGeAUect.jpg";
"genre_ids" = (
53,
80
);
id = 680;
"original_language" = en;
"original_title" = "Pulp Fiction";
overview = "A burger-loving hit man, his philosophical partner, a drug-addled gangster's moll and a washed-up boxer converge in this sprawling, comedic crime caper. Their adventures unfurl in three stories that ingeniously trip back and forth in time.";
Current Code:
let query = "Pulp+Fiction"
let urlString = "https://api.themoviedb.org/3/search/movie?api_key={MYAPIKEY}&query=\(query)"
let url = URL(string: urlString)
URLSession.shared.dataTask(with:url!) { (data, response, error) in
if error != nil {
print(error as Any)
} else {
do {
let parsedData = try JSONSerialization.jsonObject(with: data!) as Any
if let obj = parsedData as? NSDictionary {
print(obj["overview"] as Any)
print(obj)
}
} catch {
print("error")
} }
}.resume()
}
// write this extension anywhere in your any swift file
extension String{
func toDictionary() -> NSDictionary {
let blankDict : NSDictionary = [:]
if let data = self.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary
} catch {
print(error.localizedDescription)
}
}
return blankDict
}
}
//now in your code modify as
if data != nil {
let responseString = String(data: data!, encoding: .utf8)!
if(responseString != "")
{
//convert response string into dictionary using extended method
let responseValues = responseString.toDictionary()
//access value using keyPath using
let value = responseValues.value(forKeyPath: "key.key2")
//where key2 is the target key which is inside the value of key
}
}
First of all JSON results are never Any. As already mentioned in the comments the root object is a dictionary
if let parsedData = try JSONSerialization.jsonObject(with: data!) as? [String:Any],
The key overview is in array for key results
let results = parsedData["results"] as? [[String:Any]] {
You have to iterate over the array to get the values for key overview
for result in results {
print(result["overview"] as? String ?? "no value for key overview")
}
}
It's highly recommended to use the Codable protocol and custom structs in Swift 4.

Not getting expected results parsing JSON in Swift

I have a web service that look like this
[
{
"id_test" : "1",
"title":"test1"
},
{
"id_test" : "2",
"title":"test2"
},{
"id_test" : "3",
"title":"test3"
},
]
I use this code to read the json in my program:
func downloadJsonWithUrl()
{
let url = URL(string:"weserviceurl")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil
{
print("Error")
}
else
{
if let content = data
{
do
{
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
print(myJson)
}
catch
{
}
}
}
}
task.resume()
}
My question is, at the moment to use use this code below my serialization:
if let info = myJson as? NSArray
{
if let categories = info["title"]
{
print(categories)
}
}
does not work. It returns empty. Why?
Try this
func downloadJsonWithUrl() {
let url = URL(string:"link")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print("Error")
} else {
if let content = data {
do {
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as! [AnyObject]
var categories: [String] = []
for item in myJson {
categories.append(item["title"] as! String)
}
print(categories)
} catch {
print(error)
}
}
}
}
task.resume()
}
You should use swift Array and Dictionary types. For example, ... as? [[String: Any]] says that you're expecting an array of dictionaries:
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
guard let data = data, error == nil else {
print("\(error)")
return
}
guard let json = try? JSONSerialization.jsonObject(with: data), let array = json as? [[String: Any]] else {
print("not JSON array of dictionaries")
return
}
for dictionary in array {
if let idTest = dictionary["id_test"], let title = dictionary["title"] {
print("\(idTest): \(title)")
}
}
}

Grab data from JSON file doesn't work

I try to grab data from JSON (http://www.openligadb.de/api/getmatchdata/bl1/2014/15). I want to get every single game with the goals, location, team ...
I tried this but it won't work.
let url = "http://www.openligadb.de/api/getmatchdata/bl1/2014/15"
//parse url
if let JSONData = NSData(contentsOfURL: NSURL(string: url)!) {
if let json = (try? NSJSONSerialization.JSONObjectWithData(JSONData, options: [])) as? NSDictionary {
//handle json
}
}
It doesn't steps in the 2nd if-statement (if let json = (try?...).
I hope you could help me.
Edit get data of dictionaries:
//Data Team1
if let team1 = object["Team1"] as? NSDictionary {
if let name = team1["TeamName"] as? String {
print("Name Team1: \(name)")
}
if let logo = team1["TeamIconUrl"] as? String {
print("Logo Team1: \(logo)")
}
// Etc.
}
What you need to do is to understand your JSON structure: you have an array first, not a dictionary.
This array has dictionaries, each of them holding an array of dictionaries.
It may sound complex but it's actually simple, you just follow the structure of your JSON and decode the values with the correct type.
In JSON, an array starts with [ and a dictionary starts with { (also, be careful not to confuse this JSON syntax with Swift's arrays and dictionaries one).
Your code could be something like this, for example:
do {
let url = "http://www.openligadb.de/api/getmatchdata/bl1/2014/15"
if let url = NSURL(string: url),
JSONData = NSData(contentsOfURL: url),
jsonArray = try NSJSONSerialization.JSONObjectWithData(JSONData, options: []) as? NSArray {
for object in jsonArray {
if let goalsArray = object["Goals"] as? NSArray {
// Each "goal" is a dictionary
for goal in goalsArray {
print(goal)
if let name = goal["GoalGetterName"] as? String {
print("Name: \(name)")
}
if let ID = goal["GoalID"] as? Int {
print("ID: \(ID)")
}
// Etc.
}
}
}
}
} catch {
print(error)
}
UPDATE: you're almost there! But "Team1" is a dictionary, not an array. :)
Here's the solution:
do {
let url = "http://www.openligadb.de/api/getmatchdata/bl1/2014/15"
if let url = NSURL(string: url),
JSONData = NSData(contentsOfURL: url),
jsonArray = try NSJSONSerialization.JSONObjectWithData(JSONData, options: []) as? NSArray {
for object in jsonArray {
if let team1 = object["Team1"] as? NSDictionary {
if let name = team1["TeamName"] as? String {
print("Name Team1: \(name)")
}
if let logo = team1["TeamIconUrl"] as? String {
print("Logo Team1: \(logo)")
}
}
}
}
} catch {
print(error)
}