c# - 如何在 JSON 中转义特殊字符
问题描述
我需要将 JSON 对象绑定到模型类。为了实现这一点,我使用了以下代码行。
我测试的JSON如下
{
"Message":null,
"Error":false,
"VData":{
"RNumber":null,
"BRNumber":"Session1"
},
"onlineFields":{
"CCode":"Web",
"MNumber":"15478655",
"Product":"100",
"JsonFile":{
"evaluation":{
"number":[
{
"@paraID":"1000",
"@Value":"",
"@label":"We are america"
},
{
"@paraID":"2000",
"@Value":"100",
"@label":"We are japan"
},
{
"@paraID":"3000",
"@Value":"1000",
"@label":"We are UK"
},
{
"@paraID":"4000",
"@Value":"",
"@label":"We are China"
}
]
}
}
}
}
这是我的模型课。
public class VData
{
public object RNumber { get; set; }
public string BRNumber { get; set; }
}
public class Number
{
[JsonProperty("@paraID")]
public string paraID { get; set; }
[JsonProperty("@Value")]
public string Value { get; set; }
[JsonProperty("@label")]
public string label { get; set; }
}
public class Evaluation
{
public List<Number> number { get; set; }
}
public class JsonFile
{
public Evaluation evaluation { get; set; }
}
public class OnlineFields
{
public string CCode { get; set; }
public string MNumber { get; set; }
public string Product { get; set; }
public JsonFile JsonFile { get; set; }
}
public class Response
{
public object Message { get; set; }
public bool Error { get; set; }
public VData VData { get; set; }
public OnlineFields onlineFields { get; set; }
}
要将上面的 JSON 设置为我的模型,我使用了以下代码
private static void showJSON(string testJson){
Response response = JsonConvert.DeserializeObject<Response>(testJson);
var dropdowns = response.OnlineFields.JsonFile;
string json = JsonConvert.SerializeObject(dropdowns, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(json);
}
这工作正常。但问题是我的客户通过 API 向我发送了以下 JSON。
{
"Message":null,
"Error":false,
"VData":{
"RNumber":null,
"BRNumber":"Session1"
},
"onlineFields":{
"CCode":"Web",
"MNumber":"15478655",
"Product":"100",
"JsonFile":" {
\"evaluation\":{
\"number\":[
{
\"@paraID\":\"1000\",
\"@Value\":\"\",
\"@label\":\"We are america\"
},
{
\"@paraID\":\"2000\",
\"@Value\":\"100\",
\"@label\":\"We are japan\"
},
{
\"@paraID\":\"3000\",
\"@Value\":\"1000\",
\"@label\":\"We are UK\"
},
{
\"@paraID\":\"4000\",
\"@Value\":\"\",
\"@label\":\"We are China\"
}
]
}
} "
}
}
那么如何转义这个“ \
”字符以及如何将客户端发送的 JSON 绑定到我的模型。在客户端的 JSON 中有称为 JsonFile 的内部 JSON。这个内部 JSON 由 " \
" 组成,内部 JSON 由引号组成""
。所以请给我一个解决方案。
更新:
根据这个帖子。我试过这个。但这让我犯了错误。所以请给我一个解决方案。为了那个帖子或任何其他方式如何解决这个问题
dynamic obj = JsonConvert.DeserializeObject(json);
foreach (var response in (IEnumerable<dynamic>)obj.onlineFields)
{
response.onlineFields.JsonFile = JsonConvert.DeserializeObject((string)response.onlineFields.JsonFile);
}
string result = JsonConvert.SerializeObject(obj);
Console.WriteLine(result);
解决方案
推荐阅读
- python - 如何减少 sqlalchemy 用于连接数据库和选择数据的时间
- rest - 使用 keycloak rest admin apis 获取给定部分 url 路径或基于正则表达式模式的资源
- java - com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造`javax.xml.bind.JAXBElement`的实例
- python - Luis python SDK 话语添加
- jmeter - 为什么在 Jmeter 中请求时添加“&”?
- php - 使用 PHP 将时间戳数组转换为 MongoDB ObjectId
- r - 具有因子水平的双色热图
- node.js - 使用 FireStore 云功能进行课程的条带化支付
- javascript - 如何将 React js 项目从 15.6 更新到 16.8?
- javascript - minlength 验证器在猫鼬中不起作用