问题描述
出于某种原因,当我在我的淘汰模型中有一个特殊字符并将其转换为json对象时,字符串将结束特殊字符所在的位置,并且在反序列化时出现错误:
For some reason, when I have a special character in my knockout model and convert it to a json object, the string ends where the special character is supposed to be and I get an error when deserializing it:
$.ajax({
url: "/Admin/Forms/Convert",
type: "post",
//contentType: "application/json",
dataType: "text",
data: "modelData=" + ko.toJSON(theModel),
success: function (data) {
// window.open("/Admin/Forms/DisplayClient");
var win = getFullWindow('/Admin/Forms/DisplayClient');
win.open();
},
error: function (xhr, status, msg) { alert(msg); }
});
当我使用这种方法时:
public void Convert(string modelData)
{
Form form = JsonConvert.DeserializeObject<Form>(modelData);
}
我收到错误消息:
Unterminated string. Expected delimiter: ". Path 'Name', line 1, position 178.
推荐答案
如果JSON字符串包含特殊字符,如双引号,则反斜杠
\
或斜杠 /
,需要使用反斜杠 \
进行转义。没有JSON解析器能够处理首先没有正确格式化的JSON字符串。
If a JSON string contains special characters like double quotes "
, backslashes \
or slashes /
, they need to be escaped with backslashes \
. There is no JSON parser that will be able to deal with a JSON string that isn't properly formatted in the first place.
所以你需要确保你的 JSON.org 标准,code> theModel 的格式正确。
So you need to make sure that your theModel
is formatted appropriately and according to JSON.org standards.
这篇关于JsonConvert.DeserializeObject特殊字符未终止字符串。预期分隔符:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!