将JSON转换为XML [英] Convert JSON to XML
问题描述
我知道这个问题已经问了很多遍了.
I know this has been asked many many times.
但坦白说,经过这么多尝试,解决方案对我不起作用.
But honestly with so many tries, the solutions are not working for me.
由于我在 ASP.Net 1.1 中工作,因此无法处理此.net级别.
As I am working in ASP.Net 1.1, so can not handle this .net level.
所以我继续尝试了许多尝试的解决方案,即使用javascript库,jquery库来做到这一点.
So I went ahead a tried many solution i.e using javascript library, jquery library to do this.
某些工具正在转换,但是附加了< 0>,< 1>< 2> 等.在执行LoadXml时会引发格式错误.
Some tool are converting, but appending <0>, <1> <2> and so on..Which while doing LoadXml is throwing format error.
我需要一个简单的函数或一个好的库,可以将JSON对象转换为XML.
I need a simple function or a good library which can convert a JSON object to XML.
请提出建议.
我已经尝试过使用这些来列举一些:
I have tried these to name a few :
编辑
我已经删除了中间的JSON和XML数据,因为它非常庞大:
I have deleted the middle JSON and XML data as it was huge :
JSON
"{\"CrashTestResult\":[[\"Crash Test Note\",\"Results based on a 35 MPH frontal crash and 38.5 MPH side crash. Results are reported in a range of one to five stars, with five stars indicating the best crash protection for vehicles within \\"TIRES, P265/70R17 ALL-SEASON, BLACKWALL\",\"$0.00\"],[\"TIRES\",\"TIRES, P265/70R17 ON-/OFF-ROAD, WHITE OUTLINED-LETTER\",\"$125.00\"],[\"TIRES\",\"TIRES, P265/70R17 ON-/OFF-ROAD, BLACKWALL\",\"$150.00\"],[\"TIRES\",\"TIRES, P265/70R17 ALL-SEASON, WHITE OUTLINED-LETTER\",\"$125.00\"],[\"TIRES\",\"TIRES, P275/55R20 ALL-SEASON, BLACKWALL\",\"$0.00\"],[\"Drivetrain Years\",\"5\"],[\"Roadside Assistance Miles/km\",\"100,000\"],[\"Roadside Assistance Years\",\"5\"]],\"Wheel\":[\"WHEELS, 4 - 17\\\" X 7.5\\\" (43.2 CM X 19.1 CM) ALUMINUM, 5-SPOKE\",\"WHEELS, 4 - 17\\\" X 7.5\\\" (43.2 CM X 19.1 CM) ALUMINUM, 5-SPOKE\",\"WHEELS, 4 - 20\\\" X 8.5\\\" (50.8 CM X 21.6 CM) PAINTED ALUMINUM\"]}"
想要
"<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<VINDescription xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n <CrashTestResult xmlns=\"http://autoexact.com/VINDecoder/\">\r\n <ArrayOfstring xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">\r\n <string>Crash Test Note</string>\r\n <string>Results based on a 35 MPH frontal crash and 38.5 MPH side crash. Results are reported in a range of one to five stars, with five stars indicating the best crash protection for vehicles within the same weight class. This test used driver and passenger belts and airbags.</string>\r\n </ArrayOfstring>\r\n <ArrayOfstring xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">\r\n <string>Frontal Driver</string>\r\n <string>* * * *</string>\r\n </ArrayOfstring>\r\n <ArrayOfstring xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">\r\n <string>Frontal Passenger</string>\r\n <string>* * * *</string>\r\n </ArrayOfstring>\r\n<string>$2,755.00</string>\r\n </ArrayOfstring>\r\n <ArrayOfstring xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">\r\n <string>CPOS PKG</string>\r\n <string>28F CUSTOMER PREmas.microsoft.com/2003/10/Serialization/Arrays\">\r\n <string>Basic Miles/km</string>\r\n <string>36,000</string>\r\n </ArrayOfstring>\r\n <ArrayOfstring xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">\r\n <string>Basic Years</string>\r\n <string>3</string>\r\n </ArrayOfstring>\r\n <ArrayOfstring xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">\r\n <string>Corrosion Miles/km</string>\r\n <string>100,000</string>\r\n </ArrayOfstring>\r\n <ArrayOfstring xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">\r\n <string>Corrosion Years</string>\r\n <string>5</string>\r\n </ArrayOfstring>\r\n <ArrayOfstring xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">\r\n <string>Drivetrain Miles/km</string>\r\n <string>36,000</string>\r\n </ArrayOfstring>\r\n <ArrayOfstring xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">\r\n <string>Drivetrain Note</string>\r\n <string>Unlimited Years/Unlimited Miles for vehicles sold after 07/26/2007</string>\r\n </ArrayOfstring>\r\n <ArrayOfstring xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">\r\n <string>Drivetrain Years</string>\r\n <string>3</string>\r\n </ArrayOfstring>\r\n </Warranty>\r\n <Wheel xmlns=\"http://autoexact.com/VINDecoder/\" />\r\n</VINDescription>"
推荐答案
尝试这样的事情.这对我的应用程序非常有用,但是没有在它们之外使用过,因此希望对您有所帮助.
Hi try something like this. This works great for my applications, but haven't used it outside them so hope it helps.
public string ParseJsonToXml(string sJsonObject)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(string));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sXMLJson));
string sXMLDeSerialized = (string)ser.ReadObject(ms);
ms.Close();
return sXMLDeSerialized;
}
我将其解析为字符串,以便可以将xml转换为数据集,反之亦然.
I parse it to string in order so that i can convert the xml into a dataset and vice versa.
public DataSet ParseXmlToDataSet(string sXml)
{
DataSet ds = new DataSet();
StringReader sr = new StringReader(sXml);
ds.ReadXml(sr);
return ds;
}
使用
//ds is typeof(DataSet)
ds.GetXml();
注意: 这就是我处理的数据的样子
Note: This is how my data processed looks like
<DATA>
<TLOGIN>
<USER>thabo.pali@za.pwc.com</USER>
<PASSWORD>"Monday@01</PASSWORD>
</TLOGIN>
</DATA>
关于乔治
这篇关于将JSON转换为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!