2013-03-19 3 views
10

Ich muss XML-Antwort zu JSON konvertieren.XML in JSON Konvertierung in iOS

Meine XML-Antwort:

<commands> 
<command id="0" name="GetAPPsProducts"> 
    <command_parameters> 
    <command_parameter id="0" name="APPs_Code">ATAiOS</command_parameter> 
    </command_parameters> 
    <command_result> 
    <apps_products> 
     <apps_products id="1"> 
     <apps_code>ATAiOS</apps_code> 
     <apps_product_id>2</apps_product_id> 
     <brand_id>2</brand_id> 
     <brand_desc>Generic</brand_desc> 
     <brand_product_id>2</brand_product_id> 
     <product_id>001-7</product_id> 
     <descrizione>MyTravelApp</descrizione> 
     </apps_products> 
    </apps_products> 
    </command_result> 
</command> 

ich XMLReader bin mit diesem Site-Datei unterstützt:

XMLReader

ich diesen Code verwenden XML zu JSON konvertieren

NSError *parseError = nil; 
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError]; 
NSLog(@" %@", xmlDictionary); 

Ich habe JSON-Antwort wie folgt aus:

commands =   { 
     command =    { 
      "command_parameters" =     { 
       "command_parameter" =      { 
        id = 0; 
        name = "APPs_Code"; 
        text = "\n \n \n  \n  ATAiOS"; 
       }; 
       text = "\n  "; 
      }; 
      "command_result" =     { 
       "apps_products" =      { 
        "apps_products" =       { 
         "apps_code" =        { 
          text = "\n  \n  \n   \n   ATAiOS"; 
         }; 
         "apps_product_id" =        { 
          text = "\n   2"; 
         }; 
         "brand_desc" =        { 
          text = "\n   Generic"; 
         }; 
         "brand_id" =        { 
          text = "\n   2"; 
         }; 
         "brand_product_id" =        { 
          text = "\n   2"; 
         }; 
         descrizione =        { 
          text = "\n   MyTravelApp"; 
         }; 
         id = 1; 
         "product_id" =        { 
          text = "\n   001-7"; 
         }; 
         text = "\n   "; 
        }; 
        text = "\n  "; 
       }; 
       text = "\n  "; 
      }; 
      id = 0; 
      name = GetAPPsProducts; 
      text = "\n "; 
     }; 
     text = "\n "; 
    }; 
    text = "\n \n"; 
}; 

Ich brauche Antwort wie folgt aus:

{ 
    "commands": { 
    "command": { 
     "-id": "0", 
     "-name": "GetAPPsProducts", 
     "command_parameters": { 
     "command_parameter": { 
      "-id": "0", 
      "-name": "APPs_Code", 
      "#text": "ATAiOS" 
     } 
     }, 
     "command_result": { 
     "apps_products": { 
      "apps_products": { 
      "-id": "1", 
      "apps_code": "ATAiOS", 
      "apps_product_id": "2", 
      "brand_id": "2", 
      "brand_desc": "Generic", 
      "brand_product_id": "2", 
      "product_id": "001-7", 
      "descrizione": "MyTravelApp" 
      } 

ich diese Antwort, während Umwandlung in online. Wie bekomme ich eine Antwort?

Vielen Dank im Voraus.

+0

Sein nichts anderes als ein Wörterbuch Darstellung, können Sie die Daten in Wörterbuch konvertieren nach Wert von XML-Analyse, aber Frage ist, warum Sie dies tun, weil Sie letztlich xml-Datei zu analysieren haben, während auf gleiche tun zweimal ..? – iphonic

+0

mögliches Duplikat von [Wie Konvertieren von XML-Zeichenfolge zu JSON mit iPhone SDK] (http://stackoverflow.com/questions/6354159/how-to-convet-xml-string-to-json-using-iphone-sdk) –

+0

Verwenden Sie @ Ryans Code, funktioniert gut. –

Antwort

11
NSError *parseError = nil; 
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError]; 
NSLog(@" %@", xmlDictionary); 

Dieser Code konvertiert nichts in JSON. Es gibt Ihnen ein NSDictionary. Sie müssen die JSON-Daten tatsächlich aus dem Wörterbuch erstellen. Versuchen Sie dies für die Größe.

NSError *error; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:xmlDictionary 
                options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string 
                error:&error]; 

if (! jsonData) { 
    NSLog(@"Got an error: %@", error); 
} else { 
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 

    NSLog(@"%@",jsonString); 
} 
+1

Ich habe Ihren Code getestet, funktioniert wie ein Charme, Danke für Ihre Antwort. –

+0

Ich bin froh, dass ich helfen konnte. Happy Coding :) Vergessen Sie nicht, die Antwort zu akzeptieren. –

+1

@RyanPoolos Ich brauche etwas Hilfe ... Ich bekomme das: 'Code = -1016" Erwartete Inhaltstyp {( "text/json", "application/json", "text/javascript" )}, bekam text/xml“Userinfo = 0x17f9e770 {NSErrorFailingURLKey = http: //server.com/sw_icfs.asmx/Response_Sucursales Cli_Cod = 1 & key_string = hW9gvga8ieDBbM5wm4, NSLocalizedDescription = Expected Inhaltstyp {( "text/json", "application/json" , "text/javascript" )}, habe text/xml} 'irgendwelche Ideen? :) – marciokoko

Verwandte Themen