gpt4 book ai didi

ios - 将 JSON 数据发布到现有对象中

转载 作者:IT王子 更新时间:2023-10-29 08:15:39 26 4
gpt4 key购买 nike

我正在从格式如下的 URL 中检索 JSON 数据:

{"zoneresponse":
{"tasks":
[{"datafield1":"datafor1",
"datafield2":"datafor2",
"datafield3":"datafor3",...
}]
}}

我无法控制结构,因为它来自私有(private) 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 未显示)并且未在我选择的字段中输入任何数据。

我已经检查了这些链接,但没有说明它是否会插入正确的字段并暗示它会创建一个新对象而不是更新现有对象。

How to update data on server db through json api?

How to send json data in the Http request using NSURLRequest

委托(delegate)方法

//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;
}

这里的第一个方法声明数据是用“Did Receive data (null)”接收的,连接没有错误,但是最后一个方法打印错误消息“JSON 文本没有以数组或对象和允许的选项开始fragments not set.”,这是可以理解的,因为没有数据或对象被发送。

如何将数据插入现有对象的选定字段?

最佳答案

你做错了:

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[[[params objectForKey:@"zoneresponse"] objectForKey:@"tasks"] setValue:@"HelloWorld" forKey:@"datafield1"];

您的 params 是空字典,[params objectForKey:@"zoneresponse"] 不会返回任何对象。

试试这个:

NSMutableDictionary *params = [NSMutableDictionary new];
params[@"zoneresponse"] = @{@"tasks": @{@"datafield1": @"HelloWorld"}};

这会起作用,但是键 @"tasks" 的对象将是不可变的。要将另一个对象添加到 tasks 字典,我们需要使其可变:

NSMutableDictionary *params = [NSMutableDictionary new];
NSMutableDictionary *tasks = [NSMutableDictionary new];
params[@"zoneresponse"] = @{@"tasks": tasks};
params[@"zoneresponse"][@"tasks"][@"datafield1"] = @"HelloWorld";

NSMutableDictionary *params = [NSMutableDictionary new];    
params[@"zoneresponse"] = @{@"tasks": [@{@"datafield1": @"HelloWorld"} mutableCopy]};

然后你可以添加另一个对象到tasks:

params[@"zoneresponse"][@"tasks"][@"datafield2"] = @"HelloWorld2";
params[@"zoneresponse"][@"tasks"][@"datafield3"] = @"HelloWorld3";

我认为我的回答会让您对字典的操作更加清晰。

关于ios - 将 JSON 数据发布到现有对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29669296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com