如何将JSON转换为XML

时间:2021-09-06 23:47:17

I need to convert some JSON to XML with this library, I need to do that in order to send some data to the Database in a post request I am working on.

我需要使用这个库将一些JSON转换为XML,我需要这样做,以便在我正在处理的post请求中将一些数据发送到数据库。

This is what req.body returns

这就是req.body返回的内容

{ DealerName: 'amrcl',
  CardId: '123',
  Nickname: 'mkm123',
  Picture: 'http://lorempixel.com/150/150/',
  Active: '1',
  LegalId: '1',
  TypeId: '1'
}

is dynamic data. Let me show you the code

是动态数据。让我告诉你代码

export default function (req, res) {
  try {
    const connection = new sql.Connection(spConfig, function spConnection (errSpConnection) {
      const request = connection.request();
      if (errSpConnection) {
        res.status(401);
      }
      request.input('Dealer_Param', sql.VarChar(1000), req.body);
      request.input('param_IS_DEBUG', sql.Bit, null);
      request.output('output_IS_SUCCESSFUL', sql.Bit);
      request.output('output_STATUS', sql.VarChar(500));
      request.execute('[mydbo].[StoredProcedure]', function spExecution (errSpExecution, dataset) {
        connection.close();
        if (errSpExecution) {
          res.status(401);
        } else {
          if (request.parameters.output_IS_SUCCESSFUL.value) {
            res.status(200).json({
              success : 'New dealer successfully inserted.',
            });
          }
        }
      });
    });
  }
}

that is for Stored Procedure method which is with the mssql module.

这是与mssql模块一起使用的存储过程方法。

As you see the code above, there is a failure error, because in request.input('Dealer_Param', sql.VarChar(1000), req.body); I am sending the JSON I paste at the beginning of the question. But if instead of req.body I put this XML with Dummy data '<Dealers><Detail DealerName = "TESTING123" CardId = "1222" NickName = "tester123" Active = "1" LegalId = "16545" TypeId = "1"></Detail></Dealers>' then everything works fine because the DB need to receive an XML.

正如您在上面看到的代码那样,出现了一个失败错误,因为在request.input('Dealer_Param',sql.VarChar(1000),req.body)中;我在问题的开头发送了JSON I paste。但是如果不是req.body,我把这个XML与Dummy数据' '然后一切正常,因为DB需要接收XML。

So, what are your recommendations, what should I do to put the JSON data as a XML ?

那么,您的建议是什么,我应该怎么做才能将JSON数据作为XML?

1 个解决方案

#1


2  

I would just load the json2xml library...

我只是加载json2xml库...

  • install $ npm install json2xml

    安装$ npm install json2xml

  • import the module in your code: var json2xml = require("json2xml");

    在代码中导入模块:var json2xml = require(“json2xml”);

and then convert the json to xml like so:

然后将json转换为xml,如下所示:

var key,
    attrs=[];

for (key in req.body) {
    if (req.body.hasOwnProperty(key)) {
        var obj = {};
        obj.key = req.body.key;
        attrs.push(obj);
    }
}

var dealerXml = json2xml({dealer:req.body, attr:attrs}, { attributes_key:'attr'}); 

request.input('Dealer_Param', sql.VarChar(1000), dealerXml);

#1


2  

I would just load the json2xml library...

我只是加载json2xml库...

  • install $ npm install json2xml

    安装$ npm install json2xml

  • import the module in your code: var json2xml = require("json2xml");

    在代码中导入模块:var json2xml = require(“json2xml”);

and then convert the json to xml like so:

然后将json转换为xml,如下所示:

var key,
    attrs=[];

for (key in req.body) {
    if (req.body.hasOwnProperty(key)) {
        var obj = {};
        obj.key = req.body.key;
        attrs.push(obj);
    }
}

var dealerXml = json2xml({dealer:req.body, attr:attrs}, { attributes_key:'attr'}); 

request.input('Dealer_Param', sql.VarChar(1000), dealerXml);