JSON.NET:JSON 转换为 XML ,XML 转换为 JSON

前端之家收集整理的这篇文章主要介绍了JSON.NET:JSON 转换为 XML ,XML 转换为 JSON前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

[代码]XML TO JSON

@H_403_24@
01 stringxml =@"<?xml version=""1.0"" standalone=""no""?>
@H_403_24@
02 <root>
@H_403_24@
03 <person id=""1"">
04 <name>Alan</name> @H_403_24@
@H_403_24@
05 <url>http://www.google.com</url>
06 </person> @H_403_24@
@H_403_24@
07 <person id=""2"">
08 <name>Louis</name> @H_403_24@
@H_403_24@
09 <url>http://www.yahoo.com</url>
10 11 </root>"; @H_403_24@
12 @H_403_24@
@H_403_24@
13 XmlDocument doc =newXmlDocument();
14 doc.LoadXml(xml); @H_403_24@
@H_403_24@
15
16 jsonText = JsonConvert.SerializeXmlNode(doc); @H_403_24@
@H_403_24@
17 //{
18 // "?xml": { @H_403_24@
@H_403_24@
19 // "@version": "1.0",
20 // "@standalone": "no" @H_403_24@
@H_403_24@
21 // },monospace!important; font-size:10pt!important; min-height:inherit!important; display:block!important">22 // "root": {
@H_403_24@
23 // "person": [
24 // { @H_403_24@
@H_403_24@
25 // "@id": "1",monospace!important; font-size:10pt!important; min-height:inherit!important; display:block!important">26 // "name": "Alan",
@H_403_24@
27 // "url": "http://www.google.com"
28 29 // { @H_403_24@
30 // "@id": "2",monospace!important; font-size:10pt!important; min-height:inherit!important; display:block!important">31 // "name": "Louis",monospace!important; font-size:10pt!important; min-height:inherit!important; display:block!important">32 // "url": "http://www.yahoo.com" @H_403_24@
@H_403_24@
33 // }
34 // ] @H_403_24@
@H_403_24@
35 36 //}
@H_535_403@ [代码]JSON TO XML
json =@"{ @H_403_24@
""?xml"": { @H_403_24@
@H_403_24@
""@version"": ""1.0"",monospace!important; font-size:10pt!important; min-height:inherit!important; color:blue!important">""@standalone"": ""no""
@H_403_24@
},monospace!important; font-size:10pt!important; min-height:inherit!important; color:blue!important">""root"": {
@H_403_24@
""person"": [
{ @H_403_24@
@H_403_24@
""@id"": ""1"",monospace!important; font-size:10pt!important; min-height:inherit!important; color:blue!important">""name"": ""Alan"",monospace!important; font-size:10pt!important; min-height:inherit!important; color:blue!important">""url"": ""http://www.google.com""
{ @H_403_24@
""@id"": ""2"",monospace!important; font-size:10pt!important; min-height:inherit!important; color:blue!important">""name"": ""Louis"",monospace!important; font-size:10pt!important; min-height:inherit!important; color:blue!important">""url"": ""http://www.yahoo.com"" @H_403_24@
@H_403_24@
}
] @H_403_24@
@H_403_24@
}";
@H_403_24@
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
@H_403_24@
// <?xml version="1.0" standalone="no"?>
// <root> @H_403_24@
@H_403_24@
// <person id="1">
// <name>Alan</name> @H_403_24@
@H_403_24@
// <url>http://www.google.com</url>
// </person> @H_403_24@
@H_403_24@
// <person id="2">
// <name>Louis</name> @H_403_24@
@H_403_24@
// <url>http://www.yahoo.com</url>
// </root> @H_403_24@
@H_535_403@ [代码]DEMO:JSON TO XML
@H_403_24@
json_str ="{\"a\":\"a\",\"b\":\"b\"}"//json 的字符串需要按照这个格式 书写,否则会报错
@H_403_24@
""root"":"+ json_str + "}";
@H_403_24@
if(!.IsNullOrEmpty(json)) @H_403_24@
@H_403_24@
{
XmlDocument doc = JsonConvert.DeserializeXmlNode(json); @H_403_24@
@H_403_24@
}

猜你在找的Json相关文章