Pregunta

Below is a content located at json.txt

{
    "data": [
        {
            "keyId": 3,
            "title": "This is a fundraiser 1",
            "budget": "1000",
            "users": {
                "user": [
                    {
                        "id": "3",
                        "first_name": "A1",
                        "last_name": "A11",
                        "is_owner": "false"
                    },
                    {
                        "id": "2",
                        "first_name": "B1",
                        "last_name": "B11",
                        "is_owner": "true"
                    }
                ]
             }      
        }
    ]
}

What I am doing to get idKey and title and budget is

@implementation Account
@synthesize arr;
-(void)parse {
             NSString *filePath    =   [[NSBundle mainBundle] pathForResource:@"fundraiser_json" ofType:@"txt"];
             NSString *jsonString  =   [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

             NSDictionary    *dict =   [jsonString objectFromJSONString];
             self.arr              =   [dict objectForKey:@"data"];

             for ( NSDictionary  *acc in self.arr ) {

                 Account  *account     =   [[Account alloc] init];

                 [account setKeyID:   [acc objectForKey:@"keyId"]];
                 [account setTitle:   [acc objectForKey:@"title"]];
                 [account setBudget:  [acc objectForKey:@"budget"]]; 
             } 
    }       

Then I am trying to access users and get some info of each user in it but I cant

Can somebody help me how to access and get these data.

¿Fue útil?

Solución

The way to access your user data is the same idea as how you are accessing your account data. In the nested structure of your JSON object, users is a property of your account. That object is a dictionary that has one property, "user", which is an array. Each element of that array is an object that has four properties: id, first_name, last_name, and is_owner.

So when you parse your object, it is just a bunch of nested NSDictionaries and NSArrays. You can do it like this:

...
for ( NSDictionary  *acc in self.arr ) {

    NSDictionary *users = (NSDictionary*)[acc objectForKey:@"users"];
    NSArray *userArray = (NSArray*)[users objectForKey:@"user"];

    for (NSDictionary *user in userArray) {
        NSString *id = [user objectForKey:@"id"];
        NSString *firstName = [user objectForKey:@"first_name"];
        // etc.
    }
 }

EDIT: I wrote this answer to parse your JSON the way you have it in your question, but if you have control of this structure you'd be better off (and it would make more sense) for the users property to be an array that directly contains the user data dictionaries. Perhaps that's what you meant to do in the first place and why you were having trouble getting the data.

Otros consejos

When you convert the string into JSON object, it makes a tree-stuctured data-type, with mutable dictionaries and arrays. The NSJSONSerialization Class Documentation summarizes it pretty well.

I suppose if you want to access for example, the first name of the first user in the list, you would need to write the method call:

[[[[acc objectForKey:@"users"] objectForKey:@"user"] objectForKey:@"first_name"] objectAtIndex:0];

I'm not sure though that I understood your problem or it solves it.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top