Reputation: 1512
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
Upvotes: 0
Views: 5224
Reputation: 20410
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"]];
Upvotes: 4