【发布时间】:2014-09-16 09:54:37
【问题描述】:
**我无法解析以下 JSON 文件。它给了the operation couldn’t be completed. (cocoa error 3840.)。
这是我的代码:
- (NSString *)loadJsFromFile:(NSString *)fileName
{
NSString *path = [[NSBundle mainBundle]pathForResource:fileName ofType:@"txt"];
NSString *jsScript = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
return jsScript;
}
//
NSString *strInputParam = [self loadJsFromFile:@"samplein"];
NSData *dataInput = [strInputParam dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSObject *input = [NSJSONSerialization JSONObjectWithData:dataInput options:NSJSONReadingMutableContainers error:&error];
if(error)
{
NSLog(@"%@",error.localizedDescription);
}
我也尝试了以下方法,但没有帮助,请帮我解析这个 JSON。提前谢谢。
NSString *path = [[NSBundle mainBundle]pathForResource:@"samplein" ofType:@"txt"];
NSData *data = [[NSData alloc]initWithContentsOfFile:path];
NSError *error;
NSObject *input = [NSJSONSerialization JSONObjectWithData:dataInput options:NSJSONReadingMutableContainers error:&error];
if(error)
{
NSLog(@"%@",error.localizedDescription);
}
这是我的 JSON 数据
sampleinputdata = {
"round": {
"id" : 1,
"course" : {
"id" : 2,
"slope" : 118,
"holes" : [ {
"id" : 1,
"hole_number" : 1,
"handicap" : 5,
"par" : 4
} , {
"id" : 2,
"hole_number" : 2,
"handicap" : 7,
"par" : 3
} , {
"id" : 3,
"hole_number" : 3,
"handicap" : 8,
"par" : 3
} , {
"id" : 4,
"hole_number" : 4,
"handicap" : 10,
"par" : 4
}, {
"id" : 4,
"hole_number" : 5,
"handicap" : 10,
"par" : 4
} , {
"id" : 5,
"hole_number" : 6,
"handicap" : 10,
"par" : 4
} , {
"id" : 6,
"hole_number" : 7,
"handicap" : 10,
"par" : 4
} , {
"id" : 7,
"hole_number" : 8,
"handicap" : 9,
"par" : 5
} , {
"id" : 8,
"hole_number": 9,
"handicap" : 11,
"par" : 3
} , {
"id" : 9,
"hole_number" : 10,
"handicap" : 5,
"par" : 4
} , {
"id" : 10,
"hole_number" : 11,
"handicap" : 7,
"par" : 3
} , {
"id" : 11,
"hole_number" : 12,
"handicap" : 8,
"par" : 3
} , {
"id" : 12,
"hole_number" : 13,
"handicap" : 10,
"par" : 4
}, {
"id" : 13,
"hole_number" : 14,
"handicap" : 10,
"par" : 4
} , {
"id" : 14,
"hole_number" : 15,
"handicap" : 10,
"par" : 4
} , {
"id" : 15,
"hole_number" : 16,
"handicap" : 10,
"par" : 4
} , {
"id" : 16,
"hole_number" : 17,
"handicap" : 10,
"par" : 4
} , {
"id" : 17,
"hole_number" : 18,
"handicap" : 9,
"par" : 5
} ]
},
"viewer" : [
{
"id" : 1,
"name" : "jimmy dean",
"handicap" : 10,
"n_flag" : true,
"g_flag" : true,
"group_name" : "groupone",
"team_name" : "teamtwo",
"scores" : [1,4,1,5,3,2,4,3,2,5,2,4,4,4,4,5,3,2]
} , {
"id" : 2,
"name" : "martha s",
"handicap" : 12,
"n_flag" : true,
"g_flag" : true,
"group_name" : "groupone",
"team_name" : "teamtwo",
"scores" : [3,4,6,5,3,2,4,3,2,5,2,1,4,4,4,5,3,2]
} , {
"id" : 3,
"name" : "jim bob",
"handicap" : 11,
"n_flag" : true,
"g_flag" : true,
"group_name" : "groupone",
"team_name" : "teamone",
"scores" : [3,4,6,5,3,2,4,3,2,5,2,4,6,4,4,5,3,2]
} , {
"id" : 4,
"name" : "big mac",
"handicap" : 13,
"n_flag" : true,
"g_flag" : true,
"group_name" : "groupone",
"team_name" : "teamone",
"scores" : [3,4,6,5,3,2,4,3,2,5,2,4,4,4,4,2,3,2]
}
],
"bets" : {
"net_skins_bet": 10.00,
"gross_skins_bet": 5.00,
"better_ball_bet": 10.00,
"nassau_bets" : [
{
"id" : 1,
"min_golfer_id" : 2,
"max_golfer_id" : 3,
"bet_amount" : 10.00
} , {
"id" : 2,
"min_golfer_id" : 2,
"max_golfer_id" : 4,
"bet_amount" : 10.00
} , {
"id" : 3,
"min_golfer_id" : 3,
"max_golfer_id" : 4,
"bet_amount" : 10.00
} , {
"id" : 4,
"min_golfer_id" : 1,
"max_golfer_id" : 2,
"bet_amount" : 12.00
} , {
"id" : 5,
"min_golfer_id" : 1,
"max_golfer_id" : 3,
"bet_amount" : 5.00
} , {
"id" : 6,
"min_golfer_id" : 1,
"max_golfer_id" : 4,
"bet_amount" : 0.00
}
]
}
}
}
【问题讨论】:
-
请粘贴您的方法 loadJsFromFile。此外,如果您以 sampleinputdata = { 开头 JSON,则它不是有效的 JSON。
-
0|1|2== 有点恶心。但是第二个代码 sn-p 应该适用于有效的 JSON 数据。 -
@TomErikStøwer 请检查我的更新代码
-
为什么人们认为 JSON 应该转换为 NSString? NSJSONSerialization 有一个非常好的方法来从 URL 创建 JSON,包括文件 URL。
-
请修正格式。
标签: ios objective-c iphone json nsjsonserialization