objective c - Parse JSON Post with ISODate Object mongoDB - Node.js -
i'm trying parse json document mongodb isodate...but field string , not isodate. how can send json isodate without change node.js post method receive json , save in collection
- (nsdictionary*) todictionary { nsmutabledictionary* jsonable = [nsmutabledictionary dictionary]; nsstring *dataeventoiso = [nsstring stringwithformat:@"isodate(%@)",self.dataevento]; safeset(jsonable, @"name", self.name); safeset(jsonable, @"placename", self.placename); safeset(jsonable, @"location", self.location); safeset(jsonable, @"details", self.details); safeset(jsonable, @"imageid", self.imageid); safeset(jsonable, @"categories", self.categories); safeset(jsonable, @"_id", self._id); safeset(jsonable, @"usuarioid", self.usuarioid); safeset(jsonable, @"status", self.status); safeset(jsonable, @"emailusuario", self.emailusuario); safeset(jsonable, @"dataevento", dataeventoiso); return jsonable; }
and method post:
- (void) persist:(location*)location { appdelegate *appdelegate = (appdelegate *)[[uiapplication sharedapplication]delegate]; if (!location || location.name == nil || location.name.length == 0) { return; //input safety check } //if there image, save first if (location.image != nil && location.imageid == nil) { //1 [self savenewlocationimagefirst:location]; //2 return; } nsstring* locations = [kbaseurl stringbyappendingpathcomponent:klocations]; bool isexistinglocation = location._id != nil; if(!isexistinglocation) location.votos = @"0"; nsstring *urlstr = isexistinglocation ? [locations stringbyappendingpathcomponent:location._id] : locations; nsurl* url = [nsurl urlwithstring:urlstr]; //1 nsmutableurlrequest* request = [nsmutableurlrequest requestwithurl:url]; request.httpmethod = isexistinglocation ? @"put" : @"post"; //2 location.usuarioid = appdelegate.usuariologado.identificador; if([location.status length] < 1 || [location.status isequaltostring:@"(null)"]) location.status = @"0"; location.emailusuario = appdelegate.usuariologado.email; nsdata* data = [nsjsonserialization datawithjsonobject:[location todictionary] options:0 error:null]; //3 request.httpbody = data; [request addvalue:@"application/json" forhttpheaderfield:@"content-type"]; //4 nsurlsessionconfiguration* config = [nsurlsessionconfiguration defaultsessionconfiguration]; nsurlsession* session = [nsurlsession sessionwithconfiguration:config]; nsurlsessiondatatask* datatask = [session datataskwithrequest:request completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { //5 if (!error) { nsarray* responsearray = @[[nsjsonserialization jsonobjectwithdata:data options:0 error:null]]; [self parseandaddlocations:responsearray toarray:self.objects]; dispatch_group_leave([appdelegate appdelegate].grupo_persist); } }]; [datatask resume]; }
i edited rest api , works great!
collectiondriver.prototype.save = function(collectionname, obj, callback) { this.getcollection(collectionname, function(error, the_collection) { //a if( error ) callback(error) else { if(obj.dataevento) obj.dataevento = new date(obj.dataevento); the_collection.insert(obj, function() { //c callback(null, obj); }); } }); };
Comments
Post a Comment