2013-03-19 12 views
10

Devo convertire la risposta XML in JSON.Conversione XML in JSON in iOS

La mia risposta XML:

<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> 

Sto usando XMLReader supporto di file da questo sito:

XMLReader

Sto usando questo codice per convertire XML per JSON

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

ho avuto risposta JSON come questo:

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"; 
}; 

Ho bisogno di risposta in questo modo:

{ 
    "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" 
      } 

ricevo questa risposta mentre la conversione in online. Come ottenere una risposta come questa.

Grazie in anticipo.

+0

Il suo altro che una rappresentazione dizionario, è possibile convertire i dati in dizionario dopo l'analisi del valore da xml, ma domanda è il motivo per cui si farà questo, perché in definitiva si dovrà analizzare il file XML, mentre a fare la stessa cosa due volte.. ? – iphonic

+0

possibile duplicato di [Come convocare stringa XML su JSON utilizzando iPhone sdk] (http://stackoverflow.com/questions/6354159/how-to-convet-xml-string-to-json-using-iphone-sdk) –

+0

Usa il codice di Ryan, funzionante. –

risposta

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

Questo codice non converte nulla in JSON. Ti sta dando un NSDictionary. È necessario creare effettivamente i dati JSON dal dizionario. Prova questo per dimensioni.

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

Ho testato il codice, lavorando come un incantesimo, grazie per aver risposto. –

+0

Contento di aver potuto aiutare. Happy Coding :) Non dimenticare di accettare la risposta. –

+1

@RyanPoolos Ho bisogno di aiuto ... Sto ottenendo questo: 'Codice = -1016" Tipo di contenuto previsto {( "text/json", "text/javascript" )}, ottenuto text/xml "UserInfo = 0x17f9e770 {NSErrorFailingURLKey = http: //server.com/sw_icfs.asmx/Response_Sucursales? Cli_Cod = 1 & Key_String = hW9gvga8ieDBbM5wm4, NSLocalizedDescription = Tipo di contenuto previsto {( " text/json ", " application/json " , "text/javascript" )}, ottenuto testo/xml} 'qualche idea? :) – marciokoko