简体   繁体   中英

Parse combination of json nested objects and arrays

[
    {
        "Profile": {
            "id": "13",
            "user_id": "13",
            "first_name": "samm",
            "profile_image": "13-IMG_169.png",
            "where_from": "abro",
            "where_live": "simba",
            "age": "24",
            "profile_type": null,
            "company_name": "nick",
            "job_title": "developer",
            "industry": "software",
            "education": "bscs",
            "what_you_do": "developement",
            "detail_summery": "summary",
            "location": null,
            "state_id": "1",
            "city_id": "84",
            "favorite_music_bands": "",
            "favorite_teams": "",
            "favorite_books": "",
            "favorite_movies": null,
            "small_intro": "developer"
        }
    }
],
{
    "LookingData": [
        {
            "user_looking_id": "675",
            "looking_id": "1",
            "looking_text": "Expand Professional network"
        },
        {
            "user_looking_id": "456",
            "looking_id": "2",
            "looking_text": "Prospect new business / sales"
        },
        {
            "user_looking_id": "453",
            "looking_id": "3",
            "looking_text": "Share trade expertise / stories"
        },
        {
            "user_looking_id": "123",
            "looking_id": "5",
            "looking_text": "Recruitment"
        },
        {
            "user_looking_id": "654",
            "looking_id": "6",
            "looking_text": "Seeking business partner"
        },
        {
            "user_looking_id": "123",
            "looking_id": "7",
            "looking_text": "Advise"
        }
    ]
},
{
    "MusicData": [
        {
            "user_music_id": "54",
            "music_id": "2",
            "music_name": "Country"
        }
    ]
},
{
    "SportData": [
        {
            "user_sport_id": "234",
            "sport_id": "4",
            "sport_name": "Hockey"
        }
    ]
},
{
    "HobbyData": []
},
[],
{
    "MovieData": [
        {
            "user_movie_id": "645",
            "movie_id": "6",
            "movie_name": "Drama"
        }
    ]
},
[],
{
    "CarrerData": [
        {
            "user_carrer_id": "34",
            "carrer_id": "2",
            "carrer_name": "Marketing"
        },
        {
            "user_carrer_id": "645",
            "carrer_id": "8",
            "carrer_name": "Sales"
        }
    ]
}
]

get json Array using this line...

NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data //1
                                                                     options:0
                                                                       error:&error];

and get profile data using for loop...

for (NSDictionary *obj in [responseDict valueForKey:@"Profile"])
{
model.userProfileObj.userFirstName = [NSString stringWithFormat:@"%@",[obj valueForKey:@"first_name"]];
}

but in this case loop run many of time and assign null value to variables.

after this I want to parse "LookingData" array but I got null value using this code:

NSDictionary *dictUserLooking=[responseDict valueForKey:@"LookingData"];

What you have (assuming the missing outer [] ) is a JSON array containing elements that are either arrays or "objects" (dictionaries). The "Profile" data is the contained in the first element of the outer-most array, in this fashion:

outermost array -> single element array -> single element dictionary -> dictionary element for "Profile" -> dictionary containing "Profile" values.

You need to "peel the onion", one layer at a time, to access these values.

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