如何在JSON stringify中使用特殊字符?

时间:2021-08-22 15:29:35

I am trying to stringify my json code for sending it to MVC controller. But it does not work when data contains some special characters like greater than > or less than sign <.

我试图将我的json代码字符串化,以便将其发送到MVC控制器。但是当数据包含一些特殊字符(例如大于>或小于符号<)时,它不起作用。

Here is Sample code

这是示例代码

 function demo()
 {
     debugger
     var demo = [];
     demo.one = 'one';
     demo.two = '<just>'
     var treeBinding = JSON.stringify(demo);
     $.ajax({
         url: '/flow/demo',
         type: "GET",
         data: { dd: treeBinding },
         success: function (res) {

         },
         error: function (error) {
             alert(error)
         }
     });
 }

JSON.stringify returns a blank array in this case. Can anyone help me to get it worked?

在这种情况下,JSON.stringify返回一个空数组。任何人都可以帮我搞定吗?

2 个解决方案

#1


2  

First of all your declaration with array is incorrect.That is supposed to be an object but whatever case you need to check difference between object and array.However I assume that demo is an object with two key/properties which will be sent to server.

首先你的数组声明是不正确的。这应该是一个对象,但无论如何你需要检查对象和数组之间的区别。但是我假设demo是一个具有两个键/属性的对象,将被发送到服务器。

So declaration should look like this-

所以声明应该像这样 -

     var demo = {};
     demo.one = 'one';
     demo.two = '<just>';

Then you should use to escape -

然后你应该用来逃避 -

var treeBinding = encodeURIComponent(JSON.stringify(demo));

#2


0  

You can try something like this:

你可以尝试这样的事情:

function arrayToObjectString(arr) {
  var returnSrt = "{";
  for (var key in arr) {
    returnSrt += "\"" + key + "\" : \"" + arr[key] + "\"";
    returnSrt += ","
  }
  returnSrt = returnSrt.substring(0, returnSrt.length - 1) + "}";

  return returnSrt;
}

function main() {
  var demo = [];
  demo.one = 'one';
  demo.two = '<just>'

  console.log(JSON.stringify(demo))

  var resultStr = arrayToObjectString(demo);
  
  console.log(resultStr)
  console.log(JSON.parse(resultStr));
}

main();

#1


2  

First of all your declaration with array is incorrect.That is supposed to be an object but whatever case you need to check difference between object and array.However I assume that demo is an object with two key/properties which will be sent to server.

首先你的数组声明是不正确的。这应该是一个对象,但无论如何你需要检查对象和数组之间的区别。但是我假设demo是一个具有两个键/属性的对象,将被发送到服务器。

So declaration should look like this-

所以声明应该像这样 -

     var demo = {};
     demo.one = 'one';
     demo.two = '<just>';

Then you should use to escape -

然后你应该用来逃避 -

var treeBinding = encodeURIComponent(JSON.stringify(demo));

#2


0  

You can try something like this:

你可以尝试这样的事情:

function arrayToObjectString(arr) {
  var returnSrt = "{";
  for (var key in arr) {
    returnSrt += "\"" + key + "\" : \"" + arr[key] + "\"";
    returnSrt += ","
  }
  returnSrt = returnSrt.substring(0, returnSrt.length - 1) + "}";

  return returnSrt;
}

function main() {
  var demo = [];
  demo.one = 'one';
  demo.two = '<just>'

  console.log(JSON.stringify(demo))

  var resultStr = arrayToObjectString(demo);
  
  console.log(resultStr)
  console.log(JSON.parse(resultStr));
}

main();