-2

this is my json content.

 [  
  {  
    "sha":"30eae8a47d0203ac81699d8fc2ab2632de2d0bba",
     "commit":{  
     "author":{  
        "name":"Madhura Bhave",
        "email":"mbhave@pivotal.io",
        "date":"2017-03-23T23:14:32Z"
     },
     "committer":{  
        "name":"Madhura Bhave",
        "email":"mbhave@pivotal.io",
        "date":"2017-03-23T23:14:32Z"
     },
     "message":"Merge branch '1.5.x'",

   }
 }
 ]

and this is my main.i just want to retrieve key value from message and name,email,date from committer dictionary.i got stuck how to do that.

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


    for (NSDictionary *CommitDictionary in CommitJson) {
        CommitDict *commitDictObj = [[CommitDict alloc] init];
        commitDictObj.message = [CommitDictionary objectForKey:@"message"];

        for (NSDictionary *CommitterDictionary in [CommitDictionary objectForKey:@"committer"]) {
            Committer *author = [[Committer alloc] init];
            author.name = [CommitterDictionary objectForKey:@"name"];
            author.email = [CommitterDictionary objectForKey:@"email"];
            author.date = [CommitterDictionary objectForKey:@"date"];
        }

        [CommitArray addObject:commitDictObj];
    }




    for (int i =0 ; i < [CommitArray count] ; i++){
        CommitDict *commitDictObj = [CommitArray objectAtIndex:i];
        NSLog(@"Commit Message: %@", commitDictObj.message);



    }

     return 0;
}

}

i try fetch the json and display it value of message,name,email and date.how can i log the value of message, name, email and date?

  • print commit array once and print CommitJson once – Anbu.Karthik Mar 26 '17 at 04:17
  • sorry sir..i dont get it..can you explain more detail – abdul haziq Mar 26 '17 at 04:21
  • You need to clarify your question. Please [edit] your question and provide more details. In what way exactly doesn't your posted code work? Does it compile? Does it crash? How far does it get? – rmaddy Mar 26 '17 at 04:22
  • print once this data id CommitJson – Anbu.Karthik Mar 26 '17 at 04:22
  • i already edit and clarify the question..hope you guys can help me. – abdul haziq Mar 26 '17 at 04:50
  • 1
    Your JSON sample contains unbalanced []'s and {}'s; your code uses the key "commits_url" which does not occur in the JSON you show; and you are unclear about where your code goes wrong. You need to clean up your question and provide more detail if people are to help you without a lot of guesswork. – CRD Mar 26 '17 at 05:33
  • i already edit the question..i just want to display the value for key message, name, email and date.hope u guys can help me figure it out.:) – abdul haziq Mar 26 '17 at 12:26
  • Place a breakpoint on the first `for`, run your code. When the breakpoint is reached set through each statement and examine the variables as you go, starting with `CommitJson`. You will find the error (nobody else can with the supplied information). HTH (BTW follow the naming convention to start variables with a lowercase letter - note the syntax colouring in your sample code above is all wrong). – CRD Mar 26 '17 at 20:09
  • FYI ... to easily validate your JSON you can paste it into http://jsonlint.com/ – Chris Edgington Mar 26 '17 at 23:12

2 Answers2

0

Your array contains a dictionary, and that dictionary contains the commit dictionary, not the commit dictionary directly. Replace that part of your code:

for (NSDictionary *CommitDictionary in CommitJson) {
        CommitDict *commitDictObj = [[CommitDict alloc] init];

With that:

for (NSDictionary *shaCommitDictionary in CommitJson) {
        CommitDict *commitDictObj = [[CommitDict alloc] init];
        NSDictionary *CommitDictionary = [shaCommitDictionary objectForKey:@"commit"];
VitorMM
  • 1,060
  • 8
  • 33
0

(1) Convert JSON to NSDictionary

NSData *jsonData= ... // Assume you got the data already loaded
NSError *error = nil;    
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

(2) Access the dictionary values (fast enumeration available by now!!

NSString *message = dictionary[@"message"];

NSDictionary *author = dictionary[@"author"];
NSString *name = author[@"author"];
NSString *email = author[@"author"];
NSString *date = author[@"author"];
// OR:
// NSString *name = dictionary[@"author"][@"author"];
// NSString *email = dictionary[@"author"][@"author"];
// NSString *date = dictionary[@"author"][@"author"];

And thats it. I think the tricky thing is to get the JSON Data to the NSDictionary? See here: https://stackoverflow.com/a/30561781/464016

Community
  • 1
  • 1
Lepidopteron
  • 6,056
  • 5
  • 41
  • 53