Parsing JSON with Swift (NSArray) - json

I'm trying to parse a JSON file in Xcode with Swift 3. My problem is getting the "values", 14.2. The error I get is "Type Any has no subscript member".
Here is my NSArray (Aka firstIndex):
{
level = 2;
levelType = hl;
name = t;
unit = Cel;
values = (
"14.2"
);
}
Here is my code:
if let parameters = test["parameters"] as? NSArray {
let firstIndex = parameters[0]
//print(firstIndex)
PROBLEM --> if let values = firstIndex["values"] as? Int {
}
}

The value for key values is also an array, represented by the parentheses.
The error occurred because NSArray does not provide type information.
In Swift use always native Array (here [[String:Any]]).
if let parameters = test["parameters"] as? [[String:Any]], let firstIndex = parameters.first {
if let values = firstIndex["values"] as? [String], let firstValue = values.first {
print(firstValue) // or Double(firstValue)!
}
}

Related

Parsing json having object in (); using swift

How to parse below json response in swift. I want to get the value of "code"
jsonResponse : {
messages = (
{
code = "MM_777";
message = "Password wrong";
}
);
}
Done it!
if let messages = fromJsonResponse.first {
let value = messages.value as! Array<AnyObject>
let dict = value.first as? [String: Any]
print(dict!["code"] ?? "lax")
}

Swift - JSON array values

I am getting following JSON values in output:
[["category_title": Shelly], ["category_title": Thaddeus],
["category_title": Chantale], ["category_title": Adara],
["category_title": Mariko], ["category_title": Felicia]]
But I want it like below:
["Shelly","Thaddeus","Chantale", "Adara","Mariko","Felicia"]
I have the following Swift code. Please help me get above output.
func successGetTermsData(response: Any){
var UserRole : String = ""
var arrayOfDetails = response as? [[String: Any]] ?? []
UserRole = arrayOfDetails as? String ?? ""
print(arrayOfDetails)
}
You have to map the array of Dictionary arrayOfDetails to an array of String. flatMap ignores a missing key.
if let arrayOfDetails = response as? [[String: String]] {
let userRole = arrayOfDetails.flatMap { $0["category_title"] }
print(userRole)
}
There are many ways to do this. One way is to use flatmap to get just the values in your array of dictionaries:
let arrayOfValues = arrayOfDetails.flatMap { $0.values }
In order to get this to work, the names need to be inside double quotes: "Shelly", etc.

Get JSON Element in Swift 3

Please excuse me if this is a simple question, but I am stuck. I have tried to read everything I can to work it out myself.
I am trying to extract a URL from JSON data, I get the JSON data fine and I can print it to the console, however I can't work out how to access the URL for the audio file.
This is the code I use to get the JSON:
let session = URLSession.shared
_ = session.dataTask(with: request, completionHandler: { data, response, error in
if let response = response,
let data = data,
let jsonData = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) {
if let dictionary = jsonData as? [String: Any] {
if let prounce = dictionary["pronunciations"] as? [String: Any]{
if let audioPath = prounce["audioFile"] as? String {
print(audioPath)
}
}
}
print(response)
print(jsonData)
} else {
print(error)
print(NSString.init(data: data!, encoding: String.Encoding.utf8.rawValue))
}
}).resume()
The output I get is:
metadata = {
provider = "Oxford University Press";
};
results = (
{
id = maladroit;
language = en;
lexicalEntries = (
{
entries = (
{
etymologies = (
"late 17th century: French"
);
grammaticalFeatures = (
{
text = Positive;
type = Degree;
}
);
senses = (
{
definitions = (
"inefficient or inept; clumsy:"
);
examples = (
{
text = "both men are unhappy about the maladroit way the matter has been handled";
}
);
id = "m_en_gb0494140.001";
}
);
}
);
language = en;
lexicalCategory = Adjective;
pronunciations = (
{
audioFile = "http://audio.oxforddictionaries.com/en/mp3/maladroit_gb_1.mp3";
dialects = (
"British English"
);
phoneticNotation = IPA;
phoneticSpelling = "\U02ccmal\U0259\U02c8dr\U0254\U026at";
}
);
text = maladroit;
}
);
type = headword;
word = maladroit;
}
);
}
I want to get the URL called audioFile in the pronunciations. Any help is much appreciated.
If my guess is right, your output shown above lacks opening brace { at the top of the output.
(I'm also assuming the output is taken from your print(jsonData).)
Your jsonData is a Dictionary containing two values:
A dictionary value for "metadata"
An array value for "results"
So, you cannot retrieve a value for "pronunciations" directly from jsonData (or dictionary).
You may need to:
Retrieve the value for "results" from jsonData, it's an Array
Choose one element from the "results", it's a Dictionary
Retrieve the value for "lexicalEntries" from the result, it's an Array
Choose one element from the "lexicalEntries", it's a Dictionary
Retrieve the value for "pronunciations" from the lexicalEntry, it's an Array
Choose one element from the "pronunciations", it's a Dictionary
Here, you can access the values in each pronunciation Dictionary. In code, you need to do something like this:
if
let dictionary = jsonData as? [String: Any],
let results = dictionary["results"] as? [[String: Any]],
//You need to choose one from "results"
!results.isEmpty, case let result = results[0],
let lexicalEntries = result["lexicalEntries"] as? [[String: Any]],
//You need to choose one from "lexicalEntries"
!lexicalEntries.isEmpty, case let lexicalEntry = lexicalEntries[0],
let pronunciations = lexicalEntry["pronunciations"] as? [[String: Any]],
//You need to choose one from "lexicalEntries"
!pronunciations.isEmpty, case let pronunciation = pronunciations[0]
{
//Here you can use `pronunciation` as a Dictionary containing "audioFile" and some others...
if let audioPath = pronunciation["audioFile"] as? String {
print(audioPath)
}
}
(You can use let result = results.first instead of !results.isEmpty, case let result = results[0], if you always use the first element for arrays. Other two lines starting from !...isEmpty, case let... as well.)
You need to dig into the target element from the outermost element step by step.

Turning json variables into swift variables

My PHP server-side returns a JSON like this:
[{"scan_status":"ok","visitorData":[{"visitorCompany":"xyl","visitorStreet":"street","visitorBranche":"health","visitorEmail":"wesweatyoushop#gmail.com","lastmodified":"2014-12-15 14:18:55"}]}]
Now in Swift I would like to store this data, and for this I am trying to parse the data into Swift variables, however I got stuck.
do {
//check wat we get back
let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves )
let vData = jsonData[0]["visitorData"]
//let vCompany = vData["visitorCompany"]
print("Test vData: \(vData)")
}
This prints
Test vData: Optional(( { visitorStreet = street; visitorPhone = 01606478; visitorCompany = xyl; visitorBranche = Sports; visitorEmail = "health#gmail.com"; lastmodified = "2014-12-15 14:18:55"; } ))
but when I try to get visitorCompany with
let vCompany = vData["visitorCompany"]
I get a compile error:
Cannot subscript a value of type 'AnyObject?!' with an index of type 'String'
BTW, why do we see the equals sign in swift i.e. visitorStreet = street?
This is because the compiler doesn't know the type of your decoded objects.
Help the compiler using casting with if let:
do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves )
if let vData = jsonData[0]["visitorData"] as? [[String:AnyObject]] {
if let vCompany = vData[0]["visitorCompany"] {
print(vCompany)
}
}
}
let vData = jsonData[0]["visitorData"] populates vData with a generic AnyObject?, because Swift can't know what kind of objects PHP returns in the JSON.
You need to do a an optional cast to another dictionary before you can use vData like you want: jsonData[0]["visitorData"] as? [String:AnyObject].
And because a conditional cast returns an optional, it's best you do an optional binding to unwrap that optional, resulting in a code similar to this:
if let vData = jsonData[0]["visitorData"] as? [String:AnyObject] {
//let vCompany = vData["visitorCompany"]
print("Test vData: \(vData)")
}
Or even better, as jsonData can not be an array, or it could be an empty array (the server malfunctions and sends an invalid json for example), you can go even further with the validation:
if let items = jsonData as? [[String:AnyObject]], vData = items.first?["visitorData"] {
//let vCompany = vData["visitorCompany"]
print("Test vData: \(vData)")
}
items = jsonData as? [[String:AnyObject]] fails if jsonData is not an array, while vData = items.first?["visitorData"] fails if items.first is nil (optional chaining here), or if items.first doesn't have a visitorData key.
Try with this:
let vData = jsonData[0]["visitorData"]![0] as! [String:AnyObject]

How to check if a key is present in NSArray populated with json data in SWIFT?

i am trying to parse this json response.
account = (
{
accountName = "Complete Access";
accountNumber = "062005 1709 5888";
available = "226.76";
balance = "246.76";
}
);
var account : NSArray = jsonResult.valueForKey("account") as NSArray
Now, this array contains all value for key “account”. Inside this array how we can check if it contains the key “balance” or not.I tried to check it as below :
if account .containsObject(account(idx).valueForKey(“balance"))
{
transactions.balance = account(idx).valueForKey(“balance”) as? String
}
But, it never got inside the if condition. Please sugget the correct way to achieve this.
something like this may help on you:
let keyAccount: String = "account"
let keyBalance: String = "balance"
var jsonResult : Dictionary<String, AnyObject> = ["account":[["accountName":"Complete Access", "accountNumber":"062005 1709 5888", "available":"226.76", "balance":"246.76"]]]
var account : AnyObject? = jsonResult[keyAccount]
if let accountArray: Array<AnyObject>? = account as? Array {
if accountArray!.count > 0 {
if let accountDictionary: Dictionary<String, String>? = accountArray![0] as? Dictionary {
if let balanceValue: String? = accountDictionary![keyBalance] {
println("\(balanceValue)")
}
}
}
}