Convert XML to JSON (and back) using Javascript

后端 未结 12 1353
Happy的楠姐
Happy的楠姐 2020-11-22 03:18

How would you convert from XML to JSON and then back to XML?

The following tools work quite well, but aren\'t completely consistent:

  • xml2json
12条回答
  •  有刺的猬
    2020-11-22 03:27

    I would personally recommend this tool. It is an XML to JSON converter.

    It is very lightweight and is in pure JavaScript. It needs no dependencies. You can simply add the functions to your code and use it as you wish.

    It also takes the XML attributes into considerations.

    var xml = ‘John Doe’;
    var json = xml2json(xml); 
    
    console.log(json); 
    // prints ‘{“person”: {“id”: “1234”, “age”: “30”, “name”: “John Doe”}}’
    

    Here's an online demo!

提交回复
热议问题