Je suis en train de mettre en place une application basée sur le client. Dans cette application, j'ai une chaîne xml. Je dois la convertir au format JSON et l'envoyer au serveur. Je n'ai aucune idée sur la façon de convertir cela. Je n'ai aucune idée sur la façon de convertir cela. Pouvez-vous me suggérer une documentation ou une idée à ce sujet ?
Réponses
Trop de publicités?Étape 1 : Lire le XML dans le NSDictionary : http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/
Étape 2 : Convertir NSDictionary en JSON : http://code.google.com/p/json-framework/
Mario Bañares Colastra
Points
55
Comme Steve l'a dit, les deux étapes sont celles-là, je vous laisse un peu de code, qui pourra peut-être vous aider un peu plus :
// Don't forget the imports ;)
#import "XMLReader.h"
// You must have a XML string from somewhere
NSString XMLString = yourXML;
// I remove all returns and tabs from the text, after i would be annoying if you don't remove it
XMLString = [XMLString stringByReplacingOccurrencesOfString:@"\r" withString:@""];
XMLString = [XMLString stringByReplacingOccurrencesOfString:@"\t" withString:@""];
// Parse the XML into a dictionary
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:XMLString error:&parseError];
NSError *error;
self.dataParsed = [NSJSONSerialization dataWithJSONObject:xmlDictionary
options: NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
error:&error];
// Print the dictionary
NSLog(@"%@", xmlDictionary);