0

am using objective c and here is my problem..

{
    "STATUS":"OK",
    "projects":
    [

    {

        "company":
        {
            "name":"ABC Pvt Ltd.","id":"12345"
        },
        "created-on":"2012-07-07T04:29:29Z",
        "category":
        {
            "name":"",
            "id":""
        },
        "starred":false,
        "name":"MY Platform 1",
        "startDate":"",
        "logo":"abc.png","notifyeveryone":false,
        "id":"70596",
        "last-changed-on":"2013-05-20T12:22:11Z",
        "status":"active",
        "endDate":""

    },

    {   
        "company":{
                    "name":"ABC Pvt Ltd.",
                    "id":"31222"
                   },
        "created-on":"2012-05-22T07:06:30Z",
        "category":{
                    "name":"","id":""
                    },
        "starred":false,
        "name":"Miscellaneous 1",
        "startDate":"",
        "logo":"abc.png",
        "notifyeveryone":false,
        "id":"12345",
        "last-changed-on":"2013-05-20T12:19:45Z",
        "status":"active",
        "endDate":""
    }
    ]
    }

Above is my json string am retrieving from asihttpRequest

now i want to display the project name eg.MY Platform 1 and Miscellaneous 1

i have just got the array of projects with the following :

NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:data //1

                          options:kNilOptions
                          error:&error];



   ProjectDetailsarray = [json objectForKey:"projects"];

now can anyone let me know how i get the name of projects as i mentioned above in my table view with two row.

first is MY Platform 1 and second is Miscellaneous 1

i am new to objective c so i tried to solve out but ddint find a way to get the name of the project?

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
BhavikKama
  • 8,566
  • 12
  • 94
  • 164
  • 1
    All the answer available here is not generic and error prone. Check the proper method [here](http://stackoverflow.com/questions/14958883/ios-serialize-deserialize-complex-json-generically-from-nsobject-class/16771574#16771574) – Alphapico May 28 '13 at 09:50

6 Answers6

4

Simple, loop over the array:

for (NSDictionary *project in json[@"projects"]) {
    NSLog(@"Project name: %@", project[@"name"]);
}

Note I'm using subscripted notation instead of [json objectForKey:@"projects"], which is much shorter.

Cyrille
  • 25,014
  • 12
  • 67
  • 90
3

You have an array of dictionaries. How to get values from dictionary you should know:

NSDictionary *dic = @{@"Name": @"Andrew", 
                      @"Surname": @"Kama", 
                      @"Age": @24, 
                      @"Friends":@[@"Lesly", @"John", @"Carter"]};

NSString *name = dic[@"Name"];

To obtain second friend's name:

NSArray *friends = dic[@"Friends"];
NSString *secondFriend = friends[1]; // or dic[@"Friends"][1];

and so on.

You can think of JSON as nested NSDictionaries/NSArrays.

To iterate over projects:

NSArray *projects = json[@"projects"];
for (NSDictionary *project in projects) {
    NSLog(@"Project name: %@", project[@"name"]);

    // how to get company name?
    // project[@"company"][@"name"];
    // etc
}
AndrewShmig
  • 4,843
  • 6
  • 39
  • 68
1

as project is an array you can enumerate array and fetch outs project name by its key as below

__block NSMutableArray *projectNameArray=[[NSMutableArray alloc] init];
NSArray *projects = [json objectForKey:@"projects"];

[projects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSLog(@"project %i %@",idx,[obj valueForKey:@"name"]);
    [projectNameArray addObject:[obj valueForKey:@"name"]];
}];

NSLog(@"project name array is %@",projectNameArray);
Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90
1

You can use Key-Value Coding to get array of projects names with this code:

NSArray *projectsNames = [json valueForKeyPath:@"projects.name"];

This example code:

NSArray *projectsNames = [json valueForKeyPath:@"projects.name"];
NSLog(@"Projects names: %@", projectsNames);

Gives this output:

Projects names: (
  "MY Platform 1",
  "Miscellaneous 1"
)

You can get more information about Key-Value Coding in docs here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/BasicPrinciples.html

Lukas Kukacka
  • 7,604
  • 2
  • 25
  • 50
0

You have array of projects in json. and that array contains dictionaries.

So basically first you need to get dictionary from array and then you can get value from dictionary.

Like this..

NSArray *projects = [json objectForKey:@"projects"];
for(NSDictionary *dict in projects)
    NSLog(@"Project Name: %@",[dict objectForKey:@"name"]);
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
0

This is simple. Just fetch the value like this from you array:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

   static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    cell.textLabel.text = [[ProjectDetailsarray objectAtIndex:indexPath.row] objectForKey:@"name"];
}
Ajay Chaudhary
  • 1,993
  • 15
  • 23