开发者

Parsing json data not showing correct value

SBJsonParser *parser = [[SBJsonParser alloc] init];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.krsconnect.no/community/api.html?method=bareListEventsByCategory&appid=620&category-selected=350&counties-selected=Vest-Agder,Aust-Agder"]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSDictionary *object = [parser objectWithString:json_string error:nil];
    NSArray *results = [parser objectWithString:json_string error:nil];

    NSDictionary *dictOne = [results objectAtIndex:0];

    appDelegate.books = [[NSMutableArray alloc] init];

    NSArray *activitiesArray = [dictOne objectForKey:@"events"];



    NSDictionary *dictTwo = [activitiesArray objectAtIndex:0];

    NSArray *eventArray=[dictTwo objectForKey:@"event"];
    NSDictionary *dictThree=[eventArray objectAtIndex:0];



//  NSLog(@"%@ - %@", [dictOne objectForKey:@"date"]);

//  NSLog(@"%@ - %@", [dictTwo objectForKey:@"affectedDate"]);


    NSString*date=[dictOne objectForKey:@"date"];

    NSLog(@"%@ - %@", [dictTwo objectForKey:@"affectedDate"]);
    NSLog(@"%@ - %@", [dictThree objectForKey:@"title"]);
    NSLog(@"%@ - %@", [dictThree objectForKey:@"location"]);
    NSLog(@"%@ - %@", [dictThree objectForKey:@"municipality"]);

when i NSLog(t开发者_运维技巧itle) location and municipality it shows nil values

JSON:

[
  {
    "date": 1311552000000,
    "events": [
      {
        "affectedDate": 1311552000000,
        "event": {
          "appId": 42,
          "eventId": 18095,
          "location": "Sjølingstad Uldvarefabrik",
          "municipality": "Lindesnes",
          "title": "Utstillingsåpning - romfisk"
        }
      },
      {
        "affectedDate": 1311552000000,
        "event": {
          "appId": 42,
          "eventId": 18095,
          "location": "Sjølingstad Uldvarefabrik",
          "municipality": "Lindesnes",
          "title": "Utstillingsåpning - romfisk"
        }
      },
      {
        "affectedDate": 1311580800000,
        "event": {
          "appId": 620,
          "eventId": 19490,
          "location": "Høvåg Gjestehus, Vestre Vallesverd, Høvåg",
          "municipality": "Lillesand",
          "title": "Kunstutstilling på Høvåg Gjestehus"
        }
      },
...
] 

Following Praveen answer I have tried this code but it's still not working:

NSDictionary *dictOne = [results objectAtIndex:0];
NSArray *activitiesArray = [dictOne objectForKey:@"events"];
NSDictionary *dictTwo = [activitiesArray objectAtIndex:1];
NSArray *eventArray=[dictTwo objectForKey:@"event"];
NSDictionary *dictThree=[eventArray objectAtIndex:2];


  1. You don't need to initialize the parser, b/c SBJSON uses NSString category to add additional parsing method to standard NSString called JSONValue that returns NSDictionary representation of a NSString.

After you get a NSDictionary, convert it to NSArray like this NSArray * values = [dictionary allValues]; then use method [values objectForKey:@"date"] to get a date and so on.


Its nested Json. The parsing and retrieval of values are different. Once you get the the dictionary value for a nested object you need to dig into it for getting the next values.

  1. From the main array getobject for events.
  2. Get object for event from the above Dictionary
  3. From the dictionary returned in step 2 you can access appId, eventId etc

So basically your code should be

NSDictionary *dictTwo = [activitiesArray objectAtIndex:1]; // Or get object for key events
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜