gpt4 book ai didi

ios - ios json解析成字典

转载 作者:行者123 更新时间:2023-11-29 04:09:43 24 4
gpt4 key购买 nike

我有以下 PHP 脚本:

<?php $sender = $_POST['sender'];
$rcpt = $_POST['rcpt'];
$message = $_POST['message'];

$someArray = array("Bannana", "Apple", "SomeCheese");
print_r($someArray);

echo json_encode($someArray);

?>

以及以下按钮操作:

NSString *myRequestString = @"sender=my%20sender&rcpt=my%20rcpt&message=hello";
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString: @"http://development.com/ios/responseScript.php" ] ];

/**********Set Request properties*************/
[ request setHTTPMethod: @"POST" ];
[ request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[ request setHTTPBody: myRequestData ];


NSURLResponse *response;
NSError *err;

NSDictionary *returnedDictionary = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSUInteger content = [NSString stringWithUTF8String:[returnedDictionary count]];
//NSLog(@"responseData: %@", content);
self.responseLabel.text = [NSString stringWithFormat:@"Count: %u", content];

我想要实现的是被回显的 json 应该以响应的形式返回并打印到屏幕上。充其量我知道返回值为空,并且确信它不是。我有一个工作示例,但使用数组而不是字典,结果附在下面:

NSString *myRequestString = @"sender=my%20sender&rcpt=my%20rcpt&message=hello";
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString: @"http://development.com/ios/responseScript.php" ] ];
[ request setHTTPMethod: @"POST" ];
[ request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[ request setHTTPBody: myRequestData ];
NSURLResponse *response;
NSError *err;
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
//NSLog(@"responseData: %@", content);
self.responseLabel.text = [NSString stringWithFormat:@"Data: %@", content];

结果:

2013-01-30 19:07:07.919 testResponse[15921:c07] responseData: Array ( [0] => Bannana [1] => Apple [2] => SomeCheese ) ["Bannana","Apple","SomeCheese"]

请帮忙?非常感谢

最佳答案

此行有问题:

NSDictionary *returnedDictionary = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

sendSynchronousRequest:returningResponse:error: 返回 NSData,而不是 NSDictionary。

阅读documentation ,请确保您知道自己正在处理的类型,祝您好运。

关于ios - ios json解析成字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14608456/

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