简体   繁体   中英

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. The PHP array is created like this:

$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:

2013-02-05 19:17:37.910 testProducts[26786:c07] Array Retrieved: { Jane = Student; Jhonson = 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. So on for all other pairs.

How can this be done?

I am new to iOS development :)

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"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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