iOS中的XML转换为JSON

iOS中的XML转换为JSON,第1张

概述我需要将 XML响应转换为 JSON. 我的XML响应: <commands><command id="0" name="GetAPPsProducts"> <command_parameters> <command_parameter id="0" name="APPs_Code">ATAiOS</command_parameter> </command_parameters> 我需要将 XML响应转换为 JSON.

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

我正在使用本网站的XMLReader支持文件:

XMLReader

我正在使用此代码将XML转换为JsON

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

我收到了JsON响应:

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

我需要这样的回应:

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

我在线转换时得到这个回应.如何获得这样的回应

提前致谢.

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

此代码不会将任何内容转换为JsON.它给你一个NSDictionary.您需要从字典中实际创建JsON数据.尝试这个大小.

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);}
总结

以上是内存溢出为你收集整理的iOS中的XML转换为JSON全部内容,希望文章能够帮你解决iOS中的XML转换为JSON所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1096643.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存