JSON 到 NSDictionary 到 NSArray 给我空 NSArray ? :(

发布于 2025-01-01 08:07:29 字数 3104 浏览 0 评论 0原文

这是我的问题:

输入(来自某个网​​站):

{"error":[],"results":{"count":2,"data":[{"ART_ID":"656","ART_NAME":"Noir D\u00e9sir","ART_PICTURE":".....","NB_ALBUM":"15","NB_FAN":"90176","SMARTRADIO":"1","URL_REWRITING":"noir-desir"},{"ART_ID":"167225","ART_NAME":"LMFAO","ART_PICTURE":"....","NB_ALBUM":"17","NB_FAN":"122408","SMARTRADIO":"1","URL_REWRITING":"lmfao"}],"total":2}}

所以,一旦我有了它,我就会使用一些 JSON 类:

 NSError* jsonError = nil;
 //responseData contains my json feed
 NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonError];

 NSLog(@"responseDictionary : \n%@\n--------\n", [dict description]);

为了获得以下内容:

{
    error =     (
    );
    results =     {
        count = 2;
        data =         (
                        {
                "ART_ID" = 656;
                "ART_NAME" = "Noir D\U00e9sir";
                "ART_PICTURE" = 17b0a9c3defcf8c3eef943759606b3f4;
                "NB_ALBUM" = 15;
                "NB_FAN" = 90176;
                SMARTRADIO = 1;
                "URL_REWRITING" = "noir-desir";
            },
                        {
                "ART_ID" = 167225;
                "ART_NAME" = LMFAO;
                "ART_PICTURE" = 72a09b9202da38afae077e80c7598212;
                "NB_ALBUM" = 17;
                "NB_FAN" = 122408;
                SMARTRADIO = 1;
                "URL_REWRITING" = lmfao;
            }
        );
        total = 2;
    };
}

在这里,我真的没有任何问题......但现在,我想获取这个 NSDictionary 中的“数据”字段。所以我只是创建一个新的:

NSDictionary* resultField = [NSDictionary dictionaryWithDictionary:[dict objectForKey:@"results"]];

其中包含:

{
    count = 2;
    data =     (
                {
            "ART_ID" = 656;
            "ART_NAME" = "Noir D\U00e9sir";
            "ART_PICTURE" = 17b0a9c3defcf8c3eef943759606b3f4;
            "NB_ALBUM" = 15;
            "NB_FAN" = 90176;
            SMARTRADIO = 1;
            "URL_REWRITING" = "noir-desir";
        },
                {
            "ART_ID" = 167225;
            "ART_NAME" = LMFAO;
            "ART_PICTURE" = 72a09b9202da38afae077e80c7598212;
            "NB_ALBUM" = 17;
            "NB_FAN" = 122408;
            SMARTRADIO = 1;
            "URL_REWRITING" = lmfao;
        }
    );
    total = 2;
}

现在我想获取我的“数据”字段,它现在是一个 JSONArray (不再是 NSDictionary...):

NSArray* dataField = [[NSArray alloc] initWithArray:[dict objectForKey:@"data"]];

但在那之后我的 dataField 是空的...意思是这样的:

NSArray* dataField = [[NSArray alloc] initWithArray:[dict objectForKey:@"data"]];
if (!dataField || ![dataField isKindOfClass:[NSArray class]]) {
    NSLog(@"Pb parsing DATA field as NSArray...");
}
NSLog(@"dataField : \n%@\n--------\n", [dataField description]);

不会向我显示错误,因为它是格式正确的 NSArray,但会打印以下内容:

2012-02-02 14:08:52.315 myapp[5235:f803] dataField : 
(
)

我现在迷路了,我真的不知道发生了什么......如果你们对这个问题有任何线索,我'如果可以的话我会很高兴 帮我 !

谢谢

Here is my problem :

INPUT (from a certain website) :

{"error":[],"results":{"count":2,"data":[{"ART_ID":"656","ART_NAME":"Noir D\u00e9sir","ART_PICTURE":".....","NB_ALBUM":"15","NB_FAN":"90176","SMARTRADIO":"1","URL_REWRITING":"noir-desir"},{"ART_ID":"167225","ART_NAME":"LMFAO","ART_PICTURE":"....","NB_ALBUM":"17","NB_FAN":"122408","SMARTRADIO":"1","URL_REWRITING":"lmfao"}],"total":2}}

So, once I have that I'm using a few JSON classes :

 NSError* jsonError = nil;
 //responseData contains my json feed
 NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonError];

 NSLog(@"responseDictionary : \n%@\n--------\n", [dict description]);

In order to obtain the following :

{
    error =     (
    );
    results =     {
        count = 2;
        data =         (
                        {
                "ART_ID" = 656;
                "ART_NAME" = "Noir D\U00e9sir";
                "ART_PICTURE" = 17b0a9c3defcf8c3eef943759606b3f4;
                "NB_ALBUM" = 15;
                "NB_FAN" = 90176;
                SMARTRADIO = 1;
                "URL_REWRITING" = "noir-desir";
            },
                        {
                "ART_ID" = 167225;
                "ART_NAME" = LMFAO;
                "ART_PICTURE" = 72a09b9202da38afae077e80c7598212;
                "NB_ALBUM" = 17;
                "NB_FAN" = 122408;
                SMARTRADIO = 1;
                "URL_REWRITING" = lmfao;
            }
        );
        total = 2;
    };
}

Here, I don't really have any problem... But now, I want to get the "data" field in this NSDictionary. So I just create a new one :

NSDictionary* resultField = [NSDictionary dictionaryWithDictionary:[dict objectForKey:@"results"]];

Wich contains :

{
    count = 2;
    data =     (
                {
            "ART_ID" = 656;
            "ART_NAME" = "Noir D\U00e9sir";
            "ART_PICTURE" = 17b0a9c3defcf8c3eef943759606b3f4;
            "NB_ALBUM" = 15;
            "NB_FAN" = 90176;
            SMARTRADIO = 1;
            "URL_REWRITING" = "noir-desir";
        },
                {
            "ART_ID" = 167225;
            "ART_NAME" = LMFAO;
            "ART_PICTURE" = 72a09b9202da38afae077e80c7598212;
            "NB_ALBUM" = 17;
            "NB_FAN" = 122408;
            SMARTRADIO = 1;
            "URL_REWRITING" = lmfao;
        }
    );
    total = 2;
}

Now I want to get my "data" field, which now is a JSONArray (not a NSDictionary anymore...) :

NSArray* dataField = [[NSArray alloc] initWithArray:[dict objectForKey:@"data"]];

But after that my dataField is empty... By that I mean this :

NSArray* dataField = [[NSArray alloc] initWithArray:[dict objectForKey:@"data"]];
if (!dataField || ![dataField isKindOfClass:[NSArray class]]) {
    NSLog(@"Pb parsing DATA field as NSArray...");
}
NSLog(@"dataField : \n%@\n--------\n", [dataField description]);

Doesn't show me the error since it a correctly formatted NSArray, but prints me the following :

2012-02-02 14:08:52.315 myapp[5235:f803] dataField : 
(
)

I now am lost, I really don't know what's happening... If you guys have any clue on this issue I'd be glad if you could help me !

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

快乐很简单 2025-01-08 08:07:29

您正在从第一个字典(dict)中获取“数据”数组,您应该从 resultField 中获取它:

NSArray* dataField = [[NSArray alloc] initWithArray:[resultField objectForKey:@"data"]];

You are taking the "Data" Array from the first dictionary (dict), you should take it from resultField:

NSArray* dataField = [[NSArray alloc] initWithArray:[resultField objectForKey:@"data"]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文