how to get the particular value from Json? - ios

I am having the Json data as shown below in this I need to get the data that which key value pair for default is 1 then i need to get the remaining dictionaries data and need to be passed to the user to display can any one tell me how to implement this ?
And my code is as shown below
if let addressArray = jsonObj!.value(forKey: "address") as? NSArray{
for array in addressArray {
if let addressDict = array as? NSDictionary{
if let Default = addressDict.value(forKey: "default"){
}
}
}
}
"address": [
{
"default": 0,
"number": 9123456711,
"name": "Ramesh",
"address": "No:11/111 ,cross street,Nungambakkam,mylapore,chennai :600088"
},
{
"default": 1,
"number": 8123456722,
"name": "Vignesh",
"address": "No:22/222 ,cross street,Perambur,chennai :600012"
},
{
"default": 0,
"number": 7123456733,
"name": "Rajesh",
"address": "No:33/333 ,cross street,Villivakkam,chennai :600045"
}
]

You can check if Default equals 1 and add those values to an array.
var defaultArray = [NSDictionary]()
if let addressArray = jsonObj!.value(forKey: "address") as? NSArray{
for array in addressArray {
if let addressDict = array as? NSDictionary{
if let Default = addressDict.value(forKey: "default"){
if Default == 1 {
defaultArray.append(addressDict)
}
}
}
}
}

Step1: Prepare your model as shown in below code snippet.
class Addresses: NSObject {
var default = 0
var number = 0
var name = String()
var address = String()
init?(dictionary:[String:Any]) {
guard let default = dictionary["default"],
let number = dictionary["number"],
let name = dictionary["name"],
let address = dictionary["address"]
else {
return nil
}
self.default = default
self.number = number
self.name = name
self.address = address
}
}
Step2: Declare an array to store parsed addresses.
var addressesArray = [Addresses]()
Step3:
if let addressArray = jsonObj!.value(forKey: "address") as? NSArray{
for array in addressArray {
if let addressDict = array as? NSDictionary{
if let defaultValue = addressDict.value(forKey: "default") as? Int{
if defaultValue == 1 {
if let address = Addresses(dictionary: addressDict) {
addressesArray.append(address)
}
}
}
}
}
}
Thats it, take care of datatypes, and you can use addressesArray to display data.

Related

Map value in particular key in array of dictionary

Map value the particular key wherever presents in array of dictionary and replace it in same array.
We need to update pan_card key 0 to 1, in occurence of array of dictionary.
let keyToUpdate = "pan_card"
var arrayOfDictionary = [[String:Any]]()
var firstDict = [String:Any]()
firstDict["passport"] = 0
firstDict["ration_card"] = 0
firstDict["pan_card"] = 0
var arrayDict = [String : Any]()
arrayDict["currentObject"] = firstDict
arrayDict["title"] = "Documents list"
var secondDict = [String:Any]()
secondDict["dl"] = 0
secondDict["voter"] = 0
secondDict["pan_card"] = 0
//let dic = secondDict.filter({ $0.value as! NSNumber != 0})
//secondDict = dic
//print(secondDict)
//let dictionary = ["foo": 1, "bar": 2, "baz": 5]
//
//let newDictionary = dictionary.mapValues { value in
// return value - value
//}
//print(dictionary)
//print(newDictionary)
var arrayDict2 = [String : Any]()
arrayDict2["currentObject"] = secondDict
arrayDict2["title"] = "Second Documents list"
arrayOfDictionary.append(arrayDict)
arrayOfDictionary.append(arrayDict2)
//print(arrayOfDictionary)
for (index, dictionary) in arrayOfDictionary.enumerated() {
let dict = dictionary
let newDictionary = (dict["currentObject"] as![String:Any]).mapValues { value in
return 1
}
arrayOfDictionary[index] = newDictionary
}
print(arrayOfDictionary)
This code updating every key in currentObject
and tried this as well, but it adding new key
for (index, dictionary) in arrayOfDictionary.enumerated() {
var dict = dictionary
// let newDictionary = (dict["currentObject"] as![String:Any]).mapValues { value in
// return 1
// }
var newDictionary = [String: Any]()
for (key, value) in dict["currentObject"] as![String:Any] {
dict[keyToUpdate, default: value] = 1
}
arrayOfDictionary[index] = dict
}
print(arrayOfDictionary)
I need output like below
Original value
[["currentObject": ["passport": 0, "pan_card": 0, "ration_card": 0], "title": "Documents list"], ["currentObject": ["pan_card": 0, "dl": 0, "voter": 0], "title": "Second Documents list"]]
after update
[["currentObject": ["passport": 0, "pan_card": 1, "ration_card": 0], "title": "Documents list"], ["currentObject": ["pan_card": 1, "dl": 0, "voter": 0], "title": "Second Documents list"]]
Referred Document Link
We knows manually iterating and updating values, we wanted to done with higher order function.
Using a recursive method that performs the update
func update(key:String, in dict: [String:Any], with value: Any) -> [String:Any] {
var out = [String:Any]()
if let _ = dict[key] {
out = dict
out[key] = value
} else {
dict.forEach {
if let innerDict = $0.value as? [String:Any] {
out[$0.key] = update(key: key, in: innerDict, with: value)
} else {
out[$0.key] = $0.value
}
}
}
return out
}
we can use a simple map call
var original = [["currentObject": ["passport": 0, "pan_card": 0, "ration_card": 0], "title": "Documents list"], ["currentObject": ["pan_card": 0, "dl": 0, "voter": 0], "title": "Second Documents list"]]
let result = original.map{ update(key: "pan_card", in: $0, with: 1)}
The update function was based on this answer

Check Jsonobject for Int

I have a problem, I get a Json. The data comes in a dictionary.
This is a sample json:
Receivedtext: {
"x": "pricef",
"b": "usd",
"ds": [
"tpr",
"avgp",
"mcap",
"ppc7D",
"ppc12h",
"ppc4h",
"ppc24h"
],
"data": [
[
"ADA/USD",
"0.819",
"21.23B",
"6.09",
"-5.45",
"-5.36",
"-10"
],
[
"AVT/USD",
"5.968",
"35.81M",
"24.33",
"-4.51",
"-3.3",
"6.65"
],
[
"BAT/USD",
"0.946",
"unknown",
null,
null,
null,
null
], [
"FUN/USD",
"0.000",
"0.00",
0,
0,
0,
0
] ]
}
Normally the json should be all String. I can handle the nill/null but I dont know how to handle if its Int/Double.
If it is a Int/Double I want to replace the value with "unknown".
This is my code so far:
struct JsonMaintableWebsocket {
let tpr: String?
let avgp: String?
let mcap: String?
let ppc7D: String?
let ppc12h: String?
let ppc4h: String?
let ppc24h: String?
init(json: [String?]) {
self.tpr = json[0]
self.avgp = json[1]
self.mcap = json[2]
self.ppc7D = json[3]
self.ppc12h = json[4]
self.ppc4h = json[5]
self.ppc24h = json[6]
}
static func fetchJsonWebsocketMaintable(json: Data) -> [JsonMaintableWebsocket] {
var jsonWebsocket: [JsonMaintableWebsocket] = []
do {
let jsonData = try JSONSerialization.jsonObject(with: json, options: []) as? [String: Any?]
if let data = jsonData!["data"] as? [[String?]] {
for d in data {
jsonWebsocket.append(JsonMaintableWebsocket(json: d))
}
}
}
catch let error{
print(error.localizedDescription)
}
return jsonWebsocket
}
}
Thanks a lot!
One way you could handle this would be to map over the array and check to see what kind of value is stored, and act accordingly. You might change this part:
if let data = jsonData!["data"] as? [[String?]] {
for d in data {
jsonWebsocket.append(JsonMaintableWebsocket(json: d))
}
}
to this:
if let data = jsonData!["data"] as? [[Any?]] {
for d in data {
let adjustedArray: [String?] = d.map({
//First, check to see if object is nil, and return nil if so
if $0 == nil {
return nil
//Check to see if value is string, and return string
} else if let stringValue = $0 as? String {
return stringValue
//Otherwise return "unknown"
} else {
return "unknown"
}
})
jsonWebsocket.append(JsonMaintableWebsocket(json: adjustedArray))
}
}
It would also be easy to handle Int or Double as Strings as well, by adding a couple more options:
if let data = jsonData!["data"] as? [[Any?]] {
for d in data {
let adjustedArray: [String?] = d.map({
if $0 == nil {
return nil
} else if let stringValue = $0 as? String {
return stringValue
} else if let intValue = $0 as? Int {
return "\(intValue)"
} else if let doubleValue = $0 as? Double {
return "\(doubleValue)"
} else {
return "unknown"
}
})
jsonWebsocket.append(JsonMaintableWebsocket(json: adjustedArray))
}
}

Wrong value in correct json using Swift

I'm facing this strange problem. I have to parse this json and extract "symbol","High","low" and "direction". this is the original json
[{"ID":101,"Symbol":"PKR","Bid":105.7,"Ask":106,"High":105.7,"Low":106,"Change":0,"Direction":"0","CreateDate":"04:38:26","EntityState":2,
"EntityKey":{"EntitySetName":"v_openmarketrates","EntityContainerName":"tradebizEntities",
"EntityKeyValues":[{"Key":"ID","Value":101}],
"IsTemporary":false}},
{"ID":1,"Symbol":"EUR","Bid":126.696,"Ask":127.327,"High":126.7622,"Low":126.9752,"Change":0.4192,"Direction":"0","CreateDate":"06:37:31","EntityState":2,
"EntityKey":{"EntitySetName":"v_openmarketrates","EntityContainerName":"tradebizEntities","EntityKeyValues":[{"Key":"ID","Value":1}],
"IsTemporary":false}}]
When i'm parsing this in json, it is fetching all the values correctly except the value of "Direction",like this:
[{
Ask = 106;
Bid = "105.7";
Change = 0;
CreateDate = "04:38:26";
Direction = 0;
EntityKey = {
EntityContainerName = tradebizEntities;
EntityKeyValues = (
{
Key = ID;
Value = 101;
}
);
EntitySetName = "v_openmarketrates";
IsTemporary = 0;
};
EntityState = 2;
High = "105.7";
ID = 101;
Low = 106;
Symbol = PKR;
},
{
Ask = "127.265";
Bid = "126.623";
Change = "0.3463";
CreateDate = "06:30:46";
Direction = 0;
EntityKey = {
EntityContainerName = tradebizEntities;
EntityKeyValues = (
{
Key = ID;
Value = 1;
}
);
EntitySetName = "v_openmarketrates";
IsTemporary = 0;
};
EntityState = 2;
High = "126.7306";
ID = 1;
Low = "126.9752";
Symbol = EUR;
}
i have no idea how the value inside json is getting changed while parsing even though rest of the values are correct. this is how i'm parsing it.
if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: []) as? [NSDictionary] {
print("kerb rates full json = ",jsonResult )
for field in jsonResult as? [AnyObject] ?? [] {
print("fields of kerb rates = \(field)")
print("kerb directions \((field["Direction"] as? String)!)")
let subfield : AnyObject = (field["EntityKey"] as? AnyObject)!
let sub_subfield : AnyObject = (subfield["EntityKeyValues"] as? AnyObject)!
print("sub_subfield = \(sub_subfield)")
print("subfields = \(subfield)")
// for key_Subfield in sub_subfield as? [AnyObject] ?? [] {
print("inside loop!")
// converting int and bool values
let ask = (field["Ask"] as? Int)!
let bid = (field["Bid"] as? Int)!
let change = (field["Change"] as? Int)!
let EntityState = (field["EntityState"] as? Int)!
let High = (field["High"] as? Double)!
let ID = (field["ID"] as? Int)!
let IsTemporary = ""//(subfield["IsTemporary"] as? Bool)!
let Low = (field["Low"] as? Double)!
let Value = ""//(key_Subfield["Value"] as? Int)!
// it is crashing here due to multple dictionaries
self.Save_KerbRates(ask: (String(ask)),
bid: (String(bid)),
change: (String(change)),
createDate: (field["CreateDate"] as? String)!,
direction: (field["Direction"] as? String)!,
entityContainerName: "",//(subfield["EntityContainerName"] as? String)!,
entitiySetName:"",// (subfield["EntitySetName"] as? String)!,
entitiyState: (String(EntityState)),
high: (String(High)),
id: (String(ID)),
isTemporary: (String(IsTemporary)),
key:"",// (key_Subfield["Key"] as? String)!,
low: (String(Low)),
symbol: (field["Symbol"] as? String)!,
value: (String(Value)))
// }
}
Update:
After using [[String:Any]]
i'm still getting the wrong value of direction,like this
kerb rates full json = [["Low": 106, "Direction": 0, "EntityState": 2, "EntityKey": {
EntityContainerName = tradebizEntities;
EntityKeyValues = (
{
Key = ID;
Value = 101;
}
);
EntitySetName = "v_openmarketrates";
IsTemporary = 0;
}, "ID": 101, "CreateDate": 04:38:26, "Symbol": PKR, "Change": 0, "Ask": 106, "High": 105.7, "Bid": 105.7], ["Low": 126.9752, "Direction": -1, "EntityState": 2, "EntityKey": {
EntityContainerName = tradebizEntities;
EntityKeyValues = (
{
Key = ID;
Value = 1;
}
);
EntitySetName = "v_openmarketrates";
IsTemporary = 0;
}, "ID": 1, "CreateDate": 07:03:46, "Symbol": EUR, "Change": 0.4403, "Ask": 127.349, "High": 126.7654, "Bid": 126.717],
This parses (and prints) all values in the first level of the dictionary and the value for keys IsTemporary and Value in the second level. All variable names start with a lowercase letter to conform to the naming convention.
The code uses a type alias for convenience.
Be aware that the code does not use any error handling to check the dictionary keys. If it is not guaranteed that all items contain all keys and values you have to use optional bindings.
typealias JSONDictionary = [String:Any]
do {
if let jsonResult = try JSONSerialization.jsonObject(with:data!) as? [JSONDictionary] {
for field in jsonResult {
let ask = field["Ask"] as! Double
let bid = field["Bid"] as! Double
let change = field["Change"] as! Double
let createDate = field["CreateDate"] as! String
let direction = field["Direction"] as! String
let entityState = field["EntityState"] as! Int
let high = field["High"] as! Double
let id = field["ID"] as! Int
let low = field["Low"] as! Double
let symbol = field["Symbol"] as! String
let entityKey = field["EntityKey"] as! JSONDictionary
let isTemporary = entityKey["IsTemporary"] as! Bool
let entityKeyValues = entityKey["EntityKeyValues"] as! [JSONDictionary]
let value = entityKeyValues[0]["Value"] as! Int
print(ask, bid, change, createDate, direction, entityState, high, id, low, symbol, isTemporary, value)
}
}
} catch {
print(error)
}

Why can I not access the second level of this array in Swift?

I have the following array which is passed from an API call from a PHP Script:
["playerForm": {
1 = (
{
date = "2017-01-31";
name = Dicky;
result = L;
"results_id" = 42;
},
{
date = "2016-12-24";
name = Dicky;
result = W;
"results_id" = 19;
}
);
2 = (
{
date = "2017-01-25";
name = G;
result = W;
"results_id" = 38;
},
{
date = "2017-01-25";
name = G;
result = D;
"results_id" = 40;
},
{
date = "2017-01-21";
name = G;
result = L;
"results_id" = 34;
}
);
3 = (
{
date = "2017-01-31";
name = Sultan;
result = W;
"results_id" = 42;
},
{
date = "2017-01-15";
name = Sultan;
result = L;
"results_id" = 30;
}
);
}]
However I cannot seem to access the 1,2,3 parts of it...
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
print (json!)
if let dict = json?["playerForm"] as? [String:AnyObject] {
print ("step 1")
if let arr = dict["1"] as? [[String:String]] {
print ("step 2")
self.leagueForm = arr.flatMap { Form($0) }
print (self.leagueForm)
print (self.leagueForm.count)
for i in 0..<self.leagueForm.count {
let form = self.leagueForm[i]
print (form.player_results!)
self.formGuide.append(form.player_results!)
}
print ("now")
print (self.formGuide)
self.resultsDisplay.results = self.formGuide
self.resultsDisplay.results = self.resultsDisplay.results.reversed()
self.resultsDisplay.displayResults()
}
}
With this code above it only gets as far as Step 1.
What am I doing wrong?
UPDATED WITH PROGRSS*
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
print (json!)
if let dict = json?["playerForm"] as? [String:AnyObject] {
print ("step 1")
for (_ , value) in dict {
if let arr = value as? [[String:Any]] {
print(arr)
//your code
}
}
}
Custom Struct to define array:
struct Form {
var player_result: String?
var player_name: String?
var result_date: String?
var result_id: String?
init(_ dictionary: [String : Any]) {
self.player_result = dictionary["result"] as? String ?? ""
self.player_name = dictionary["name"] as? String ?? ""
result_date = dictionary["date"] as? String ?? ""
result_id = String(dictionary["results_id"] as? Int ?? 0)
}
}
Change your array type [[String:String]] to [[String:Any]] because it contains both String and Number as value.
if let arr = dict["1"] as? [[String:Any]] {
print(arr)
//your code
}
Note: You need to loop through the dict Dictionary because its each key having array.
for (_ , value) in dict {
if let arr = value as? [[String:Any]] {
print(arr)
//your code
}
}

NIL when Parsing JSON

I am trying to parse and get the values from this structure of JSON:
["mst_customer": 1, "data": {
0 = 2;
1 = 1;
2 = 1;
3 = "JAYSON TAMAYO";
4 = "581-113-113";
5 = 56;
6 = on;
7 = g;
8 = jayson;
9 = active;
"app_access" = on;
id = 2;
"mst_customer" = 1;
name = "Jayson Tamayo";
status = active;
territory = 1;
}, "status": OK, "staff_id": 2, "staff_name": Jayson Tamayo]
I use the following Swift code to get the values:
(data: Dictionary<String, AnyObject>, error: String?) -> Void in
if error != nil {
print(error)
} else {
if let feed = data["data"] as? NSDictionary ,let entries = data["data"] as? NSArray{
for elem: AnyObject in entries{
if let staff_name = elem["name"] as? String{
print(staff_name)
}
}
}
}
I am trying to get the name by using the keys name or staff_name. But I always get nil.
you want to access staff_name, which is not in "data" variable ... you can simply get that like
if error != nil {
print(error)
} else {
if let name = data["staff_name"] as? String{
print(name)
}
}
for elem: AnyObject in entries{
if let songName = elem["name"] as? String{
print(songName)
}
}
//replace above code with below code
if let songName : String = entries["name"] as? String{
print(songName)
}

Resources