0

**I am unable to parse the following JSON file. It's giving the operation couldn’t be completed. (cocoa error 3840.).

Here is my code:

- (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);
 }

I have also tried the following, but it didn't help, pls help me out to parse this JSON. Thanx in advance.

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

this is my JSON data

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
                    }
                ]
            }
        }
    }
7
  • Please paste your method loadJsFromFile. Also if you begin your JSON with sampleinputdata = {, then it is not valid JSON. Commented Sep 16, 2014 at 9:59
  • 2
    0|1|2 == a bit nasty. But the 2nd code snippet should work for valid JSON data. Commented Sep 16, 2014 at 10:02
  • Why do people think JSON should be converted to an NSString? NSJSONSerialization has a perfectly fine method to create JSON from a URL, including a file URL. Commented Sep 16, 2014 at 10:09
  • Please fix the formatting. Commented Sep 16, 2014 at 10:09
  • @TomErikStøwer i have tried to parse that JSON data using this online parser jsonviewer.stack.hu and it parsed successfully Commented Sep 16, 2014 at 10:09

1 Answer 1

2

There are several apparent problems here:

1. You have not posted the code from this method (```loadJsFromFile:```), so there could be something wrong there that we can't comment on. ```NSString *strInputParam = [self loadJsFromFile:@"samplein.txt"];```


2. What is the intention of these options? 0|1|2? ```NSObject *input = [NSJSONSerialization JSONObjectWithData:dataInput options:0|1|2 error:&error];```


According to http://jsonlint.com/, your JSON is not valid:

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

Hence,

sampleinputdata = { ... } is not valid JSON. Perhaps you meant "sampleinputdata": { ... }, or simply { ... }.

Note: There's no need to pass the data through a string, you can load it directly from file to NSData. So your second method is the one you should use.

Sign up to request clarification or add additional context in comments.

6 Comments

I have looked at it. Your JSON is still not valid.
i have tried to parse that JSON data using this online parser jsonviewer.stack.hu and it parsed successfully
I am using the same JSON which i have posted, i guess u might have copied those ** which is there in the end
You also have to remove the initial "sampleinputdata = ". I just tested this in Xcode now. When leaving "sampleinputdata = ", I get the error you got, as expected. When removing it, no errors.
Thank you,,it worked out,,,but how can jsonviewer.stack.hu could able to parse it? though it is invalid
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.