简体   繁体   中英

Get values from JSON string - Objective C

I have this JSON response string:

{"d":"{\"ID_usuario\":\"000130\",\"Nombre\":null,\"Vipxlo\":0,\"Provmun\":null,\"Descuentos\":null,\"Listaviplocal\":null}"}`

With this code:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    //Check valid signal

    connection = nil;

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    //data =nil;

    NSArray *jsonArray = [responseString JSONValue];

How can I do it?

When you can afford to require iOS 5 you should try NSJSONSerialization .

Your code could look like this but I suggest reading the Docs first.

    NSArray* jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:someError]

This JSON should result in a Dictionary

NSDictionary *jsonDict = [responseString JSONValue];

then use:

[jsonDict objectForKey:@"d"];

Try using the SBJasonParser library for iOS.

You can then use this code (for all iOS versions):

SBJsonParser* parser = [[[SBJsonParser alloc] init] autorelease];
NSDictionary* myDict = [parser objectWithString: responseString];

Note: Your code above has a JSON Dictionary but you were trying to access it as an Array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM