有没有办法在目标C中解析serializeJSON文件

时间:2011-09-02 16:05:43

标签: iphone objective-c json sbjson

我正在尝试以SerializeJSON格式解析从ColdFusion服务器生成的JSON文件。是否有任何特定的方法来解析serializeJSON文件。它与普通的Twitter Feed JSON文件不同。如何以这种格式解析JSON文件?我正在使用SBJSON文件来解析它。

{
"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"
    ]

}
}

代码:

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);
}

我的词典输出:

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"                                                                                                                                                                                                                                       
)
 }

3 个答案:

答案 0 :(得分:1)

不知道你是否还需要答案,但想出来了!来自Coldfusion的Json回归真的是一个NSArray。没什么,没有什么比Parse更复杂了

关键是匹配列名称,值为......

希望完全有所帮助。

//在这里写函数 - (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;

}

/ <强> * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * **  *  *私人执行部分  *  的 * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** /

pragma mark -

pragma mark私有方法

/ * --------------------------------------------- ----------------  *   ----------------------------------------------- ------------- / - (void)connection:(NSURLConnection *)连接didReceiveData:(NSData *)数据 {     //将传入的数据存储到字符串中     NSString * jsonString = [[NSString alloc] initWithData:数据编码: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 ......这是Grov

答案 1 :(得分:0)

使用像https://github.com/TouchCode/TouchJSON

这样的库

然后从冷融合服务器获取数据后执行类似的操作;

NSDictionary *jsonDictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:&error];

这将为您提供一个包含json对象的字典。

有一些方法可以使用CJSONSerializer

进入另一个方向

答案 2 :(得分:0)

NSArray *array = [resultsDictionary objectForKey:@"DATA"];
NSLog(@"array : %@", array);

“DATA”条目是“对象”,而不是数组。如果您查看记录的内容,您会看到它正在记录字典。

JSON“对象”以“{”开头,而数组以“[”。

开头