0

i have this json string

{
    "@uri": "http://localhost:8080/TunisairRESTful/resources/cities/",
    "city": [
        {
            "@uri": "http://localhost:8080/TunisairRESTful/resources/cities/ABJ/",
            "agencyCollection": {
                "@uri": "http://localhost:8080/TunisairRESTful/resources/cities/ABJ/agencyCollection/"
            },
            "codecit": "ABJ",
            "country": {
                "@uri": "http://localhost:8080/TunisairRESTful/resources/cities/ABJ/country/"
            },
            "namecit": "ABIDJAN"
        },
        {
            "@uri": "http://localhost:8080/TunisairRESTful/resources/cities/ALG/",
            "agencyCollection": {
                "@uri": "http://localhost:8080/TunisairRESTful/resources/cities/ALG/agencyCollection/",
                "agency": [
                    {
                        "@uri": "http://localhost:8080/TunisairRESTful/resources/cities/ALG/agencyCollection/3/"
                    },
                    {
                        "@uri": "http://localhost:8080/TunisairRESTful/resources/cities/ALG/agencyCollection/4/"
                    }
                ]
            },
            "codecit": "ALG",
            "country": {
                "@uri": "http://localhost:8080/TunisairRESTful/resources/cities/ALG/country/"
            },
            "namecit": "ALGER"
        },
        {
            "@uri": "http://localhost:8080/TunisairRESTful/resources/cities/AMS/",
            "agencyCollection": {
                "@uri": "http://localhost:8080/TunisairRESTful/resources/cities/AMS/agencyCollection/",
                "agency": [
                    {
                        "@uri": "http://localhost:8080/TunisairRESTful/resources/cities/AMS/agencyCollection/5/"
                    },
                    {
                        "@uri": "http://localhost:8080/TunisairRESTful/resources/cities/AMS/agencyCollection/6/"
                    }
                ]
            },
            "codecit": "AMS",
            "country": {
                "@uri": "http://localhost:8080/TunisairRESTful/resources/cities/AMS/country/"
            },
            "namecit": "AMSTERDAM"
        }
    ]
}

I want to parse it, I wrote this code

NSString *myJSON = [[NSString alloc] initWithContentsOfFile:responseString encoding:NSUTF8StringEncoding error:NULL];   
NSDictionary *json    = [myJSON JSONValue];
//NSLog(responseString);
NSArray *citysList    =  [json objectForKey:@"city"];
NSLog(@"ok");
NSLog(@" number of element : %@", [citysList count]);

But I have 0 number of element , help please

2 Answers 2

1

Michael is right. Here's a more detailed answer:

initWithContentsOfFile takes a string containing a file path (e.g. "/users/mehdi/documents/myFile.txt"). You seem to be passing in your actual JSON string, which isn't a file path. As a result, initWithContentsOfFile is probably returning nil.

Check this by asking:

if (myJSON == nil) NSLog(@"myJSON variable == nil!");

If it is nil, then your code is also setting json and citysList to nil.

Try this:

NSDictionary *json    = [responseString JSONValue];
NSArray *citysList    =  [json objectForKey:@"city"];
NSLog(@" number of element : %d", [citysList count]);
Sign up to request clarification or add additional context in comments.

Comments

1

You're doing an initWithContentsOfFile but passing in a string. Have you tried simply [responseString JSONValue]? Your code suggests that responseString has the PATH to the file you're trying to open, not your full response string itself.

Comments

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.