Objective-C Language                
            NSJSONSerialization
        
        
            
    Zoeken…
Syntaxis
- (id) JSONObjectWithData: (NSData *) gegevensopties: (NSJSONReadingOptions) opt-fout: (NSError * _Nullable *) fout
parameters
| operator | Beschrijving | 
|---|---|
| gegevens | Een gegevensobject dat JSON-gegevens bevat | 
| opteren | Opties voor het lezen van de JSON-gegevens en het maken van de Foundation-objecten. | 
| fout | Als er een fout optreedt, bevat deze bij terugkeer een NSError-object dat het probleem beschrijft. | 
Opmerkingen
NSJSONSerialization is beschikbaar in iOS 5.0 en hoger Een object dat kan worden geconverteerd naar JSON moet de volgende eigenschappen hebben:
- Het object op het hoogste niveau is een NSArray of NSDictionary. 
- Alle objecten zijn exemplaren van NSString, NSNumber, NSArray, NSDictionary of NSNull. 
- Alle woordenboeksleutels zijn exemplaren van NSString. 
- Nummers zijn geen NaN of oneindig. 
JSON Parsing met NSJSONSerialisatie Doelstelling c
NSError *e = nil;
NSString *jsonString = @"[{\"id\": \"1\", \"name\":\"sam\"}]";
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options:  NSJSONReadingMutableContainers error: &e];
if (!jsonArray) {
    NSLog(@"Error parsing JSON: %@", e);
} else {
    for(NSDictionary *item in jsonArray) {
        NSLog(@"Item: %@", item);
    }
}
Output:
Item: {
    id = 1;
    name = sam;
}
Voorbeeld 2: inhoud van URL gebruiken:
//Parsing:
NSData *data = [NSData dataWithContentsOfURL:@“URL HERE”];
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@“json :%@”,json);
Voorbeeld reactie:
json: {
    MESSAGE = “Test Message";
    RESPONSE =(
                {
            email = "[email protected]";
            id = 15;
            phone = 1234567890;
            name = Staffy;
        }
    );
    STATUS = SUCCESS;
}
 NSMutableDictionary *response = [[[json valueForKey:@"RESPONSE"] objectAtIndex:0]mutableCopy];
 NSString *nameStr = [response valueForKey:@"name"];
 NSString *emailIdStr = [response valueForKey:@"email"];
Modified text is an extract of the original Stack Overflow Documentation
        Licentie onder CC BY-SA 3.0
        Niet aangesloten bij Stack Overflow