I'm trying to convert a JSON string to an array. The JSON string is:
[
{
"field_value" : "28 Aug 2017",
"field_data_type_combo_value" : "",
"field_data_type_category_id" : "1",
"form_id" : "19",
"field_id" : "133",
"message_unique_id" : "7941501245582800298",
"field_data_type_combo_id" : "0",
"field_data_type_id" : "1"
},
{
"field_value" : "",
"field_data_type_combo_value" : "",
"field_data_type_category_id" : "9",
"form_id" : "19",
"field_id" : "134",
"message_unique_id" : "7941501245582714588",
"field_data_type_combo_id" : "0",
"field_data_type_id" : "26"
},
{
"field_value" : "hk",
"field_data_type_combo_value" : "",
"field_data_type_category_id" : "6",
"form_id" : "19",
"field_id" : "135",
"message_unique_id" : "7941501245582699681",
"field_data_type_combo_id" : "0",
"field_data_type_id" : "17"
}
]
and my conversion code is
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String : AnyObject]
} catch {
print(error.localizedDescription)
}
}
The conversion result is always nil. I also checked that JSON string in Online JSON Viewer and the string is correct. Please help me guys.
You explicitely write in your call:
as? [String: AnyObject]
In other words, you ask Swift to take whatever JSON returned, check that it is a dictionary with String keys, and either return that dictionary or nil. Since your data is an array and not a dictionary, it returns nil. Exactly what you asked for, but not what you wanted.
What you are expecting is an array of dictionaries, not a dictionary. You should also use Any instead of AnyObject. So convert it using
as? [[String: Any]]
The outer [] means it is an array, the inner [String: Any] means the items in the array must be dictionaries with String keys and Any values.
And why are you using .mutableContainers? If you have a good reason that you can explain then use it. If you don't and just copied the code from somewhere then remove it.
That json isn't a dictionary on the top level but an array. You can see that from the [ ... ], it would be a dictionary if it was { ... }. Fix your code using the corresponding cast:
return try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [AnyObject]
Related
I 'm having an array of dictionary like so...
[
{
"id" : "3",
"sellingPrice" : "520",
"quantity" : "15"
},
{
"id" : "5",
"sellingPrice" : "499",
"quantity" : "-1"
},
{
"id" : "8",
"sellingPrice" : "500",
"quantity" : "79"
}
]
Now I want to add to the dictionary another key called remaining_balance with a value of 420,499 & 500 respectively. How can I achieve this..? Hope somebody can help...
It seems like you want to add a value to your dictionary that is an array:
var arrDict = Array<Dictionary<String,Any>>() //Your array
arrDict.append(["id":"3","sellingPrice":"520","quantity":"13"])
arrDict.append(["id":"5","sellingPrice":"43","quantity":"32"])
arrDict.append(["id":"8","sellingPrice":"43","quantity":"33"])
let arrValue = ["420","499","500"] //Your remaining value in array
print("Before ",arrDict)
for (index,dict) in arrDict.enumerated() {
var dictCopy = dict //assign to var variable
dictCopy["remaining_balance"] = arrValue[index]
arrDict[index] = dictCopy //Replace at index with new dictionary
}
print("After ",arrDict)
EDIT
If you are able keep an index of an array it would be possible,
Assuming that you have the index of an array
var dictCopy = arrDict[index]
dictCopy["remaining_balance"] = "666" //Your calculated value
arrDict[index] = dictCopy //Replace at index with new dictionary
var newKV = [["remaining_balance": "420"],["remaining_balance": "490"],["remaining_balance": "500"]]
let array = [["id":"3", "sellingPrice":"520", "quantity":"15"], ["id":"5", "sellingPrice":"520", "quantity":"15"], ["id":"8", "sellingPrice":"520", "quantity":"15"]]
let newArray = array.enumerated().map { (index : Int, value: [String: String]) -> [String: String] in
var dic = value
dic.merge(newKV[index]) { (_, new) -> String in
new
}
return dic
}
You could achieve it by mapping your array:
var myArray = [["id": "3", "sellingPrice": "520", "quantity" : "15"], ["id": "5", "sellingPrice": "499", "quantity" : "-1"], ["id": "8", "sellingPrice": "500", "quantity" : "79"]]
print(myArray)
/*
[["id": "3", "sellingPrice": "520", "quantity": "15"],
["id": "5", "sellingPrice": "499", "quantity": "-1"],
["id": "8", "sellingPrice": "500", "quantity": "79"]]
*/
print("___________________")
var remainingBalanceDesriedValue = 420
myArray = myArray.map { (dict: [String: String]) -> [String: String] in
var copyDict = dict
copyDict["remaining_balance"] = "\(remainingBalanceDesriedValue)"
remainingBalanceDesriedValue = (remainingBalanceDesriedValue == 420) ? 499 : (remainingBalanceDesriedValue == 499) ? 500 : 420
return copyDict
}
print(myArray)
/*
[["sellingPrice": "520", "quantity": "15", "id": "3", "remaining_balance": "420"],
["sellingPrice": "499", "quantity": "-1", "id": "5", "remaining_balance": "499"],
["sellingPrice": "500", "quantity": "79", "id": "8", "remaining_balance": "500"]]
*/
Let's assume you have an array of dictionaries like so:
var arrayOfDictionaries = [
[
"id": 3,
"sellingPrice": 520,
"quantity": 15
]
]
It is important that arrayOfDictionaries is not a let constant, otherwise it is considered immutable and you can not call append on it.
Now you init a new dictionary like:
let newDictionary = [
"id": 10,
"remaining_balance": 420,
"quantity": 15
]
Now add the newDictionary like
arrayOfDictionaries.append(newDictionary)
If the order is important
If the order is important there are a couple of ways to go about that.
When calling append the new value (in this case the new dictionary) will always be inserted at the bottom of the array.
If for some reason you can not call append in the correct order you could use insert, which inserts your dictionary at a specific position.
Yet another way is to append the values wildly and after you are done, call sort on the array.
Improvement Tips
Notice that for the values I did not use strings, as you only have numbers like "id" : 30.
Also, if you want the second key to be called remaining_balance you should call the first key selling_price instead of sellingPrice. Because of conistency.
Alternative approach
As far as I have understood you are trying to implement some software that is responsibly for selling some products.
I think you are tackling this problem from a completely wrong side.
I think you should read about database relationships. Selling products actually is a very common problem.
Maybe this will help you. I would offer a possible solution myself, but I think this misses the point of your question.
If you decide to use the database approach, you won't necessarily have to use a database. You can take the approach and implement it using simple structs/classes/arrays.
I noticed this question lacks an extension answer, yes.. I'm gonna be that guy, so here it is. This could be made more generic by supporting other types of dictionaries, feel free to pitch in ;)
Inspiration from #eason's answer.
var newKV = [["remaining_balance": "420"],["remaining_balance": "490"],["remaining_balance": "500"]]
var array = [["id":"3", "sellingPrice":"520", "quantity":"15"], ["id":"5", "sellingPrice":"520", "quantity":"15"], ["id":"8", "sellingPrice":"520", "quantity":"15"]]
extension Array where Element == [String: String] {
enum UniquingKeysStrategy {
case old
case new
}
mutating func merge(with target: Array<Element>, uniquingKeysWith: UniquingKeysStrategy = .new) {
self = self.merged(with: target)
}
func merged(with target: Array<Element>, uniquingKeysWith strategy: UniquingKeysStrategy = .new) -> Array<Element> {
let base = self.count > target.count ? self : target
let data = self.count > target.count ? target : self
return data.enumerated().reduce(into: base, {
result, data in
result[data.offset]
.merge(data.element, uniquingKeysWith: {
old, new in
if strategy == .new { return new }
return old
})
})
}
}
let mergedArrays = newKV.merged(with: array, uniquingKeysWith: .old)
array.merge(with: newKV)
Happy Coding :)
I have NSDictionary, now I am trying to add an object in a particular key.How can I do that ? below is my code.
let businessPhoneDic = ["locationname" : "",
"area" : "",
"number" : "",
"type" : "",
];
let emailDic:NSMutableDictionary? = ["email" : "",];
let businessPhoneDic2 = ["locationname" : "hello",
"area" : "",
"phonenumber" : "",
"type" : "",
];
var mainDictionary = ["businessPhone" : businessPhoneDic as AnyObject,"email" : emailDic as AnyObject,
];
Now I want to add "businessPhoneDic2" into mainDictionary for key "businessPhone".How can I do that into Swift 3
By definition, you can't have two values mapped to the same key. The following code will replace the old key:
Swift 3
mainDictionary["businessPhone"] = businessPhoneDic2
Just use a new key if you need both values in your dictionary, or maybe store an array of dictionaries as values as such:
var mainDictionary : [String : [NSDictionary]] = ["businessPhone" : [businessPhoneDic]]
Then:
mainDictionary["businessPhone"]?.append(businessPhoneDic2)
First of all do not use MSMutable... collection types in Swift.
Use native Swift Dictionary
let emailDic = ["email" : ""]
Second of all annotate a dictionary with different types as [String:Any]
var mainDictionary : [String:Any] = ["businessPhone" : businessPhoneDic, "email" : emailDic]
If the value for key businessPhone is an array you can append the value. If it's a single dictionary you have to create an array.
This code considers both cases:
let businessPhone = mainDictionary["businessPhone"]
if var phone = businessPhone as? [[String:Any]] {
phone.append(businessPhoneDic2)
mainDictionary["businessPhone"] = phone
} else if let phone = businessPhone as? [String:Any] {
mainDictionary["businessPhone"] = [phone, businessPhoneDic2]
}
Try this :
mainDictionary["your key"] = businessPhoneDic2 as AnyObject // cast as per suggestions
Swift 3.x
// Initialize the Dictionary
var dict = ["name": "Foo", "surname": "Bar"]
// Add a new key with a value
dict["email"] = "foo.bar#email.com"
print(dict)
I've got to deal with a Dictionary like this, or more nested.
How can I access fields like "twotwo" ? Or is there any better possibility to model such structure?
let nestedDict = [
"fieldOne": "name",
"fieldTwo": "name",
"fieldThree":
[
[
"twoOne": "some text",
"twoTwo": true,
"twoThree": 1e-40
],
[
"twoOne": "some text",
"twoTwo": true,
"twoThree": 1e-40
]
]
]
nestedDict is a Dictionary, you get fieldThree with
let fieldThree = nestedDict["fieldThree"] as! [[String:Any]] // [[String:AnyObject]] in Swift 2 and lower.
fieldThree is an Arrayof [String:AnyObject] dictionaries, you get the value of twoTwo of the first array item with
let twoTwo = fieldThree[0]["twoTwo"] as! Bool
You can even retrieve all values of key twoTwo in the array
let allTwoTwo = fieldThree.map { $0["twoTwo"] as! Bool }
I have two Dictionaries that look like:
var dict1 = [Int: [String: AnyObject]]
var dict2 = [Int: [String: AnyObject]]
for example:
dict1: [0: ["sender": "user1", "time": NSDate(), "mId": "as2f2ASf"], [1: ["sender": "user1", "time": NSDate(), "mId": "Sjf82FsJ"]
dict2: [0: ["sender": "user2", "time": NSDate(), "mId": "J2fAS92D"], [1: ["sender": "user2", "time": NSDate(), "mId": "Sjf82FsJ"]
I want to find out if the dictionaries have data with the same mId and if, I want to do some action.
I wrote this code for that and got these errors.
for (_, value) in dict1! {
for content in value {
if content.0 == "mId" {
var mID = content.1
for (_, 2ndValue) in dict2 {
for 2ndContent in 2ndValue {
if 2ndContent.0 == "mId" {
var 2ndMID = 2ndContent.1
if mID == 2ndMID {
//Do Some Action
}
}
}
}
}
}
}
Here are the errors:
https://www.dropbox.com/s/e28ple6640hkrzo/Bildschirmfoto%202015-08-28%20um%2021.21.54.png?dl=0
Firstly, as Eric says in his comment, you are getting errors because you can't start a variable name with a number.
Secondly what you are calling a 'dictionary' here is in fact an array of tuples - if you define your dict1 variable in the Swift REPL by running var dict1 = [Int,[String,AnyObject]()) you get:
dict1: [(Int, [String : AnyObject])] = 0 values
A dictionary with the equivalent structure would be defined thus:
var dict1 = [Int:[String:AnyObject]]() - this returns
dict2: [Int : [String : AnyObject]] = 0 key/value pairs
in the REPL.
Therefore the dict1 and dict2 declarations you show will not work. They need to be declared as follows to match the type you've specified:
var dict1 = [(0, ["sender": "user1", "time": NSDate(), "mId": "as2f2ASf"]), (1, ["sender": "user1", "time": NSDate(), "mId": "Sjf82FsJ"])]
var dict2 = [(0, ["sender": "user2", "time": NSDate(), "mId": "J2fAS92D"]), (1, ["sender": "user2", "time": NSDate(), "mId": "Sjf82FsJ"])]
If you want to use an actual dictionary, use :
var dict3 = [[0: ["sender": "user1", "time": NSDate(), "mId": "as2f2ASf"]], [1: ["sender": "user1", "time": NSDate(), "mId": "Sjf82FsJ"]]]
While I can see what you're trying to achieve in the code you've posted, I don't think the approach you're taking is the right one. Any time you have 4 levels of nested for loops you should consider whether there's a better way of solving the problem.
You are iterating over all the keys in the dictionary when you are only interested in the value of "mId". Just looking for the value of that key gets rid of 2 loops and a lot of pointless iterating over things you aren't interested in:
for (_,value1) in dict1 {
if let mld1 = value1["mId"] {
for(_,value2) in dict2 {
if let mld2 = value2["mId"] {
if mld1 == mld2 {
println("Found diff")
}
}
}
}
}
The if let is necessary to ensure you are making a valid comparison (i.e. to avoid a false positive if the value of 'mld' is nil in both dicts). No idea whether this is even possible in your data model but always worth being thorough. Note that this solution uses arrays of tuples, as in your examples - you'd need something slightly different if you use actual dictionaries.
Well im not sure i understood your problem, but this is my idea for that:
for (key, value) in dicOne {
for (keyTwo, valueTwo) in dicTwo {
for (keyItem,valueItem) in value {
for (keyItemTwo, valueItemTwo) in valueTwo {
if keyItem == keyItemTwo && valueItem == valueItemTwo{
println("do some action")
}
}
}
}
}
The first loop is for the first dictionary and the second, for the other dictionary. After this you'r in the second layer ["a": "A"] of both dictionaries. So the next step is to get this dictionary. For this are the other two loops.
Just a question: Did u tried tuple? Like this:
var dictionaryOne:[Int: (String, String)] = [1: ("a", "A")]
var dictionaryTwo:[Int: (String, String)] = [1: ("a", "A")]
for (key, value) in dictionaryOne {
for (keyTwo, valueTwo) in dictionaryTwo {
if value.0 == valueTwo.0 && value.1 == valueTwo.1 {
println("nice action")
}
}
}
I am parsing this json:
{
"destination_addresses" : [ "Göteborg, Sverige" ],
"origin_addresses" : [ "Stockholm, Sverige" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "467 km",
"value" : 466568
},
"duration" : {
"text" : "4 timmar 34 min",
"value" : 16468
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
Into an NSDictionary like this:
let dataDict = NSJSONSerialization.JSONObjectWithData(data.dataUsingEncoding(NSUTF8StringEncoding), options: nil, error: nil) as NSDictionary
let distance = dataDict.valueForKeyPath("rows.elements.distance.value")
Now, this works, no crashes or errors. But, I expect it to return 466568 but instead it returns this:
(
(
466568
)
)
I dont know why and I cant use it and cant cast it into anything :(
There is another question similar to this, but the answer there didnt help me because Im not the one to change the json, Google is. So please no hate.
valueForKeyPath is returning a nested NSArray. You might want to cast it twice:
let distanceValue = ((distance as NSArray).objectAtIndex(0) as NSArray).objectAtIndex(0) as NSNumber
// or
let distanceValue = (distance as [NSArray])[0][0] as NSNumber
The distance value can't be directly accessed because valueForKeyPath does't implement a selector for array items.
Without valueForKeyPath:
let rows = dataDict.objectForKey("rows") as [NSDictionary]
let elements = rows[0]["elements"] as [NSDictionary]
let distance = elements[0]["distance"] as NSDictionary
let value = distance["value"] as NSNumber
This may be because rows is giving you array instead of Dictionary. So first get the value of rows which will give you an array. Get the object at zero index of your array and assign it to Dictionary and then try to get elements.distance.value