这是我的问题:
INPUT(来自某个网站):
{"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 :
(
)
我现在迷路了,我真的不知道发生了什么......如果你们对这个问题有任何线索,我很高兴你能帮助我!
由于
答案 0 :(得分:4)
您正在从第一个字典(dict)中获取“数据”数组,您应该从resultField获取它:
NSArray* dataField = [[NSArray alloc] initWithArray:[resultField objectForKey:@"data"]];