**我无法解析以下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
}
]
}
}
}
答案 0 :(得分:2)
这里有几个明显的问题:
<德尔> 1.你还没有发布这个方法的代码(```loadJsFromFile:```),所以我们可以评论一些错误。 ```NSString * strInputParam = [self loadJsFromFile:@&#34; samplein.txt&#34;];``` 德尔>
根据http://jsonlint.com/,您的JSON无效:
Parse error on line 1:
sampleinputdata={
^
Expecting '{', '['
因此,
sampleinputdata = { ... }
无效JSON。也许你的意思是"sampleinputdata": { ... }
,或者只是{ ... }
。
注意:没有必要通过字符串传递数据,您可以直接从文件加载到NSData。所以你的第二种方法是你应该使用的方法。