xcode iOS 中 XML 到 JSON 的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15498989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:01:00  来源:igfitidea点击:

XML into JSON conversion in iOS

iosobjective-cxcodeios6

提问by Thangavel

I need to convert XML response to JSON.

我需要将 XML 响应转换为 JSON。

My XML response:

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

I am using XMLReader supporting file from this site:

我正在使用来自此站点的 XMLReader 支持文件:

XMLReader

XML阅读器

I am using this code to convert XML to JSON

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

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

I got JSON response like this:

我得到了这样的 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";
};

I need response like this:

我需要这样的回应:

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

I get this response while conversion in online. How to get response like this.

我在在线转换时收到此回复。如何得到这样的回应。

Thanks in Advance.

提前致谢。

回答by Ryan Poolos

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

This code isn't converting anything to JSON. Its giving you an NSDictionary. You need to actually create the JSON data from the dictionary. Try this on for size.

此代码不会将任何内容转换为 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);
}