C#中使用JSON.NET實(shí)現(xiàn)JSON、XML相互轉(zhuǎn)換
官方 JSON.NET 地址
http://james.newtonking.com/pages/json-net.aspx
XML TO JSON
string xml = @"<?xml version=""1.0"" standalone=""no""?>
<root>
<person id=""1"">
<name>Alan</name>
<url>http://www.google.com</url>
</person>
<person id=""2"">
<name>Louis</name>
<url>http://www.yahoo.com</url>
</person>
</root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);
//{
// "?xml": {
// "@version": "1.0",
// "@standalone": "no"
// },
// "root": {
// "person": [
// {
// "@id": "1",
// "name": "Alan",
// "url": "http://www.google.com"
// },
// {
// "@id": "2",
// "name": "Louis",
// "url": "http://www.yahoo.com"
// }
// ]
// }
//}
JSON TO XML
string json = @"{
""?xml"": {
""@version"": ""1.0"",
""@standalone"": ""no""
},
""root"": {
""person"": [
{
""@id"": ""1"",
""name"": ""Alan"",
""url"": ""http://www.google.com""
},
{
""@id"": ""2"",
""name"": ""Louis"",
""url"": ""http://www.yahoo.com""
}
]
}
}";
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
// <?xml version="1.0" standalone="no"?>
// <root>
// <person id="1">
// <name>Alan</name>
// <url>http://www.google.com</url>
// </person>
// <person id="2">
// <name>Louis</name>
// <url>http://www.yahoo.com</url>
// </person>
// </root>
DEMO:JSON TO XML
string json_str = "{\"a\":\"a\",\"b\":\"b\"}";
//json 的字符串需要按照這個(gè)格式 書寫,否則會(huì)報(bào)錯(cuò)
string json = @"{
""?xml"": {
""@version"": ""1.0"",
""@standalone"": ""no""
},
""root"":" + json_str + "}";
if (!string.IsNullOrEmpty(json))
{
XmlDocument doc = JsonConvert.DeserializeXmlNode(json);
}
- C#中的DataSet、string、DataTable、對(duì)象轉(zhuǎn)換成Json的實(shí)現(xiàn)代碼
- C#實(shí)現(xiàn)JSON和對(duì)象之間互相轉(zhuǎn)換功能示例
- C#實(shí)現(xiàn)集合轉(zhuǎn)換成json格式數(shù)據(jù)的方法
- C#實(shí)現(xiàn)json格式轉(zhuǎn)換成對(duì)象并更換key的方法
- C#實(shí)現(xiàn)將json轉(zhuǎn)換為DataTable的方法
- C#實(shí)現(xiàn)Json轉(zhuǎn)Unicode的方法
- C#中把Datatable轉(zhuǎn)換為Json的5個(gè)代碼實(shí)例
- C# XML與Json之間相互轉(zhuǎn)換實(shí)例詳解
- C#實(shí)現(xiàn)任意數(shù)據(jù)類型轉(zhuǎn)成json格式輸出
- 自定義實(shí)現(xiàn)Json字符串向C#對(duì)象轉(zhuǎn)變的方法
- C#實(shí)現(xiàn)String類型和json之間的相互轉(zhuǎn)換功能示例
相關(guān)文章
C#實(shí)現(xiàn)Word轉(zhuǎn)換TXT的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)Word轉(zhuǎn)換TXT的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
C# 動(dòng)態(tài)輸出Dos命令執(zhí)行結(jié)果的實(shí)例(附源碼)
這篇文章主要介紹了C# 動(dòng)態(tài)輸出Dos命令執(zhí)行結(jié)果的實(shí)例,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
Winform學(xué)生信息管理系統(tǒng)登陸窗體設(shè)計(jì)(1)
這篇文章主要為大家詳細(xì)介紹了Winform學(xué)生信息管理系統(tǒng)登陸窗體設(shè)計(jì)思路,感興趣的小伙伴們可以參考一下2016-05-05
C#基于Linq和反射實(shí)現(xiàn)數(shù)據(jù)持久化框架Xml4DB詳解
在本篇文章里小編給大家整理的是關(guān)于C#基于Linq和反射實(shí)現(xiàn)數(shù)據(jù)持久化框架Xml4DB相關(guān)知識(shí)點(diǎn),有需要的朋友們學(xué)習(xí)下。2019-08-08
C#獲取進(jìn)程的主窗口句柄的實(shí)現(xiàn)方法
C#獲取進(jìn)程的主窗口句柄的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-04-04
通過VS中的數(shù)據(jù)源選擇對(duì)話框簡單實(shí)現(xiàn)數(shù)據(jù)庫連接配置[圖]
通過VS中的數(shù)據(jù)源選擇對(duì)話框簡單實(shí)現(xiàn)數(shù)據(jù)庫連接配置[圖]...2007-03-03

