首页 > 解决方案 > 如何在 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); 

标签: c#jsonjson.netescaping

解决方案


推荐阅读