如何删除转义序列以格式化为适当的json?

时间:2017-07-10 01:55:09

标签: json stringify

我在帖子中将一个字符串数组传递给服务器。该字符串实际上是使用json.stringify

转换为字符串的对象
#include "Python.h"

我的控制器代码是

var editedRows[];

editedRows.push(117);
editedRows.push(123);

var dataItems = [];

for (var i = 0; i < editedRows.length; i++) {

            var item = grid.dataSource.get(editedRows[i]);
            dataItems.push(JSON.stringify(item));
        }

var model = JSON.stringify({ 'model': dataItems });

$.ajax({
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            type: 'POST',
            url: "@Url.Action("SaveResults", "Maps")",
            data: model ,
            success: function () { },
            failure: function (response) { }
        });

在服务器端(s的值为),我收到

public int SaveResults(List<string> model)
    {
      foreach (var s in model)
        {
          var serializer = new JsonSerializer();
          ShortDetail shortDetail = serializer.Deserialize<ShortDetail>(new JsonTextReader(new StringReader(s)));
         }
      return 0;}

虽然预期是

"{\"firstname\":\"Jacob\",\"surname\":\"Price\",\"preferredname\":\"\",\"classid\":114,\"classname\":\"5 A\",\"studentid\":140,\"Age\":\"12y4m\",\"male\":true,\"indicatorID\":null,\"indicatorDescription\":null,\"results\":[{\"ResultID\":11741,\"StudentID\":140,\"ClassID\":114,\"YearID\":22,\"assessmentId\":49,\"ResultValue\":\"94.0\",\"shortdescription\":\"Accuracy %\",\"Term\":1,\"Month\":0,\"Week\":0,\"Semester\":1,\"ResultTypeID\":14,\"NotesLong\":\"{\\\\rtf1\\\\ansi\\n{\\\\rtf1\\\\ansi\\n{\\\\pard\\n{\\\\pard\\\\par}\\n\\n\\\\par}\\n\\n}\\n}\",\"Color\":\"ffc080\",\"AllowNumber\":true,\"AllowLetter\":false,\"AllowAll\":false,\"AgeLevel\":false,\"SelectFromList\":false,\"RowOrder\":17,\"MinNumber\":0,\"MaxNumber\":100,\"DecimalPoints\":null,\"AllowedValues\":\"\",\"ColumnAlignment\":2,\"ColumnWidth\":20,\"Updated\":true,\"InActive\":false,\"SchoolID\":0,\"FormattingEnabled\":true,\"AssessmentName\":\"LITERACY - PROBE Fiction\",\"YearTermSemester\":\"2015 S1 T1\",\"MaxLength\":\"0 - 100\",\"MapDetailId\":2532},{\"ResultID\":11741,\"StudentID\":140,\"ClassID\":114,\"YearID\":22,\"assessmentId\":49,\"ResultValue\":\"13.0-14.0\",\"shortdescription\":\"Read Age\",\"Term\":1,\"Month\":0,\"Week\":0,\"Semester\":1,\"ResultTypeID\":22,\"NotesLong\":\"{\\\\rtf1\\\\ansi\\n{\\\\rtf1\\\\ansi\\n{\\\\pard\\n{\\\\pard\\\\par}\\n\\n\\\\par}\\n\\n}\\n}\",\"Color\":\"ffffff\",\"AllowNumber\":false,\"AllowLetter\":false,\"AllowAll\":true,\"AgeLevel\":false,\"SelectFromList\":false,\"RowOrder\":23,\"MinNumber\":0,\"MaxNumber\":0,\"DecimalPoints\":null,\"MapDetailId\":2532}]}"

如何删除转义序列以格式化为正确的json?

1 个答案:

答案 0 :(得分:0)

这真是我的坏......我在传递项目之前在另一个函数中做了JSON.stringify。所以它被串联三次:( ..现在一切都好!