2012-11-07 174 views
1

我知道這已被問了很多次。將JSON轉換爲XML

但老實說,有這麼多的嘗試,解決方案不適合我。

因爲我在ASP.Net 1.1,所以無法處理這個.net級別。

所以我使用的JavaScript庫,jQuery庫要做到這一點就提前嘗試了許多解決方案即

一些工具被轉換,但追加< 0>,< 1> < 2>等on..Which,而這樣做的loadXML拋出格式錯誤。

我需要一個簡單的函數或一個可以將JSON對象轉換爲XML的好庫。

請建議,如果有任何。

我嘗試了這些僅舉幾例:

This gave me the xml having <0>, <1> ,<2> sort of tags.

This Did not work at all.

編輯

我刪除了中間JSON和XML數據,因爲它是巨大的:

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

"<?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>" 
+1

是您的XML元素名稱「0」?因爲XML元素不能以數字開頭(所以顯然它們不能是數字)。這就是爲什麼你得到格式錯誤,沒有自尊的XML庫應該允許它。 –

+0

不......將json轉換爲xml之後...庫正在製作<0><1>標籤類型...這就是爲什麼我在尋找更好的庫或函數。 – James

+1

您能否提供一個源代碼JSON的示例以及您希望XML的外觀? –

回答

0

嗨嘗試這樣的事情。這對我的應用程序非常有用,但沒有在它們之外使用它,所以希望它有幫助。

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轉換成一個數據集,反之亦然。

public DataSet ParseXmlToDataSet(string sXml) 
    {  
     DataSet ds = new DataSet(); 
     StringReader sr = new StringReader(sXml); 
     ds.ReadXml(sr); 
     return ds; 
    } 

獲得的數據集的XML使用

//ds is typeof(DataSet) 
ds.GetXml(); 

注: 這是我的數據是如何處理的模樣

<DATA> 
<TLOGIN> 
    <USER>[email protected]</USER> 
    <PASSWORD>"[email protected]</PASSWORD> 
</TLOGIN> 
</DATA> 

問候喬治

+0

@George ...這是最簡單的方法來做到這一點..但我在ASP.NET 1.1 :( – James

+0

@Learner然後恐怕我不能幫助隊友。希望你找到你的答案。 –