【问题标题】:Unable to parse JSON file in obj c无法解析 obj c 中的 JSON 文件
【发布时间】: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


【解决方案1】:

这里有几个明显的问题:

1. 你还没有发布这个方法的代码(```loadJsFromFile:```),所以那里可能有问题我们不能评论。 ```NSString *strInputParam = [self loadJsFromFile:@"samplein.txt"];```


2. 这些选项的目的是什么? 0|1|2? ```NSObject *input = [NSJSONSerialization JSONObjectWithData:dataInput options:0|1|2 error:&error];```


根据http://jsonlint.com/,您的 JSON 无效:

Parse error on line 1: sampleinputdata={
^ Expecting '{', '['

因此,

sampleinputdata = { ... } 不是有效的 JSON。也许你的意思是"sampleinputdata": { ... },或者只是{ ... }

注意:不需要通过字符串传递数据,可以直接从文件加载到 NSData。因此,您应该使用第二种方法。

【讨论】:

  • 我看过了。您的 JSON 仍然无效。
  • 我已尝试使用此在线解析器 jsonviewer.stack.hu 解析该 JSON 数据并成功解析
  • 我使用的是我发布的相同 JSON,我猜你可能复制了最后的那些 **
  • 您还必须删除初始的“sampleinputdata =”。我现在刚刚在 Xcode 中测试了这个。当离开“sampleinputdata =”时,我得到了你得到的错误,正如预期的那样。删除它时,没有错误。
  • 谢谢,,它成功了,,,但是jsonviewer.stack.hu 怎么能解析它呢?虽然无效
猜你喜欢
  • 1970-01-01
  • 2011-12-09
  • 2013-11-16
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多