-1

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.
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"                                                                                                                                                                                                                                       
)
 }
lifemoveson
  • 1,661
  • 8
  • 28
  • 55
  • Note that your JSON is invalid. There should be no comma after "CLASS_END". – Hot Licks Sep 02 '11 at 17:56
  • Sorry for the mistake. But my JSON is valid and works fine !! – lifemoveson Sep 02 '11 at 17:59
  • Post the results of `NSLog(@"%@", [resultsDictionary description]);` – Hot Licks Sep 02 '11 at 18:01
  • So you're not showing us the real JSON data? – Hot Licks Sep 02 '11 at 18:02
  • Ah, you've eliminated "array" with your latest edit. Are you editing the code as you do this, or just not showing us the code you're using? – Hot Licks Sep 02 '11 at 18:06
  • I am not showing you the real data and by mistake I typed it. I just need the concept if you can help me. Spying was not the motive of this forum rite ? – lifemoveson Sep 02 '11 at 18:07
  • Note that now you're reading the dictionary entrires from "dict" in random order. No reason to believe that what you get would be the "TITLE" value. – Hot Licks Sep 02 '11 at 18:09
  • You're floundering. THINK about what you're doing, don't just change statements randomly!! – Hot Licks Sep 02 '11 at 18:10
  • It's OK to not show the real data. But when you change the structure of the data (vs just changing names/values) then it's impossible for us to understand what you're doing. (And it wouldn't hurt you to put together a "dummy" test file that you use for testing and can show us, just to assure no slips in your copying.) – Hot Licks Sep 02 '11 at 18:13
  • So cant you read the dictionary entries in random order ? and I am changing since you said that way !!! I am working on such things for the first time and I expected some help rather than being so rude !! I am not here to know you nor you are here to know me. The forum is to help each other if you can else its ok. – lifemoveson Sep 02 '11 at 18:14
  • You can read the dictionary in random order, but it's meaningless to do so. A dictionary associates a name with a value, and if you lose the name what good is the value? – Hot Licks Sep 02 '11 at 18:16
  • I have added my NSLOG of my dictionary which I got from the file. – lifemoveson Sep 02 '11 at 18:22
  • Note that TITLE is an array. Use `NSArray* title = [dict objectForKey:@"TITLE"];` to get the TITLE array. Then you can (if you wish) use a `for (NSString* titleString in title)` loop to get the individual elements of TITLE. – Hot Licks Sep 02 '11 at 18:34
  • so you mean I have to have for loops for each tag in the dictionary ? – lifemoveson Sep 02 '11 at 19:32
  • Well, since the different tags seem to contain "columns" of the same table, with the array entries corresponding to "rows", I'd recommend a SINGLE loop -- `for (int i = 0; i < title.count; i++)`. Read the `i`th entry of each of your four inner arrays in one iteration of the loop, so you associate the right data items with each other. – Hot Licks Sep 02 '11 at 20:59
  • (Of you can get the limit of the loop from ROWCOUNT.) – Hot Licks Sep 02 '11 at 21:01
  • @Daniel: How will you read the ith entry for each row key ? – lifemoveson Sep 06 '11 at 16:07
  • Each column is represented by a different array in the dictionary. To get the individual cells of the `i`th entry you'd just index each of these arrays -- `objectAtIndex` if the parser returns a standard NSArray. – Hot Licks Sep 06 '11 at 17:48
  • @Daniel: Thank you I figured that out before. Thanks for the help. – lifemoveson Sep 06 '11 at 19:20

3 Answers3

1

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

0

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

crackity_jones
  • 1,077
  • 2
  • 13
  • 16
  • 1
    Isnt SBJSON work the same way because I tried it I didnt get any thing back as NSDictionary. – lifemoveson Sep 02 '11 at 16:53
  • I can't speak for SBJSON I haven't used it. Can you post the code you're using to try and get something back? Are you sure you're getting something back in your NSData object? You can check it to make sure by adding the following lines assuming that you are getting back the data from the web call into a NSData called data. NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"jsonString: %@", jsonString); [jsonString release]; Then see what gets logged. – crackity_jones Sep 02 '11 at 17:15
  • I have edited my question above with the errors I am receiving. – lifemoveson Sep 02 '11 at 17:53
0
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 "[".

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • Look at the results of the NSLog of "array". If your JSON was successfully parsed, and it really resembles what you show above. The NSLog output will show that "array" is an NSDictionary. (If it shows something different you're not telling us the truth about something.) – Hot Licks Sep 02 '11 at 18:04