简体   繁体   中英

Parse nested Json and then insert into uitableview

So I am trying to parse this json data:

        {
    deal = Test;
    expires = "2012-07-29 22:50:30";
    "is_active" = 1;
    "id" = 27;
    users =         (
                    {
            distance = 0;
            latitude = "41.312618";
            longitude = "-74.195114";
            "user_id" = 34;
        },
                    {
            distance = 0;
            latitude = "41.312618";
            longitude = "-74.195114";
            "user_id" = 35;
        },
                    {
            distance = 0;
            latitude = "41.312618";
            longitude = "-74.195114";
            "user_id" = 36;
        },
                    {
            distance = 0;
            latitude = "41.312618";
            longitude = "-74.195114";
            "user_id" = 38;
        }
    );
},
    {
    deal = Testing;
    expires = "2012-07-29 23:37:38";
    "is_active" = 0;
    "id" = 26;
    users =         (
                    {
            distance = 0;
            latitude = "41.312618";
            longitude = "-74.195114";
            "user_id" = 37;
        }
    );
}

So I want the header of each section to be deal and then the rows to be each of the users associated with that deal. So for the first deal "Test" there will be four rows with user 34, 35, 36, and 38. Then for the second deal "Testing" there is only one user so only one row. This needs to be able to work for an unlimited number of deals. I can parse all of the deals into one array but I am having trouble figuring out how to separate the users. Please help!

EDIT:

This is how I am getting the Json data and storing it and the deals.

NSURL *loginURL = [NSURL URLWithString: [NSString stringWithFormat: URL];   

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:loginURL];
[theRequest setHTTPMethod:@"POST"];  

NSError *error;
NSURLResponse *response;

NSData * responseData=[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];  

NSDictionary * luckyNumbers = [NSJSONSerialization JSONObjectWithData:responseData //1                                                          
                                                              options:NSJSONReadingAllowFragments 
                                                                error:&error];  
NSLog(@" %@", luckyNumbers);

NSArray * array1 = [luckyNumbers objectForKey:@"active"]; //THIS IS THE JSON THAT IS DISPLAYED ABOVE

deals = [[NSMutableArray alloc] init];

active = [[NSMutableArray alloc] init];

for (NSDictionary *item in array1)
{
    [deals addObject:item]; // THIS WORKS FINE. IT RETURNS ALL OF THE DEALS IN AN ARRAY WHICH I SET TO THE NUMBER OF SECTIONS IN TABLE AND THE HEADER TITLE OF THE SECTION
    [active addObject:[item objectForKey:@"users"]];//THIS IS THE PROBLEM. 
}

EDIT 2:

So this is still giving me issues. I am able to parse the deals perfectly fine and display them as the header for each section. When I parse the users into an array called active It is an array as shown below. The first four are part of deal "test" and the last is part of deal "Testing". I'm not sure how to set the first four users to be in section 0, with the correct number of rows in that section and display them correctly in the tableview. And then repeat this for the next sections. This needs to work for X number of deals with Y number of users. I hope this makes sense, Please help!

  (
                {
        distance = 0;
        latitude = "41.312618";
        longitude = "-74.195114";
        "user_id" = 34;
    },
                {
        distance = 0;
        latitude = "41.312618";
        longitude = "-74.195114";
        "user_id" = 35;
    },
                {
        distance = 0;
        latitude = "41.312618";
        longitude = "-74.195114";
        "user_id" = 36;
    },
                {
        distance = 0;
        latitude = "41.312618";
        longitude = "-74.195114";
        "user_id" = 38;
    }
),
(
                {
        distance = 0;
        latitude = "41.312618";
        longitude = "-74.195114";
        "user_id" = 37;
    }
);
var yourDataHolder = JSON.parse(yourJSONObject);
for(var x in yourDataHolder)
{
if(yourDataHolder[x].hasOwnProperty)
{
/**
*   DO STUFF HERE
*/
console.log(yourDataHolder[x].deal);
console.log(yourDataHolder[x].expires);
}
}

To make it easier for you to understand here is what happens: yourDataHolder[x] will iterate over the different items in your JSON object. To access the properties of your object, use yourDataHolder[x].deal,yourDataHolder[x].expires,yourDataHolder[x].is_active,yourDataHolder[x].id

To access the data inside the user table, do this: var yourDataHolder = JSON.parse(yourJSONObject);

for(var x in yourDataHolder)
{
if(yourDataHolder[x].hasOwnProperty)
{
/**
*   DO STUFF HERE
*/
for(var y in yourDataHolder[x].users)
{
   if(yourDataHolder[x].users[y].hasOwnProperty)
        {
            /**
            *   You are now inside the current "users" object
            */
            console.log(yourDataHolder[x].users[y]);
        }
}
}
}

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