I need your help. I did an array, and when I launch my application on different devices or simulator, this array sorted into different types, but I need the same order on each device. How to do that?
var settings = [String: [ [String: String] ]]()
settings = [ "cards":[ ], "groups":[ ] ]
...
for card in db.prepare(table.order(orderN.asc)) {
settings["cards"]?.append(["id":String(card[id]), "group_id":String(card[group_id]), "service":card[service], "bgcolor":card[bgcolor], "logoimg":card[logoimg]!, "balance_desc":card[balance_desc], "balance":"0.0", "balance_id":String(card[balance_id]), "uniqueID":card[uniqueID], "balance_currency":String(card[balance_currency]), "orderN":String(card[orderN])])
}
...
for group in db.prepare(table.order(orderN.asc)) {
settings["groups"]?.append(["name":group[name]!,"id":String(group[id]),"orderN":String(group[orderN])])
}
...
For example,
On the first device
print(settings) // ["cards": [["orderN": "0", "bgcolor": "0.0, 0.0, 0.0, 1.0", "balance": "0.0", "logoimg": "example.com/images/img.png", "uniqueID": "00a2413f74f4a3f186e439a67057de67", "group_id": "2", "id": "1", "service": "servicename", "balance_desc": "Description", "balance_id": "1", "balance_currency": "1"]], "groups": [["orderN": "0", "name": "GroupName", "id": "2"]]]
On the second device
print(settings) // ["cards": [["orderN": "0", "uniqueID": "00a2413f74f4a3f186e439a67057de67", "service": "servicename", "id": "1", "bgcolor": "0.0, 0.0, 0.0, 1.0", "balance_currency": "1", "balance": "0.0", "group_id": "2", "logoimg": "example.com/images/img.png", "balance_id": "1", "balance_desc": "Description"]], "groups": [["id": "2", "orderN": "0", "name": "GroupName"]]]
Thank you for you attention.
That's because settings is not an Array, it's Dictionary. Order of key-value pairs in dictionary is not defined.
If you need some particular order, you should reimplement settings, probably make them separate struct or class, because you'll have a hard time working with nested dictionaries.
Related
I just want to create an API JSON Structure. The following are the post body keys and objects. Are there any methods like object with keys and values similar to Objective C in Swift 4?
{
"name": "switch 1",
"type": "Switch",
"gatewayId":515,
"serialKey": "98:07:2D:48:D3:56",
"noOfGangs": 4,
"equipments": [
{
"name": "light",
"type": "Light",
"port": "1"
},
{
"name": "television",
"type": "Television",
"port": "3"
}
]
}
You can create the dictionary literally by annotating the type and replace the curly braces with square brackets
let dict : [String:Any] = ["name": "switch 1", "type": "Switch", "gatewayId":515, "serialKey": "98:07:2D:48:D3:56", "noOfGangs": 4, "equipments": [[ "name": "light", "type": "Light", "port": "1" ], ["name": "television", "type": "Television", "port": "3" ]]]
Or build it:
var dict : [String:Any] = ["name": "switch 1", "type": "Switch", "gatewayId":515, "serialKey": "98:07:2D:48:D3:56", "noOfGangs": 4]
var equipments = [[String:String]]()
equipments.append(["name": "light", "type": "Light", "port": "1" ])
equipments.append(["name": "television", "type": "Television", "port": "3" ])
dict["equipments"] = equipments
how to create Dictionary
var populatedDictionary = ["key1": "value1", "key2": "value2"]
this how to create Array
var shoppingList: [String] = ["Eggs", "Milk"]
you can create Dictionary by this type
var dictionary = [Int:String]()
dictionary.updateValue(value: "Hola", forKey: 1)
dictionary.updateValue(value: "Hello", forKey: 2)
dictionary.updateValue(value: "Aloha", forKey: 3)
// anather example
var dict = [ 1 : "abc", 2 : "cde"]
dict.updateValue("efg", forKey: 3)
print(dict)
your JSON
let dic :[String:Any] = ["name": "switch 1", "type": "Switch", "gatewayId":515, "serialKey": "98:07:2D:48:D3:56", "noOfGangs": 4, "equipments": [ [ "name": "light", "type": "Light", "port": "1" ],
[ "name": "television", "type": "Television", "port": "3" ] ] ]
Hi all how do i create a model class with this kind of response in swift using SiwftJson am not sure how to include this
"total_songs": 12,
"title": "Arun Thapa",
"result": "success",
"favorite": "false",
"cover": "artists/4096759431451532756hqdefault.jpg" in model using below response
{
"total_favorite": 0,
"Data": [
[
{
"album_song_id": "120",
"artist_name": "Arun Thapa",
"status": "1",
"song_duration": "245213",
"song_favorite": "false",
"albumb_name": "Arun Thapa",
"created_by": "1",
"created_at": "0000-00-00 00:00:00",
"albumb_id": "42",
"chart_name": "Evergreen Songs",
"song_lyrics": "<html>\r\n<head>\r\n\t<title></title>\r\n</head>\r\n<body></body>\r\n</html>\r\n"
}
]
"total_songs": 12,
"title": "Arun Thapa",
"result": "success",
"favorite": "false",
"cover": "artists/4096759431451532756hqdefault.jpg"
}
thanks in advance
This is an example how to map JSON to a struct. I hope you can start with this piece of code.
Pay attention to optional and not-optional values.
struct SomeDataModel {
let totalFavorites: Int
let totalSongs: Int
// other properties go here
init(withJson json: JSON) {
self.totalFavorites = json["total_favorites"].intValue
self.totalSongs = json["total_songs"].intValue
// initialization of other properties go here
}
}
First of all your JSON data missing "]," in end of "Data".
So correct JSON according me :
{
"total_favorite": 0,
"Data": [
[
{
"album_song_id": "120",
"artist_name": "Arun Thapa",
"status": "1",
"song_duration": "245213",
"song_favorite": "false",
"albumb_name": "Arun Thapa",
"created_by": "1",
"created_at": "0000-00-00 00:00:00",
"albumb_id": "42",
"chart_name": "Evergreen Songs",
"song_lyrics": "<html>\r\n<head>\r\n\t<title></title>\r\n</head>\r\n<body></body>\r\n</html>\r\n"
}
]
],
"total_songs": 12,
"title": "Arun Thapa",
"result": "success",
"favorite": "false",
"cover": "artists/4096759431451532756hqdefault.jpg"
}
And i suggest one json To Swift model converter, you need to just simple put JSON and you get swift model file.
If you don't need extra code you can remove it from swift model file.
Link of JSON to SWIFT MODEL CONVERTER - http://www.json4swift.com
I have a simple "rss" (ApplicationRecord) table indexed by an id. I would like to have a structured JSON that group each user from a family in an array structure. And then each family in a global array. How can I do that ?
my current plain code to put my data in a json file is :
json.rss #rss do |rs|
json.id rs.id
json.name rs.name
json.family rs.family
json.lastdate rs.lastdate
json.last rs.last
json.s1w rs.s1w
json.s2w rs.s2w
end
But the target file that I want is this one :
{
"rss": [
{
"familyname": "Smith",
"children": [
{
"id": "1",
"name": "bob",
"lastdate": "2010-09-23",
"last": "0.88",
"s1w": "0.83",
"s2w": "0.88"
},
{
"id": 2,
"name": "Mary",
"lastdate": "2011-09-23",
"last": "0.89",
"s1w": "0.83",
"s2w": "0.87"
}
]
},
{
"familyname": "Wesson",
"children": [
{
"id": "1",
"name": "john",
"lastdate": "2001-09-23",
"last": "0.88",
"s1w": "0.83",
"s2w": "0.88"
},
{
"id": 2,
"name": "Bruce",
"lastdate": "2000-09-23",
"last": "0.89",
"s1w": "0.83",
"s2w": "0.87"
}
]
}
]
}
The grouping you are trying to achieve can be done in Ruby with:
#rss.group_by(&:family).values
This is assuming #rss is an array-like collection of objects that have a .family method. The result: is an array of arrays of objects grouped by family.
Now it will be up to use to use Jbuilder's array! method to build the desired JSON output.
Here is the request format that server requires ,
{
"internal_name": "SDSSD",
"display_name": "SDSDSDSD",
"image": "sesse.jpg",
"notes": "sdsdsdsdsdsdsdsd",
"short_description": "ssdsdsd",
"long_description": "sdsdsdsd",
"ean_code": "3434343434",
"status": "not_verified",
"state": "active",
"quantity": 1,
"brand": {
“name”: “My Brand”
},
"categories": [
{
“id”: “My Category”
}
]
}
In here , as you can see , it requires , categories as an array , so my question is how can i create an array . using swift . here is my swift code
let parameters :[String:AnyObject] = [
"internal_name":product.displayName,
"display_name":product.displayName,
"language":Constant.Language.LAN_ENGLISH,
"notes":product.initialName,
"image": product.photo,
"short_description":product.longDescription,
"long_description":product.longDescription,
"ean_code":product.eanCode,
"status":product.status,
"state":Constant.Status.STATUS_ACTIVE,
"categories": [
"id":product.categoryObject.id
],
"quantity":1,
]
this doesnt accept from the server since its not an array , what am i missing here
Try below code :
let parameters :Parameters = [
"internal_name":product.displayName,
"display_name":product.displayName,
"language":Constant.Language.LAN_ENGLISH,
"notes":product.initialName,
"image": product.photo,
"short_description":product.longDescription,
"long_description":product.longDescription,
"ean_code":product.eanCode,
"status":product.status,
"state":Constant.Status.STATUS_ACTIVE,
"brand" : ["name" : "My Brand"],
"categories": [
["id":product.categoryObject.id]
],
"quantity":1,
]
Below JSON contains 4 items in an array. If you look at each item it is some what shows incomplete keys. I am unable to figure out how to represent the consistent data in iOS (of any UI design patterns). By looking at the below information somewhat they are interlinked to each other for example parent key value information is same as company key value and also in employees one value is same as name key value. This seems to be very typical.
{
"arays": [
{
"company": "Microchip",
"parent": "File",
"employees": [
"John",
"Mike"
]
},
{
"company": "Apple",
"mobiles": [
"111-111-1111",
"121-121-1212"
]
},
{
"company": "File",
"parent": "Apple",
"addresses": [
"2600 space center blvd",
"2700 university dr"
]
},
{
"name": "John",
"mobiles": [
"222-222-2222"
],
"addresses": [
"Time Square, NY"
]
}
]
}