how to parse array of objects using json for iphone

后端 未结 2 1037
太阳男子
太阳男子 2020-12-30 14:17

I have a problem parsing the array of objects from the JSON result.

[
    {
        \"first_name\":\"vijay\",
        \"last_name\":\"last\",
        \"credi         


        
相关标签:
2条回答
  • 2020-12-30 14:25

    Google "JSON Framework". Follow the (easy) instructions to install it.

    Then go:

    //let's say there's NSString *jsonString.
    
    NSArray *userData = [jsonString JSONValue];
    
    NSMutableArray *creditCards = [NSMutableArray array];
    for (NSDictionary *user in userData) {
        [creditCards addObject:[user objectForKey:@"creditCardNumber"]];
    }
    

    You'll drop out the bottom of that with NSMutableArray *creditCards full of NSString objects containing the credit card numbers.

    0 讨论(0)
  • 2020-12-30 14:33

    @Dan Ray answer is correct, but if you want to avoid third-party librairies you can use NSJSONSerialization:

    Assuming that NSData *responseData is containing your JSON.

    NSArray *userData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];
    
    NSMutableArray *creditCards = [NSMutableArray array];
    for (NSDictionary *user in userData) {
        [creditCards addObject:[user objectForKey:@"creditCardNumber"]];
    }
    

    Source: NSJSONSerialization.

    0 讨论(0)
提交回复
热议问题