By default, While a fetching a resource, it returns a Object with key _items.
{
"_items": [
{
"_id": "5d0b787a10414b47f8e8c858",
"username": "A",
"message": "TEST",
"_updated": "Thu, 20 Jun 2019 12:13:43 GMT",
"_created": "Thu, 20 Jun 2019 12:13:43 GMT",
"_etag": "1fad0ab63ebe77731bf623800b8367ac6b94e8a8"
}
]
}
but I would like to get only the array of items as response.
[
{
"_id": "5d0b787a10414b47f8e8c858",
"username": "A",
"message": "TEST",
"_updated": "Thu, 20 Jun 2019 12:13:43 GMT",
"_created": "Thu, 20 Jun 2019 12:13:43 GMT",
"_etag": "1fad0ab63ebe77731bf623800b8367ac6b94e8a8"
}
]
I tried using on_fetched_resource. It can modify the response Object. But not able to assign an array to response
I think it can't be done, as the response is to be a dictionary which contains keys that might appear (pagination data, HATOEAS links, and actual documents).
As per Nicola larocci answers, it is currently not possible, however if you are using ajax or someother thing to fetch the response, you can chop off _item or use some other technique to do so.
Related
I have array like this.
var Array = [Int:[String:Any]]()
I just update the values
let updateValue = ["Name":tripName!,"date":firstDate,"year":year,"startData":startDateValue,"endDate":endDateValue,"countryName":countrname!,"totelBudjet":totelBudjet,"backgroundImage":arrImages[randomItem],"code":getAddtripeAttibutes.code,"countryIcon":countryIcon] as [String : Any]
Array.updateValue(updateValue, forKey: tripId)
tripId = tripId + 1
We have more Array data as mentioned below
trip keys like :
[0] [1] [2]
[0: ["year": "2019", "tripName": "Test", "backgroundImage": "Europe_Trip", "code": "DZD", "startData": 2019-05-30 10:14:11 +0000, "date": "May 30 - May 31", "endDate": 2019-05-31 10:14:11 +0000, "totelBudjet": "5000 DZD", "countryIcon": "dz", "countryName": "Algeria"], 1: ["date": "May 30 - May 31", "backgroundImage": "Europe_Trip", "endDate": 2019-05-31 10:14:43 +0000, "code": "DZD", "countryName": "Algeria", "tripName": "Gg", "countryIcon": "dz", "totelBudjet": "500 DZD", "startData": 2019-05-30 10:14:43 +0000, "year": "2019"], 2: ["year": "2019", "backgroundImage": "Asia_Trip", "endDate": 2019-05-31 10:15:00 +0000, "countryIcon": "al", "totelBudjet": "5800 ALL", "code": "ALL", "tripName": "Bb", "countryName": "Albania", "date": "May 30 - May 31", "startData": 2019-05-30 10:15:00 +0000]]
I have updated tableview with above data
after i delete one array [1] it look like:
this
[0] [2]
[0: ["year": "2019", "tripName": "Test", "backgroundImage": "Europe_Trip", "code": "DZD", "startData": 2019-05-30 10:14:11 +0000, "date": "May 30 - May 31", "endDate": 2019-05-31 10:14:11 +0000, "totelBudjet": "5000 DZD", "countryIcon": "dz", "countryName": "Algeria"], 2: ["year": "2019", "backgroundImage": "Asia_Trip", "endDate": 2019-05-31 10:15:00 +0000, "countryIcon": "al", "totelBudjet": "5800 ALL", "code": "ALL", "tripName": "Bb", "countryName": "Albania", "date": "May 30 - May 31", "startData": 2019-05-30 10:15:00 +0000]]
But I want to update the keys as [0][1] not as [0][2]
Can any one please help me with this?
Why are you trying to mimic an array using a dictionary?
If you have an array like this.
var array: [[String: Any]] = [["year": "2019"], ["year": "2020"], ["year": "2021"]] // Minimalistic example array
You can delete values at that particular location and it'll give you what you need currently.
array.remove(at: 1)
print(array[1])
["year": "2021"]
Note - Your data looks like JSON response. So, you could use #Kamran's suggestion in the comments and use a struct Trip conforming Decodable and directly get your data as an array of Trips. However, if your JSON is formatted with keys 0, 1, 2... then your JSON is really bad and you should get it changed if you can. If you can't, you'll have to change the dictionary of dictionaries to an array of dictionary or better yet array of structs.
it is not quite clear to me what's the use-case here, so I'm trying to cover the most possible.
using enumerated() on the array
if you need only to add the index, call the enumerated() method on an array, like e.g.
var array: [[String: Any]] = [["year": "2019", "tripName": "Trip#1"], ["year": "2020", "tripName": "Trip#2"], ["year": "2021", "tripName": "Trip#3"]]
array.enumerated().forEach { (arg0) in
let (offset, element) = arg0
debugPrint("\(offset) : \(element)")
}
that would assign an offset to each element which you can print out or use otherwise, in my example the output on the console would look like this:
0 : ["tripName": "Trip#1", "year": "2019"]
1 : ["tripName": "Trip#2", "year": "2020"]
2 : ["tripName": "Trip#3", "year": "2021"]
if you need to actually remove an item, like e.g.
array.remove(at: 1)
and call the same snippet (above) again, it assigns the offset like this:
0 : ["tripName": "Trip#1", "year": "2019"]
1 : ["tripName": "Trip#3", "year": "2021"]
that is quite similar to what you might looking for.
mapping an array from dictionary
if you need to actually map the dictionary first into an array (like e.g. during post-processing a JSON response), you may go for that as well like e.g.:
var dictionary: [Int: [String: Any]] = [0: ["year": "2019", "tripName": "Trip#1"], 1: ["year": "2020", "tripName": "Trip#2"], 2: ["year": "2021", "tripName": "Trip#3"]]
var array = dictionary.map { $0.value }
then you can start dealing with the enumerated() method just like I shown it to you above.
NOTE: obviously if you do the mapping like I did, you will lose the information what the original key holds, and that will be replaced coincidently with the same number in my example, which may be misleading but if you are aware of that the offset and the key are two completely different values here, you may not have a huge surprise after you do the mapping.
how to assert the json response array contains a substring in a string value.
I have tried this but assertion is failed response.then().body("response.content.Date", Matchers.everyItem(contains("2019-03") ));
my response body is
{
"response": {
"totalSize": 2,
"content": [
{
"requestId": " 931-f8222e",
"name": "gowtham",
"date": "2019-03-06",
"issue": "i have a cause"
},
{
"requestId": " 931-f8222e",
"name": "tharun",
"date": "2019-03-09",
"issue": "has a issue in billing"
}
]
}
}
i want to get all the records in the month(value) and assert the response showing data for given month
I found the solution. You were very close. Instead of using contains which matches full string, you can use containsString to match a substring.
Code:
response.then().body("response.content.date", Every.everyItem(Matchers.containsString("2019-03")));
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
}
}]
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.)