I need to send data over socketio from ios using swift. Data looks like this:
{
birthday: {
"date": 24,
"month": 12,
"year": 1991
}
}
I tried this code, but it doesn't work:
let myJSON = [
"birthday" : [
"date": 24,
"month": 12,
"year": 1991
]
]
socket.emit("updateUserInfo", myJSON)
Please, tell me what i'm doing wrong
January 2020
Asumming that you are using Socket.IO-Client-Swift Library
Here is the sample source on how to effectively pass your JSON data.
// Adopt to SocketData protocol
struct CustomData : SocketData {
let name: String
let age: Int
func socketRepresentation() -> SocketData {
return ["name": name, "age": age]
}
}
// Usage
socket.emit("myEvent", CustomData(name: "Erik", age: 24))
You are right that if you have a dictionary to send over socket.io then you don't have to convert it to JSON because the method socket.emit does that for you.
So the syntax you show seems to be correct : socket.emit("event-as-string", dictionary).
You should tell us the exact error you are encountering such as someone can help you.
With socket.io you can send a normal object from javascript and it will be converted to JSON automatically.
I tested that code and everything is ok
let myJSON = { birthday: { "date": 24, "month": 12, "year": 1991}};
socket.emit("updateUserInfo", myJSON);
I believe you'll need to convert your Swift object to a JSON string. As Eric D. said there's a built in way to convert dictionary to JSON. If you already have a class representation of the data, you can take a look at a Swift class to JSON converter here:
https://gist.github.com/peheje/cc3618253d4f38ea4885
Have you tried just writing it as a string?
let myJSON = "[\"birthday\" : [\"date\": 24, \"month\": 12, \"year\": 1991]]"
Try this may work , internally it's getting object from zero index
[{
birthday: {
"date": 24,
"month": 12,
"year": 1991
}
}]
Related
I have a JSON response that looks something like this:
{
"persons": {
"John": {
"name": "John",
"age": 24
},
"Michael": {
"name": "Michael",
"age": 44
},
"Jack": {
"name": "Jack",
"age": 25
}
}
}
As you can see this could be parse with a struct that looks like this:
struct PersonsResponse: Decodable {
let persons: [String: Person]
}
struct Person: Decodable {
let name: String
let age: Ing
}
However, what this does, is returns a dictionary and parses fine. What I would need is to preserve the order of the persons as they arrive inside this JSON response. I have come across KeyValuePairs in swift which basically are ordered dictionaries but for the love of God I can't figure out how to decode it into being an KeyValuePairs<String, Person>.
Apple documentation says that instantiating a KeyValuePairs object is as easy as doing:
let recordTimes: KeyValuePairs = ["Florence Griffith-Joyner": 10.49,
"Evelyn Ashford": 10.76,
"Evelyn Ashford": 10.79,
"Marlies Gohr": 10.81]
Literally. But when I am decoding my response with:
struct PersonsResponse: Decodable {
let persons: KeyValuePairs<String, Person>
...
...
init(from decoder: Decoder) throws {
...
let personsDictionary = try container.decode([String: Person].self, forKey: .persons)
and then try to do:
persons = personsDictionary
of course it doesn't work at all. I tried to do all kinds of magic already with no luck. Does anyone have any solution to parsing dictionaries into ordered sequences or even Arrays? Thanks for helping!
There is NO WAY the JSON response changes into being an array and YES it always is the same order.
Both Swift, JSON Dictionaries are unordered by there nature. The JSON format does notmaintain key ordering, and as such, does not required parser to preserve the order.
If you need an ordered collection, you its better to returning an array of key-value pairs in the JSON
{
"persons": [
{ "John": {
"name": "John",
"age": 24
}
},
{"Michael": {
"name": "Michael",
"age": 44
}
},
{ "Jack": {
"name": "Jack",
"age": 25
}
}
]
}
I want to append my JSON data for sending to server..
I have this code
var json = JSONSerializer.toJson(data2)
and this is my data2
var data2 = Activitiesdata (type: leftlabel.text!, amount: qtytext.text.toInt()!)
and this is my Activitiesdata
class Activitiesdata
{
var type = "type"
var amount = 0
init (type: String, amount: Int)
{
self.type = type
self.amount = amount
}
}
when I click on a icon (lets say softball) and I println the outcome like this
{"type": "Softball", "amount": 90}
but whenever I click another icon, the outcome like this
{"type": "Badminton", "amount": 60}
how to append the data? so that the outcome like this whenever I click the second button and so on..
{"type": "Softball", "amount": 90} {"type": "Badminton", "amount": 60}
or is it possible to make like this
{"type": "Softball", "amount": 90, "type": "Badminton", "amount": 60} // (only 1 bracket)
thanks
using array is a good idea, but I always get compile error .. please help thanks
You put not enough information to answer, but it looks like you create json object every time you press the button, that's why the old value in a variable var json is replaced by a new one. You must first create an array of the objects and then serialize it into json.
Your JSON result will looks somthing like this:
{[{"type": "Softball", "amount": 90}, {"type": "Badminton", "amount": 60}]}
I do not know what mechanism you use to serializing, but if it write in the style in which it is written in your code, it will be something like this:
var dataArray: [Activitiesdata] = []
// click on a icon
let firstElement = Activitiesdata (type: leftlabel.text!, amount: qtytext.text.toInt()!)
dataArray.append(firstElement)
// click another icon
let secondElement = Activitiesdata (type: otherlabel.text!, amount: otherText.text.toInt()!)
dataArray.append(secondElement)
// serialize array to json in your style
var json = JSONSerializer.toJson(dataArray)
There is a good library for working with JSON - https://github.com/SwiftyJSON/SwiftyJSON
Sorry if my question is not clear, I'll try to make myself clear with an explanation. So here is exactly what I'm trying to do, I'm trying to use Alamofire to post more than one comment (Something that my app implements and will be stored as a JSON object whenever user writes a new comment). I'm passing these JSON comments to my post routine, where I can use SwiftyJSON to extract each value. Noe the thing is I know how to set the parameters if I'm trying to authorize the user as follows,
var parameters = [
"userName": userName,
"password": passwordSalt,
"somethingElse": somethingElse
]
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters , options: nil, error: &err)
it's quite straightforward until here, now comes my problem. I'm trying to use alamofire post to post multiple json objects, which should look like this
[
{
"comment": "my First Comment",
"commentDate": "2014-05-13 14:30 PM",
"isSigned": 1,
"patientId": 2,
"documentId": 3
},
{
"comment": "my SecondComment",
"commentDate": "2014-05-14 14:30 PM",
"isSigned": 2,
"patientId": 3,
"documentId": 4
},
{
"comment": "my third Comment",
"commentDate": "2014-05-15 14:30 PM",
"isSigned": 3,
"patientId": 4,
"documentId": 5
}
]
How do I create above array/json (I'm not exactly sure on what to call this) by iterating JSON object? I know how to get the JSON values from the JSON object all I'm asking is how to create this parameters variable to hold the data like above example. Is it even possible to do this using Alamofire? (POST multiple objects at once)
I tried a couple of ways to but they didn't work out
var dictArray = [Dictionary<String, Any>]
var dict = Dictionary<String, Any>
While iterating over JSON object inserted each value in dict and appended dict to dictArray, now when I'm trying to use dictArray as parameters in .dataWithJSONObject it doesn't like the object.
var dict = Dictionary<String, AnyObject>
var array = NSArray()
extracted each value by iterating over the JSON object and inserted them into dict and tried inserting dict into array. But this gives a different problem. The way it builds the objects is different from what is required, as follows.
[
{
comment: my First Comment,
commentDate: 2015-05-13 13:30 PM"",
isSigned: 1,
patientId: 2,
documentId: 3
},
{
comment: my Second Comment,
commentDate: 2015-05-13 13:30 PM"",
isSigned: 2,
patientId: 5,
documentId: 4
},
{
comment: my third Comment,
commentDate: 2015-06-13 13:30 PM"",
isSigned: 5,
patientId: 1,
documentId: 9
}
]
Here the Keys doesn't get wrapped inside quotes (Correct way: "comment", wrong way: comment).
Did anyone try posting multiple objects, is alamofire capable of doing so? I hope I made the question clear. Sorry if this is too simple of a question to answer, I spent my whole day figuring this out but didn't work out.
The correct representation in Swift for the array of comment objects you have posted would be like this:
let comments: Array<[String:AnyObject]> = [
[
"comment": "my First Comment",
"commentDate": "2014-05-13 14:30 PM",
"isSigned": 1,
"patientId": 2,
"documentId": 3
],
[
"comment": "my SecondComment",
"commentDate": "2014-05-14 14:30 PM",
"isSigned": 2,
"patientId": 3,
"documentId": 4
],
[
"comment": "my third Comment",
"commentDate": "2014-05-15 14:30 PM",
"isSigned": 3,
"patientId": 4,
"documentId": 5
]
]
Sending a single comment would be fairly simple:
let comment: [String:AnyObject] = [
"comment": "my First Comment",
"commentDate": "2014-05-13 14:30 PM",
"isSigned": 1,
"patientId": 2,
"documentId": 3
]
Alamofire.request(.POST, "http://httpbin.org/post", parameters: comment).responseJSON { (req, res, json, error) in
println(req)
println(res)
println(json)
println(error)
}
However, in order to send an array of comments, seems like you would have to generate the URLRequest your self and then pass it to Alamofire as follows:
let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "http://httpbin.org/post")!)
mutableURLRequest.HTTPMethod = "POST"
var error: NSError? = nil
let options = NSJSONWritingOptions.allZeros
if let data = NSJSONSerialization.dataWithJSONObject(comments, options: options, error: &error) {
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
mutableURLRequest.HTTPBody = data
}
Alamofire.request(mutableURLRequest).responseJSON { (req, res, json, error) in
println(req)
println(res)
println(json)
println(error)
}
If you could modify the API backend to accept an object with multiple comments, you could also send them this way:
Alamofire.request(.POST, "http://httpbin.org/post", parameters: ["comments": comments]).responseJSON { (req, res, json, error) in
println(req)
println(res)
println(json)
println(error)
}
Regards.
This would be better
Create dictionary and array of dictionary var
Then loop how many parameters you need to send from data source either an array or whatever.
Here my scenario
Need to answer to all questions (will be a random number/size)
var ansParams = [[String: String]]()
var paramz = [String: String]()
for question in sectionQuestions{
paramz = [
AppConstants.PARAMETER.KEY_1 : "Value",
AppConstants.PARAMETER.KEY_2 : "Value",
AppConstants.PARAMETER.KEY_3 : "Value",
AppConstants.PARAMETER.KEY_4 : "Value",
AppConstants.PARAMETER.KEY_5 : "Value"
]
ansParams.append(paramz)
}
print(ansParams)
//Check All Paramz and its values then send ansParams as Parameter to POST request
I had a similar issue in my project while working with an API that did now allow posting multiple objects at once. The formatting of the array as noted above is fine.
let comments: Array<[String:AnyObject]> = [
[
"comment": "my First Comment",
"commentDate": "2014-05-13 14:30 PM",
"isSigned": 1,
"patientId": 2,
"documentId": 3
],
[
"comment": "my SecondComment",
"commentDate": "2014-05-14 14:30 PM",
"isSigned": 2,
"patientId": 3,
"documentId": 4
],
[
"comment": "my third Comment",
"commentDate": "2014-05-15 14:30 PM",
"isSigned": 3,
"patientId": 4,
"documentId": 5
]
]
Then I used a for loop to post each object of the array to post the API.
var index = comments.count
var i = 0
for i = 0; i < index; i++ {
let urlString = "\(.baseURL)...etc"
let parameters = comments[i]
Alamofire.request(.POST, urlString, parameters: parameters)
.responseJSON { (req, res, data, error) -> Void in
// println(req)
// println(res)
// println(data)
// println(error)
println("\(i) of \(index) posted")
}
}
More efficient ways if the API allows, but otherwise this flow works great.
I am working in iOS using Swift. The service I am using returns the following JSON (cut off as the node begins to repeat):
{
"Ambler Departures: April 15, 2015, 2:57 pm": [
{
"Northbound": [
{
"direction": "N",
"path": "R5N",
"train_id": "564",
"origin": "Frazer Yard",
"destination": "Colmar-Link Belt",
"status": "On Time",
"service_type": "LOCAL",
"next_station": "North Broad St",
"sched_time": "Apr 15 2015 03:27:00:000PM",
"depart_time": "Apr 15 2015 03:27:00:000PM",
"track": "2",
"track_change": null,
"platform": " ",
"platform_change": null
},
{
"direction": "N",
"path": "R5N",
"train_id": "6566",
"origin": null,
Notice that the root node is created dynamically by the service-- I don't have a way of knowing what it will be as the user can chose departures from one of fifty stations and the time is based on the time on the server, which is likely inconsistent with the time on the user's clock.
I want to extract the data from the Northbound Array, and then a Southbound Array lower in the JSON.
I am getting the data returned form the service but I can't parse correctly. I can't get past the root node (the dynamic one) to get to the structure inside.
Here's what I am trying:
let json = JSON(data:data)
var x = 0
while x < json.count
{
let track = json[0]["Northbound"][x]["track"].string
println(track)
x++
}
This results in "nil". I am stuck...
I know that json[0] is probably wrong since that level of the JSON is not an array. Thanks in advance
If json is a JSON object from SwiftyJSON but you know it's a dictionary with an unknown key, you can find this key with:
let theKey = json.dictionary!.keys.first
Then access the array:
let theArray = json[theKey] as! [[String:AnyObject]] // array of dictionaries
Then theArray[0] will be the dictionary with "Northbound" as a key, which contains an array of dictionaries containing the infos.
(Edited to reflect the correct answer specific to SwiftyJSON.)
Here is my Json
{
"id": "63",
"name": "Magnet",
"price": "₹1250",
"description": "",
"image": [
"catalog/IMG-20150119-WA0012_azw1e3ge.jpg",
"catalog/IMG-20150119-WA0029_6mr3ndda.jpg",
"catalog/IMG-20150119-WA0028_ooc2ea52.jpg",
"catalog/IMG-20150119-WA0026_4wjz5882.jpg",
"catalog/IMG-20150119-WA0024_e38xvczi.jpg",
"catalog/IMG-20150119-WA0020_vyzhfkvf.jpg",
"catalog/IMG-20150119-WA0018_u686bmde.jpg",
"catalog/IMG-20150119-WA0016_c8ffp19i.jpg"
],
"thumb_image": [
"cache/catalog/IMG-20150119-WA0012_azw1e3ge-300x412.jpg",
"cache/catalog/IMG-20150119-WA0029_6mr3ndda-300x412.jpg",
"cache/catalog/IMG-20150119-WA0028_ooc2ea52-300x412.jpg",
"cache/catalog/IMG-20150119-WA0026_4wjz5882-300x412.jpg",
"cache/catalog/IMG-20150119-WA0024_e38xvczi-300x412.jpg",
"cache/catalog/IMG-20150119-WA0020_vyzhfkvf-300x412.jpg",
"cache/catalog/IMG-20150119-WA0018_u686bmde-300x412.jpg",
"cache/catalog/IMG-20150119-WA0016_c8ffp19i-300x412.jpg"
],
"specifications": [
{
"Fabrics": [
"Pure chiffon straight cut suits 48" length"
]
},
{
"MOQ": [
"Minimum 10"
]
}
]
}
In above json string the "specification" arraylist has dynamic number of key and each key has dynamic number of values
So how can parse this? Please help if anyone knows this...
Thanks in advance
There are multiple ways to do parsing. In your case specifications should be an array, so you'll be able to loop on each items.
You might want to :
Create your own JSON parse class / methods ;
Use an existing library to parse JSON.
For the second option, you can give a look at the following :
https://github.com/Wolg/awesome-swift#jsonxml-manipulation
var yourJson = data as? NSDictionary
if let id = yourJson.valueForKey("id") as String
{
//save your id from json
}
if let name = yourJson.valueForKey("name") as String
{
//save your name
}
...
if let images = yourJson.valueForKey("image") as NSArray
{
for im in images
{
//save image
}
//the same for all othe images
}
... And so on...
You should also watch some tutorials, to understand the basics of JSON parsing..
https://www.youtube.com/watch?v=MtcscjMxxq4