本文介绍了XML转换为iOS中的JSON转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将XML响应转换为JSON。
I need to convert XML response to 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支持文件来自这个网站:
I am using XMLReader supporting file from this site:
我使用此代码将XML转换为JSON
I am using this code to convert XML to JSON
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError];
NSLog(@" %@", xmlDictionary);
我得到了这样的JSON回复:
I got JSON response like this:
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"
}
我在网上转换时收到此回复。如何得到这样的回应。
I get this response while conversion in online. How to get response like this.
先谢谢。
推荐答案
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError];
NSLog(@" %@", xmlDictionary);
此代码未将任何内容转换为JSON。它给你一个NSDictionary。您需要从字典中实际创建JSON数据。试试这个尺寸。
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.
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);
}
这篇关于XML转换为iOS中的JSON转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!