How can I get the JSON array data from nsstring or byte in xcode 4.2?

10,388

Solution 1

Try JSONKit for this. Is is extremely simple to use.

Solution 2

I did it by using the framework : http://stig.github.com/json-framework/

It is very powerfull and can do incredible stuff !

Here how I use it to extract an item name from an HTTP request : (where result is the JSO string)

NSString *result = request.responseString;

jsonArray = (NSArray*)[result JSONValue]; /* Convert the response into an array */

NSDictionary *jsonDict = [jsonArray objectAtIndex:0];

/* grabs information and display them in the labels*/
name = [jsonDict objectForKey:@"wine_name"];

Hope this will be helpfull

Solution 3

Looking at your JSON, you are not querying the right object in the object hierarchy. The top object, which you extract correctly, is an NSDictionary. To get at the items array, and the single items, you have to do this.

NSArray *items = [iTem objectForKey:@"item"];
NSArray *filteredArray = [items filteredArrayUsingPredicate:
   [NSPredicate predicateWithFormat:@"id = %d", 2];
if (filteredArray.count) NSDictionary *item2 = [filteredArray objectAtIndex:0];
Share:
10,388
user1471568
Author by

user1471568

Updated on August 22, 2022

Comments

  • user1471568
    user1471568 over 1 year

    I'm trying to get values from nsdata class and doesn't work.

    here is my JSON data.

    {
        "count": 3,
        "item": [{
            "id": "1",
            "latitude": "37.556811",
            "longitude": "126.922015",
            "imgUrl": "http://175.211.62.15/sample_res/1.jpg",
            "found": false
        }, {
            "id": "3",
            "latitude": "37.556203",
            "longitude": "126.922629",
            "imgUrl": "http://175.211.62.15/sample_res/3.jpg",
            "found": false
        }, {
            "id": "2",
            "latitude": "37.556985",
            "longitude": "126.92286",
            "imgUrl": "http://175.211.62.15/sample_res/2.jpg",
            "found": false
        }]
    }
    

    and here is my code

    -(NSDictionary *)getDataFromItemList
    {
    
        NSData *dataBody = [[NSData alloc] initWithBytes:buffer length:sizeof(buffer)]; 
        NSDictionary *iTem = [[NSDictionary alloc]init];
        iTem = [NSJSONSerialization JSONObjectWithData:dataBody options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"id = %@",[iTem objectForKey:@"id"]);
    
        //for Test
        output = [[NSString alloc] initWithBytes:buffer length:rangeHeader.length encoding:NSUTF8StringEncoding];
        NSLog(@"%@",output);
        return iTem;
    
    }
    

    how can I access every value in the JSON? Please help me.

  • Mundi
    Mundi almost 12 years
    Where did you get @"wine_name" from?
  • Edelweiss
    Edelweiss almost 12 years
    This is a key in my JSON string {"wine_name":"some wine name"}
  • Mundi
    Mundi almost 12 years
    But what does that have to do with the question? He already extracted the NSDictionary correctly from the JSON... Never mind.
  • Mundi
    Mundi almost 12 years
    How about the other log statements? Trace back with logs and find out what is nil. Maybe your input string? This is how you will find the error.