我想通过JSON传递一个文件路径。在反序列化时,我得到了错误:
无法识别的逃逸序列。(43):{"Jobtype":"StepBatch","SelectedId":"D:\Input\file1.CATPart"}
我已经转义了角色,但它仍然显示error...am,我在这里遗漏了什么?
string json = "{\"Jobtype\": \"StepBatch\",\"SelectedId\": \"D:\\Input\\file1.CATPart\"}";
var jsonObj = new JavaScriptSerializer().Deserialize<List<Arguments>>(json);发布于 2019-02-11 16:01:01
问题是在执行时字符串的内容是:
{"Jobtype": "StepBatch","SelectedId": "D:\Input\file1.CATPart"}这是无效的JSON,因为SelectedId的值中有反斜杠。您需要JSON是:
{"Jobtype": "StepBatch","SelectedId": "D:\\Input\\file1.CATPart"}所以你的C#必须是:
string json = "{\"Jobtype\": \"StepBatch\",\"SelectedId\": \"D:\\\\Input\\\\file1.CATPart\"}";但是,考虑到您无论如何都要立即反序列化JSON,我建议完全消除JSON部分,只需要自己创建Arguments值。
如果您需要生成JSON,直接创建正确的值,然后获得JavaScriptSerializer (或者更好的Json.NET)来为您创建JSON,而不是手工编码它。
https://stackoverflow.com/questions/54634386
复制相似问题