0
我正在傳遞一個字符串數組到服務器中。此字符串實際上是一個對象透過JSON.stringify我如何刪除轉義序列以格式化爲正確的json?
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) { }
});
我的控制器代碼是
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;}
在服務器側(一個或多個的值是)中,i接收
"{\"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}]}"
轉換成字符串雖然預計是
{"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{\\pard\n{\\pard\\par}\n\n\\par}\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{\\pard\n{\\pard\\par}\n\n\\par}\n\n}","Color":"FFFFFF","AllowNumber":false,"AllowLetter":false,"AllowAll":true,"AgeLevel":false,"SelectFromList":false,"RowOrder":23,"MinNumber":0,"MaxNumber":0,"DecimalPoints":null,"AllowedValues":"4.5-5Û4.5Û","ColumnAlignment":0,"ColumnWidth":10,"Updated":true,"InActive":false,"SchoolID":0,"FormattingEnabled":false,"AssessmentName":"LITERACY - PROBE Fiction","YearTermSemester":"2015 S1 T1","MaxLength":"Any value","MapDetailId":2532},{"ResultID":11741,"StudentID":140,"ClassID":114,"YearID":22,"assessmentId":49,"ResultValue":"10.00","shortdescription":"Stud Age","Term":1,"Month":0,"Week":0,"Semester":1,"ResultTypeID":23,"NotesLong":"{\\rtf1\\ansi\n{\\pard\n{\\pard\\par}\n\n\\par}\n\n}","Color":"FFFFFF","AllowNumber":true,"AllowLetter":false,"AllowAll":false,"AgeLevel":false,"SelectFromList":false,"RowOrder":24,"MinNumber":0,"MaxNumber":0,"DecimalPoints":null,"AllowedValues":"","ColumnAlignment":0,"ColumnWidth":8,"Updated":true,"InActive":false,"SchoolID":0,"FormattingEnabled":false,"AssessmentName":"LITERACY - PROBE Fiction","YearTermSemester":"2015 S1 T1","MaxLength":"1 - 17","MapDetailId":2532},{"ResultID":11741,"StudentID":140,"ClassID":114,"YearID":22,"assessmentId":49,"ResultValue":"Green Star - Blank","shortdescription":"Hom Lev Pr","Term":1,"Month":0,"Week":0,"Semester":1,"ResultTypeID":24,"NotesLong":"{\\rtf1\\ansi\n{\\pard\n{\\pard\\par}\n\n\\par}\n\n}","Color":"FF8080","AllowNumber":false,"AllowLetter":false,"AllowAll":false,"AgeLevel":false,"SelectFromList":true,"RowOrder":25,"MinNumber":0,"MaxNumber":0,"DecimalPoints":null,"AllowedValues":"Gold StarÛ1ÛÛRed Green DotÛ2ÛÛBlue StarÛ3ÛÛBronze StarÛ4ÛÛGreen Star - BlankÛ5Û","ColumnAlignment":0,"ColumnWidth":8,"Updated":true,"InActive":false,"SchoolID":0,"FormattingEnabled":true,"AssessmentName":"LITERACY - PROBE Fiction","YearTermSemester":"2015 S1 T1","MaxLength":"Select from list","MapDetailId":2532},{"ResultID":11741,"StudentID":140,"ClassID":114,"YearID":22,"assessmentId":49,"ResultValue":"2","shortdescription":"Determ Set","Term":1,"Month":0,"Week":0,"Semester":1,"ResultTypeID":25,"NotesLong":"{\\rtf1\\ansi\n{\\pard\n{\\pard\\par}\n\n\\par}\n\n}","Color":"FFFFFF","AllowNumber":false,"AllowLetter":false,"AllowAll":false,"AgeLevel":false,"SelectFromList":true,"RowOrder":26,"MinNumber":1,"MaxNumber":20,"DecimalPoints":null,"MapDetailId":2532}]}
如何刪除轉義序列以格式化爲正確的json?
更改*用戶端*不要打包兩遍:將'dataItems.push(JSON.stringify(item));'改成'dataItems.push(item)'。而不是繞着破碎的代碼工作,只要把它放在第一位(如果你真的完全必須做前者,做相反的事情:解析json,並映射到數組並解析每個元素。) – Amadan
Duplicate-but-for-accepted-answer:[解析節點js中的JSON字符串?]( https://stackoverflow.com/questions/44714715/parsing-a-json-string-in-node-js#comment76412714_44714715) – Amadan