有没有办法在目标C中解析serializeJSON文件
我正在尝试解析从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不知道您是否还需要答案,但已经找到了!从 Coldfusion 返回的 Json 确实是一个 NSArray。仅此而已,解析起来很复杂,
关键是将列名与值相匹配......
希望这会有所帮助。
//这里写函数
- (NSString*) getQueryValue:(NSArray*)queryData queryColumns:(NSArray*)querycolumns queryColumn:(NSString*)querycolumn {
}
/********************* *************** ******
*
* 私有实现部分
*
************* >********< em>***************< em>******/
pragma mark -
pragma mark 私有方法
/*-------------------------------------------------------- --------------------
*
------------------------------------------------------------ -------------/
- (void)连接:(NSURLConnection *)连接 didReceiveData:(NSData *)数据
{
// 将传入的数据存储到字符串中
NSString *jsonString = [[NSString alloc] initWithData:数据编码:NSUTF8StringEncoding];
是
的...这是格罗夫
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 {
}
/******************************************
*
* 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];
}
Yeap...It's Grov
“DATA”条目是一个“对象”,而不是数组。如果您查看记录的内容,您会发现它正在记录字典。
JSON“对象”以“{”开头,而数组以“[”开头。
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 "[".
使用像 https://github.com/TouchCode/TouchJSON 这样的库
,然后从你的冷库中获取数据融合服务器做类似的事情;
这将为您提供一个带有 json 对象的字典。
有一些方法可以使用 CJSONSerializer 走向另一个方向
use a library like https://github.com/TouchCode/TouchJSON
then after grabbing the data from your cold fusion server do something like;
and this will give you a dictionary with the json object.
There are methods to go the other direction with CJSONSerializer