我有这个:
NSURL *url = [NSURL URLWithString:@"http://www.ios.com/ios/responseScript.php"];
NSError *error = nil;
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:&error];
NSMutableArray *someArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(@"Array Retrieved: %@", someArray);
它清楚地从站点获取 json 数据。 PHP 数组的创建方式如下:
$simpleProduct = array("John"=>"Employee","Jane"=>"Student","Jhonson"=>"Somejob","Mike"=>"Nothing");
echo json_encode($simpleProduct);
当打印上面的 Objective-C 代码的结果时,它显示如下:
2013-02-05 19:17:37.910 testProducts[26786:c07] Array Retrieved: { Jane = Student; Jhonson = Somejob; John = Employee; Mike = Nothing; }
现在我希望将此数据插入到字典(NSMutableDictionary)中,以便例如Jane 是关键,Student 是值(value)。对于所有其他对,依此类推。
如何做到这一点?
我是 iOS 开发新手:)
最佳答案
为什么不直接得到这样的结果
NSDictionary *someDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
它让你的生活更轻松,只需通过这样的键访问值
NSString *jane= [someDictionary objectForKey:@"Jane"];
关于ios - 将数组内容正确存储到带有键的字典中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14712045/