2010-09-19 4 views
1

Je sais qu'il ya quelques cadres pour analyser JSON, mais comment puis-je générer une chaîne JSON en Objective-C? Sera-ce quelque chose que je devrais écrire moi-même, ou y a-t-il quelque chose de simple déjà là-bas?générer une chaîne JSON en utilisant Objective-C

+0

Cela a été demandé plusieurs fois auparavant. S'il vous plaît passer une minute pour prendre connaissance SO avant de poser de nouvelles questions: http://stackoverflow.com/questions/2000599/how-to-send-an-array-of-object-from-iphone-app-to-a-php- script avec JSON/http://stackoverflow.com/questions/1895708/saving-and-editing-json-on-iphone-ipod – Felixyz

Répondre

3

Personnellement, je aime « utilisation de catégories sur NSObject plus avoir à passer par TouchJSON » json-framework interface de CJSONDataSerializer.

NSDictionary *dict = [NSDictionary dictionaryWithObject:@"b" forKey:@"a"]; 
NSString *json = [dict JSONRepresentation]; 
0

TouchJSON comprend un mécanisme permettant de prendre un dictionnaire et produire JSON de celui-ci:

http://github.com/schwa/TouchJSON

Le code exemple de cette page:

NSDictionary *dictionary = [NSDictionary dictionaryWithObject:@"b" forKey:@"a"]; 
NSError *error = NULL; 
NSData *jsonData = [[CJSONDataSerializer serializer] serializeObject:dictionary error&error]; 
Questions connexes