我從被格式化,像這樣一個URL獲取JSON數據:POST JSON數據到現有的對象
{"zoneresponse":
{"tasks":
[{"datafield1":"datafor1",
"datafield2":"datafor2",
"datafield3":"datafor3",...
}]
}}
我在結構上沒有控制,因爲它是從私有API。
如何在現有對象的選定數據字段中插入數據?
我曾嘗試這樣的:
self.responseData = [NSMutableData data];
//testingURL is the api address to the specific object in tasks
NSURL *url = [NSURL URLWithString:testingURL];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[[[params objectForKey:@"zoneresponse"] objectForKey:@"tasks"] setValue:@"HelloWorld" forKey:@"datafield1"];
//HAVE TRIED setObject: @"" objectForKey: @"" as well
//*****PARAMS IS EMPTY WHEN PRINTED IN NSLog WHICH IS PART OF THE ISSUE - SETTING VALUE DOES NOT WORK
NSError * error = nil;
NSLog(@"Params is %@", params);
NSData *requestdata = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];
NSMutableURLRequest *request;
request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [requestdata length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestdata];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn) {
NSLog(@"Connection Successful, connection is: %@", conn);
} else {
NSLog(@"Connection could not be made");
}
該連接正被提出,但在打印時(所述的setValue沒有顯示),並且不輸入任何數據到我選擇字段字典params爲空。
我檢查了這些鏈接,但沒有任何解釋它是否會插入到正確的字段中,並暗示它將創建新對象而不是更新現有對象。
How to update data on server db through json api?
How to send json data in the Http request using NSURLRequest
委託方法
//any time a piece of data is received we will append it to the responseData object
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.responseData appendData:data];
NSError *jsonError;
id responseDict =
[NSJSONSerialization JSONObjectWithData:self.responseData
options:NSJSONReadingAllowFragments
error:&jsonError];
NSLog(@"Did Receive data %@", responseDict);
}
//if there is some sort of error, you can print the error or put in some other handling here, possibly even try again but you will risk an infinite loop then unless you impose some sort of limit
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// Clear the activeDownload property to allow later attempts
self.responseData = nil;
NSLog(@"Did NOT receive data ");
}
//connection has finished, the requestData object should contain the entirety of the response at this point
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *jsonError;
id responseDict =
[NSJSONSerialization JSONObjectWithData:self.responseData
options:NSJSONReadingAllowFragments
error:&jsonError];
if(responseDict)
{
NSLog(@"%@", responseDict);
}
else
{
NSLog(@"%@", [jsonError description]);
}
//clear out our response buffer for future requests
self.responseData = nil;
}
這裏的第一個方法,指出數據與「的確收到數據(空)」收到,有但是與連接沒有錯誤final方法打印錯誤消息「JSON文本沒有以數組或對象開始,選項允許片段未設置」,這是可以理解的,因爲沒有數據或對象被髮送。
如何將數據插入到現有對象的選定字段中?
在哪裏以及你如何啓動** **康涅狄格州?我的意思是[conn start] –
@VivekMolkar我有一個(IBAction)方法的內部,我假設它根據我的返回方法自動連接 - (void)連接:(NSURLConnection *)連接didReceiveData:(NSData *)data' (NSURLConnection *)連接didFailWithError:(NSError *)error'和' - (void)connectionDidFinishLoading:(NSURLConnection *)connection' –
不,您必須將一行代碼'[conn start ]'啓動連接。 ** conn **本身不會啓動。 –