0

I am trying to create an array similar to the one that is created like this:

NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"www.example.com/j.php"]];
NSError *error = nil;
if (jsonData) {
     result = [NSJSONSerialization JSONObjectWithData:jsonData
                                              options:NSJSONReadingMutableContainers 
                                                error:&error];
}

where www.example.com/j.php Is JSON that looks like this:

[{"name":"matt","genre":"Photography","info":"foo","date":"2014-06-12"},{"name":"jamie","genre":"Art","info":"Bar","date":"2014-06-13"}]

This array is then used like

result[indexPath.row][@"name"];

How can I build a new NSArray with my own name, genre and info that is in the same format as the one above??


I have tried to print the array using logs but it comes up as (null)

and I know I do not create it like this:

[NSArray arrayWithObjects: @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", nil]
1
  • 3
    Go to json.org and learn the JSON syntax. Then understand that a JSON "array" maps to an NSArray while a JSON "object" maps to an NSDictionary. Commented Jul 7, 2014 at 12:07

3 Answers 3

5

Its NSArray of NSDictionary.

NSArray *tmp = @[@{@"name": @"matt", @"genre" : @"Photography"}, @{@"name": @"jamie", @"genre" : @"Art"}];

You have to create it using the given example.

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

12 Comments

You are incorrect on that last update. You can access an NSDictionary using [] notation, in later versions of iOS.
That error message is coming from the NSLog you're surrounding it all with.
Thank you @YogeshSuthar how would I then add more results to the array? Like this? [tmp addObject: @[@{@"name": @"matt", @"genre" : @"Photography"}]]; ?
(Hint: Do NSLog(@"%@", something);, not NSLog(something);.)
@HotLicks Ohhh yes yes I was trying it in NSLog. Thanks for correction. :)
|
0

use this code i hope help you :

 for (NSDictionary *data in jsonObjects) {


            NSString *name_data           =  [data objectForKey:@"name"];
            NSString *secteur_data      =  [data objectForKey:@"genre"];
            NSString *société_data      =  [data objectForKey:@"info"];

     DictionaryOfname = [NSDictionary dictionaryWithObjectsAndKeys:
                               [data objectForKey:@"name"],@"name",
                                  [data objectForKey:@"genre"],@"genre",
                                 [data objectForKey:@"info"],@"info",
                                   nil];

            [Listeofname addObject:DictionaryOfname];
    }

3 Comments

I can't see that even compiling.
What does [ objectForKey:@"name"]; do then?
And leading caps should not be used for variable names in Objective-C.
0

You can as @Yogesh Suthar suggested and Can access using valueForKeyPath or just valueForKey Method.

NSArray * tmp = @[
                      @{@"name": @"matt", @"genre" : @"Photography"},
                      @{@"name": @"jamie", @"genre" : @"Art"}
                 ];


    NSLog(@"name is : %@", tmp[0][@"name"]);
    NSLog(@"name is : %@", [tmp valueForKeyPath:@"name"][0]);

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.