Search code examples
jsonios6nsarraynsdata

NSData --> Json --> NSArray and show information in Uitableview


I have a URL request, which return a JSON String in this format:

    {
"code": 1, "data": [
{
"city": "Trier",
"distance": 6,
"id": "1",
"image": "1_51af0b6a7ce86.png", "lat": "49.750893",
"long": "6.622419",
"name": "A1 Trier",
"street": "Über Brücken 4",
"zip": "54294"
}, {
} ]
}
"city": "Trier",
    "distance": 7,
    "id": "2",
    "image": "nologo.png", "lat": "49.757984",
    "long": "6.640876", "name": "Safari Haus Trier", "street": "Stockplatz 2a", "zip": "54294"

Now I need a NSData and then a NSArray or NSDictionary to save this information,right?. I want to show the data from the JSON in a tableview. In each cell should be the name, place and distance from the club. How can I do this? in the first cell should be the first club and the name and so on.. and in the second cell the second club.

How can I split this JSON string?


Solution

  • Refer to this link:

    Parsing JSON in Objective-C using NSJSONSerialization

    As mentioned in the above link, you can parse JSON using NSJSONSerialization as follows:

    STEP-1: Convert your JSON String (say jsonString) to NSData

    NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
    

    STEP-2: Serializing JSON into data of NSArray

    NSError *jsonParsingError = nil;
    NSArray *responseArray = [NSJSONSerialization JSONObjectWithData:response
    options:0 error:&jsonParsingError];
    

    STEP-3: In your cellForRowAtIndexPath: tableview delegate method, create a custom cell for tableview where in you have three labels for displaying club name (say lblName), club distance (say lblDistance) and club place (say lblPlace). Once you are done with this, you can just manipulate data from array and display it in tableview

    Let me know if you need more help.