2
[
    {
        "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"
        }
    ]
}
]

この行を使用してjson配列を取得します...

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

forループを使用してプロファイルデータを取得します...

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

ただし、この場合、ループは何度も実行され、null 値が変数に割り当てられます。

この後、「LookingData」配列を解析したいのですが、次のコードを使用して null 値を取得しました。

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

1 に答える 1

0

あなたが持っているもの (outer が欠落していると仮定[]) は、配列または「オブジェクト」(辞書) のいずれかである要素を含む JSON 配列です。「プロファイル」データは、次のように、最も外側の配列の最初の要素に含まれています。

最も外側の配列 -> 単一要素配列 -> 単一要素辞書 -> 「プロファイル」の辞書要素 -> 「プロファイル」値を含む辞書。

これらの値にアクセスするには、一度に 1 つのレイヤーで「タマネギの皮をむく」必要があります。

于 2012-11-26T17:26:44.713 に答える