由于ajax请求,我得到了一个json字符串。 我需要使用javascript / jquery将json转换为xml字符串。 请帮我。 Json字符串如下:
var json =
"{
"Data": {
"SOM": {
"Tab": [
{
"Values": {
"SelectedValues": null,
"LoadedValues": null,
"ExpandedValues": null,
"ID": "msorgrole"
},
"ID": "OrgRole"
},
{
"Values": {
"SelectedValues": null,
"LoadedValues": null,
"ExpandedValues": null,
"ID": "msorg"
},
"ID": "Organization"
},
{
"Values": {
"SelectedValues": null,
"LoadedValues": null,
"ExpandedValues": null,
"ID": "mscontenttype"
},
"ID": "PeopleType"
},
{
"Values": {
"SelectedValues": ",B79720D5-0E95-4CB7-B4F9-37BE24696F4F,831A2A77-B758-493A-B0F4-991A6427C31C,",
"LoadedValues": null,
"ExpandedValues": null,
"ID": "mspeople"
},
"ID": "People"
}
]
}
}
}"
我需要转换上面的json字符串,如下所示:
var json = "<Data><SOM><Tab ID="OrgRole"> <Values ID="msorgrole"><SelectedValues /> <LoadedValues /> <ExpandedValues /></Values></Tab><Tab ID="Organization"> <Values ID="msorg"><SelectedValues /> <LoadedValues /> <ExpandedValues /></Values></Tab><Tab ID="PeopleType"> <Values ID="mscontenttype"><SelectedValues /> <LoadedValues /> <ExpandedValues /></Values></Tab><Tab ID="People"> <Values ID="mspeople"><SelectedValues>,831A2A77-B758-493A-B0F4-991A6427C31C,B79720D5-0E95-4CB7-B4F9-37BE24696F4F,</SelectedValues> <LoadedValues /> <ExpandedValues /></Values></Tab></SOM></Data>"
答案 0 :(得分:1)
您可以使用json2xml
函数
var data = '{"Data":{"SOM":{"Tab":[{"Values":{"SelectedValues":null,"LoadedValues":null,"ExpandedValues":null,"ID":"msorgrole"},"ID":"OrgRole"},{"Values":{"SelectedValues":null,"LoadedValues":null,"ExpandedValues":null,"ID":"msorg"},"ID":"Organization"},{"Values":{"SelectedValues":null,"LoadedValues":null,"ExpandedValues":null,"ID":"mscontenttype"},"ID":"PeopleType"},{"Values":{"SelectedValues":",B79720D5-0E95-4CB7-B4F9-37BE24696F4F,831A2A77-B758-493A-B0F4-991A6427C31C,","LoadedValues":null,"ExpandedValues":null,"ID":"mspeople"},"ID":"People"}]}}}';
var jsonObj = JSON.parse(data); // important to first convert json string into object
alert(json2xml(jsonObj));
<script src="http://goessner.net/download/prj/jsonxml/json2xml.js"></script>
答案 1 :(得分:0)
你可以使用这个插件非常有效: goessner
答案 2 :(得分:0)
这会对您有所帮助:jasontoxml
答案 3 :(得分:0)
查看此https://github.com/javadev/xml-to-json
我是图书馆的维护者。
var xml = jsonToXml("{}");
// <?xml version="1.0" encoding="UTF-8"?>
// <root></root>