简体   繁体   English

用键正确地将数组内容存储到字典中

[英]storing array content into dictionary with keys properly

I have this: 我有这个:

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);

It clearly gets json data from site. 它显然从站点获取json数据。 The PHP array is created like this: 这样创建PHP数组:

$simpleProduct = array("John"=>"Employee","Jane"=>"Student","Jhonson"=>"Somejob","Mike"=>"Nothing");
echo json_encode($simpleProduct);

and when the result of objective-c code above is printed, it shows like: 当上面的objective-c代码的结果被打印时,它显示为:

2013-02-05 19:17:37.910 testProducts[26786:c07] Array Retrieved: { Jane = Student; 2013-02-05 19:17:37.910 testProducts [26786:c07]检索到的数组:{Jane = Student; Jhonson = Somejob; 琼森= Somejob; John = Employee; 约翰=员工; Mike = Nothing; 迈克=没什么; } }

Now I wish to have this data inserted into dictionary (NSMutableDictionary) so that for eg Jane is key and Student is value. 现在,我希望将此数据插入字典(NSMutableDictionary)中,以便例如Jane是键,而Student是值。 So on for all other pairs. 对于所有其他对,依此类推。

How can this be done? 如何才能做到这一点?

I am new to iOS development :) 我是iOS开发的新手:)

why not you just get the results like this 为什么不只是得到这样的结果

NSDictionary *someDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

it makes your life easier, and after just access the values via keys like this 它使您的生活更加轻松,并且只需通过如下键即可访问值

NSString *jane= [someDictionary objectForKey:@"Jane"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM