0

Hi I have implemented code for parsing the below response as follows but it is not working properly:

 NSString *req = [NSString stringWithFormat: @" My URL"];   
 NSDictionary *googleResponse = [[NSString stringWithContentsOfURL: [NSURL URLWithString: req] encoding: NSUTF8StringEncoding error: NULL] JSONValue];        
 NSDictionary *resultsDict = [googleResponse valueForKey:  @"eventtitle"];

What is the code for parsing the below response? Please give me solution.

{
  "AlansHarleyEvents": [
    {
      "id": "3",
      "eventtitle": "22nd Annual Pig Roast",
      "eventdate": "April 22nd 8am-5pm"
    },
    {
      "id": "4",
      "eventtitle": "Poker Run",
      "eventdate": "April 28th 8am at Shooters"
    },
    {
      "id": "5",
      "eventtitle": "Kickstands for kids",
      "eventdate": "May 12th 8am-5pm"
    },
    {
      "id": "6",
      "eventtitle": "Ride for the Cure",
      "eventdate": "May28th 8am Free Drinks!"
    },
    {
      "id": "7",
      "eventtitle": "Veterans Ride",
      "eventdate": "June 10th 9am @City Hall"
    },
    {
      "id": "8",
      "eventtitle": "Biker Beach Bash",
      "eventdate": "June 28th 8-5pm @ The Pier"
    },
    {
      "id": "10",
      "eventtitle": "22nd Annual Pig Roast",
      "eventdate": "April 22nd 8am-5pm"
    },
    {
      "id": "11",
      "eventtitle": "Poker Run",
      "eventdate": "April 28th 8am at Shooters Lounge"
    },
    {
      "id": "12",
      "eventtitle": "22nd Annual Pig Roast",
      "eventdate": "April 22nd 8am-5pm"
    },
    {
      "id": "13",
      "eventtitle": "Swamp Run",
      "eventdate": "April 22nd 8am-5pm"
    }
  ]
}
0

3 Answers 3

1

If your resultsDict contains the above JSON response then you can parse it as :

NSString *req = [NSString stringWithFormat: @" My URL"];
NSDictionary *googleResponse = [[NSString stringWithContentsOfURL: [NSURL URLWithString: req] encoding: NSUTF8StringEncoding error: NULL] JSONValue];
NSDictionary *resultsDict = [googleResponse valueForKey: @"eventtitle"];

NSMutableArray *resultArray = [resultsDict valueForKey:@"AlansHarleyEvents"]; 

for(int i = 0; i<[resultArray count]; i++)
{
    NSLog(@"%@",[[resultArray objectAtIndex:i] valueForKey:@"id"]) ;
    NSLog(@"%@",[[resultArray objectAtIndex:i] valueForKey:@"eventtitle"]) ;
    NSLog(@"%@",[[resultArray objectAtIndex:i] valueForKey:@"eventdate"]) ; 
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think that the downvote is caused by the fact that resultsDict doesn't has a key/value AlansHarleyEvents.
@maulik: when i used ur code it giving me null value at the point of NSDictionary *googleResponse = [[NSString stringWithContentsOfURL: [NSURL URLWithString: req] encoding: NSUTF8StringEncoding error: NULL] JSONValue];
1

NSJSONSerialization Class is the Native Class for only iOS 5 and above http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html

For any iPhone OS version means you can go for JSONKit: https://github.com/johnezang/JSONKit

Comments

1

Yu can use NSJSONSerialization object with IOS 5

   NSDictionnary *jsonObject = [NSJSONSerialization JSONObjectWithData:resultsDict options:NSJSONReadingMutableContainers error:&error];

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.