jquery - convert json string to xml string in javascript - Stack Overflow

I got a json string as a result of ajax request.And I need to convert json into xml string using javas

I got a json string as a result of ajax request. And I need to convert json into xml string using javascript/jquery. Please help me. Json string like below:

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"
            }
          ]
        }
      }
    }"

I need to convert the above json string like below:

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>"

I got a json string as a result of ajax request. And I need to convert json into xml string using javascript/jquery. Please help me. Json string like below:

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"
            }
          ]
        }
      }
    }"

I need to convert the above json string like below:

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>"
Share Improve this question edited Feb 7, 2017 at 10:07 sai v asked Feb 7, 2017 at 9:47 sai vsai v 1831 gold badge1 silver badge13 bronze badges 2
  • Can you please try the below link stackoverflow./questions/7769829/… – nagendrakumarkasam Commented Feb 7, 2017 at 9:57
  • i just asked json to xml not "xml to json" – sai v Commented Feb 7, 2017 at 10:01
Add a ment  | 

4 Answers 4

Reset to default 2

You can use http://goessner/download/prj/jsonxml/ like this using the function 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/download/prj/jsonxml/json2xml.js"></script>

You can use this plugin the it is very effective : goessner

This will help you : jasontoxml

Check out this https://github./javadev/xml-to-json

I am the maintainer of the library.

var xml = jsonToXml("{}");

// <?xml version="1.0" encoding="UTF-8"?>
// <root></root>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744939074a4602209.html

相关推荐

  • jquery - convert json string to xml string in javascript - Stack Overflow

    I got a json string as a result of ajax request.And I need to convert json into xml string using javas

    1天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信