0

I have this JSON data :

array: {
    data = (
                {
            "com_id" = 1;
            "com_name" = Apple;
        },
                {
            "com_id" = 2;
            "com_name" = "Google";
        },
               {
            "com_id" = 3;
            "com_name" = "Yahoo";
        }
    );
    message = "Data found";
    response = success;
}

here's my code to fetch that data :

    NSURL * url = [[NSURL alloc] initWithString:@"https://jsonurlhere.com"];

    // Prepare the request object
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
                                                cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                            timeoutInterval:30];

    // Prepare the variables for the JSON response
    NSData *urlData;
    NSURLResponse *response;
    NSError *error;

    // Make synchronous request
    urlData = [NSURLConnection sendSynchronousRequest:urlRequest
                                    returningResponse:&response
                                                error:&error];

    // Construct a Array around the Data from the response
    NSArray* object = [NSJSONSerialization
                       JSONObjectWithData:urlData
                       options:0
                       error:&error];

    NSLog(@"array: %@", object);

now, I want to use that JSON data into my PickerView. how to change that JSON data into array so that I can load it to replace my existing array (self.nameCompany)?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.nameCompany = @[@"Apple", @"Google", @"Yahoo"];
}
2
  • 1
    Look at your JSON data: That is not an array. It is a dictionary, and the value of the "data" key is an array ... Commented Feb 6, 2014 at 6:44
  • @MartinR : that's the point. I know that JSON is JSON, not an array. that's why I want to convert it into NSArray so that it can be used as a data in my UIPickerView. Commented Feb 6, 2014 at 6:55

2 Answers 2

3

I think It's helpful to you. Can you try this following link? It's not use to any supporting files. Also, see this link NSJSONSerialization supported URL NSJSONSerialization

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

Comments

1

check this code , your response is not a array but Dictionary.

-(void)loadData{

NSURL * url = [[NSURL alloc] initWithString:@"https://jsonurlhere.com"];

// Prepare the request object
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
                                            cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                        timeoutInterval:30];

// Prepare the variables for the JSON response
NSData *urlData;
NSURLResponse *response;
NSError *error;

// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
                                returningResponse:&response
                                            error:&error];

// Construct a Array around the Data from the response
NSDictionary* object = [NSJSONSerialization
                   JSONObjectWithData:urlData
                   options:0
                   error:&error];

NSLog(@"array: %@", [object objectForKey:@"data"]);


NSMutableArray *companyArray=[[NSMutableArray alloc] init];

for (NSDictionary *tempDict in [object objectForKey:@"data"]) {

    [companyArray addObject:[tempDict objectForKey:@"com_name"]];
}

self.nameCompany=[NSArray arrayWithArray:companyArray];
}

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.