Is there a way to parse serializeJSON file in objective C
I am trying to parse JSON file generated from ColdFusion server in SerializeJSON format. Is there any specific way to parse the serializeJSON file. It is different than normal Twitter Feed JSON file. How to parse the JSON file in such a format ? I am using SBJSON File for parsing this.
{
"ROWCOUNT": 2,
"COLUMNS": [
"ID",
"TITLE",
"CLASS_START",
"CLASS_END",
],
"DATA": {
"KEY_ID": [
"a11c1a361a38",
"6be127103538"
],
"TITLE": [
"Test ",
"Test2 "
],
"CLASS_START": [
"October, 25 2011 00:00:00",
"October, 26 2011 14:47:00"
],
"CLASS_END": [
"October, 25 2011 00:00:00",
"October, 27 2011 14:47:00"
]
}
}
CODE TO PARSE:
NSString *jsonString = [self jsonFromURLString:urlString];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSLog(@"dATA : %@", jsonData);
// Parse JSON results with TouchJSON. It converts it into a dictionary.
开发者_JAVA百科CJSONDeserializer *jsonDeserializer = [CJSONDeserializer deserializer];
NSError *error = nil;
NSDictionary *resultsDictionary = [jsonDeserializer deserializeAsDictionary:jsonData error:&error];
[self handleError:error];
NSDictionary *dict = [resultsDictionary objectForKey:@"DATA"];
NSLog(@"dict : %@", dict);
for (NSArray *data in dict) {
NSDictionary *title = [data objectAtIndex:0]; /**** Errors here saying [NSCFString objectforkey] not recognised was getting the same error before too****/
NSLog(@"Title : %@", title);
}
Output of my Dictionary:
dict : {
"CLASS_END" = (
"October, 25 2011 00:00:00",
"October, 27 2011 14:47:00"
);
"CLASS_START" = (
"October, 25 2011 00:00:00",
"October, 26 2011 14:47:00"
);
"KEY_ID" = (
"a11c1a361a38",
"6be127103538"
);
TITLE = (
"Test",
"Test2"
)
}
Don't know if you still need the answer but figured it out! The Json return from Coldfusion is truly a NSArray. Nothing more, nothing less but complicated to Parse
The key is matching up the Column Names, with the values...
Hope fully this will help.
//Write Function HERE - (NSString*) getQueryValue:(NSArray*)queryData queryColumns:(NSArray*)querycolumns queryColumn:(NSString*)querycolumn {
NSString *arrayValue;
NSString *returnValue = nil;
//Loop Through Query Columns To Find Node
for(int i=0; i< [querycolumns count];i++){
arrayValue = [NSString stringWithFormat:@"%@",[querycolumns objectAtIndex:i]]; //Cast Value To String
NSRange searchResult = [arrayValue rangeOfString:querycolumn options:NSCaseInsensitiveSearch];
if(searchResult.location != NSNotFound){
//Found NODE NOW BREAK
return [NSString stringWithFormat:@"%@",[queryData objectAtIndex:i]];
break;
}
}
return returnValue;
}
/****************************************** * * Private implementation section * ******************************************/
pragma mark -
pragma mark Private Methods
/*------------------------------------------------------------- * ------------------------------------------------------------/ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // Store incoming data into a string NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// Create a dictionary from the JSON string
NSDictionary *results = [jsonString JSONValue];
// Build an array from the dictionary for easy access to each entry
NSArray *categoryKeys = [results objectForKey:@"COLUMNS"];
NSArray *categoryArray = [results objectForKey:@"DATA"];
NSLog(@"Category Count from json...%i",[categoryArray count]);
// Loop through each entry in the dictionary...
for (NSArray *category in categoryArray)
{
// Get title of the image
NSString *categoryname = [self getQueryValue:category queryColumns:categoryKeys queryColumn:@"categoryname"]; //Case Insensative
NSString *categoryid = [self getQueryValue:category queryColumns:categoryKeys queryColumn:@"categoryid"]; //Case Insensative
}
}
Yeap...It's Grov
NSArray *array = [resultsDictionary objectForKey:@"DATA"];
NSLog(@"array : %@", array);
The "DATA" entry is an "object", not an array. If you look at what was logged you'll see that it's logging a dictionary.
JSON "objects" start with "{", while arrays start with "[".
use a library like https://github.com/TouchCode/TouchJSON
then after grabbing the data from your cold fusion server do something like;
NSDictionary *jsonDictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:&error];
and this will give you a dictionary with the json object.
There are methods to go the other direction with CJSONSerializer
精彩评论