I have problem with the way i received the json in the server. have students array (of dictionary) wrapping it in dictionary:
var students:[[String: String]] = []
students.append(["id":"2", "name":"joe"]);
students.append(["id":"3", "name":"jake"]);
students.append(["id":"4", "name":"may"]);
students.append(["id":"1", "name":"donna"]);
let json:[String:[[String:String]]] = ["students" : students]
when printing the json using NSJSONSerialization
var data = NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.PrettyPrinted, error: &error)
println(NSString(data: data!, encoding: NSUTF8StringEncoding))
{
"students" : [
{
"id" : "2",
"name" : "joe"
},
{
"id" : "3",
"name" : "jake"
},
{
"id" : "4",
"name" : "may"
},
{
"id" : "1",
"name" : "donna"
}
]
}
In nodeJS
I get the json like this:
{ 'students[][id]': [ '2', '3', '4', '1'],
'students[][name]': [ 'joe', 'jack', 'may', 'donna' ]
}
Edit: Afn code:
AFHTTPRequestOperationManager().POST("http://10.0.0.1:8080/echo", parameters: json,
success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
println(operation.responseString)
}, failure: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
})
In your question, you are taking your dictionary structure, and manually creating JSON. But when you create the request via AFNetworking, you're not specifying a JSON request, but rather a Content-Type
of application/x-www-form-urlencoded
because the default requestSerializer
is AFHTTPRequestSerializer
.
If you want to create JSON request in AFNetworking, you would:
let manager = AFHTTPRequestOperationManager()
manager.requestSerializer = AFJSONRequestSerializer(writingOptions: nil)
manager.POST("http://10.0.0.1:8080/echo", parameters: json, success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
println(operation.responseString)
}, failure: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
println(error)
})