0
[
    {
        "Id": 52,
        "Name": "name1",
        "author": "john"
    },
    {
        "Id": 53,
        "Name": "name2",
        "author": "jacob"
    },
    {
        "Id": 54,
        "Name": "name3",
        "author": "jobin"
    }
]

I used the following code to fetch and parse this json array with no main key

SBJsonParser *parser1 = [[SBJsonParser alloc] init];
   NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:str2]];
    // Perform request and get JSON back as a NSData object
    NSData *response1 = [NSURLConnection sendSynchronousRequest:request1 returningResponse:nil error:nil];
    NSError *error;
   NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:response1 options:kNilOptions error:&error];

but i am getting the following exception

data parameter is nil 

this exception is occuring at this code

NSData *response1 = [NSURLConnection sendSynchronousRequest:request1 returningResponse:nil error:nil];

How to iterate the id value?

4
  • check if you are getting any error in NSError variable... Commented Apr 23, 2015 at 11:24
  • are you getting response in jsonArray ? Commented Apr 23, 2015 at 11:26
  • there is no need of main key to access elements? you can easily access elements. Commented Apr 23, 2015 at 11:29
  • no error only exception as mentioned above, please tell me how to access elements Commented Apr 23, 2015 at 11:51

3 Answers 3

1

You have an array of JSON files here, so i would:

NSArray *jsonArray = ; //your JSON

for (NSDictionary *dict in jsonArray) {
    NSInteger number = [dict[@"Id"] intValue];
    NSString *name = dict[@"Name"];
    NSString *author = dict[@"author"];
}
Sign up to request clarification or add additional context in comments.

3 Comments

the exception is occuring at this line NSData *response1 = [NSURLConnection sendSynchronousRequest:request1 returningResponse:nil error:nil]; so how can i fetch values?
Make sure the URL is correct, are you performing the correct HTTP request type (GET)?
I'd recommend AFNetworking or MKNetworkKit, to streamline these things
0

What you given is perfect it is working for me try check your API And you did not use the SBJson also try print the NSLog("%@",jsonArray);

Comments

0

The return value from sendSynchronousRequest is void. So it makes sense that the data parameter is nil.

+ (void)sendAsynchronousRequest:(NSURLRequest *)request
                      queue:(NSOperationQueue *)queue
          completionHandler:(void (^)(NSURLResponse *response,
                                      NSData *data,
                                      NSError *connectionError))handler

The data value you're expecting is provided in the block. That's where you want to extract the NSArray and do something with it.

In my current project, I'm not using synchronousRequest but doing something similar. Maybe that helps you.

    NSError *error;
    // Read the given JSON file from the server
    NSData *filedata = [NSData dataWithContentsOfURL:url options:kNilOptions error:&error];

    if (filedata)
    {
        // Read the returned NSData from the server to an NSDictionary object
        // Essentially this is the bookstore in NSDictionary format.
        NSArray *bookStore = [NSJSONSerialization JSONObjectWithData:filedata options:kNilOptions error:&error];

    }

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.