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

发布于 2024-12-03 05:21:33 字数 2727 浏览 1 评论 0原文

我正在尝试解析从 ColdFusion 服务器生成的 SerializeJSON 格式的 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"                                                                                                                                                                                                                                       
)
 }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

强辩 2024-12-10 05:21:33

不知道您是否还需要答案,但已经找到了!从 Coldfusion 返回的 Json 确实是一个 NSArray。仅此而已,解析起来很复杂,

关键是将列名与值相匹配......

希望这会有所帮助。

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

}

/********************* *************** ******
*
* 私有实现部分
*
************* >********< em>*
**************< em>******/

pragma mark -

pragma mark 私有方法

/*-------------------------------------------------------- --------------------
*
------------------------------------------------------------ -------------/
- (void)连接:(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

}

的...这是格罗夫

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

装迷糊 2024-12-10 05:21:33
NSArray *array = [resultsDictionary objectForKey:@"DATA"];
NSLog(@"array : %@", array);

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

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

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 "[".

百思不得你姐 2024-12-10 05:21:33

使用像 https://github.com/TouchCode/TouchJSON 这样的库

,然后从你的冷库中获取数据融合服务器做类似的事情;

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

这将为您提供一个带有 json 对象的字典。

有一些方法可以使用 CJSONSerializer 走向另一个方向

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文