首页 > 解决方案 > C# 在将 JSON 数据写入文件时不删除反斜杠

问题描述

我正在尝试将一些数据添加到一个大的 json 文件中,因此我尝试在 C# 中执行此操作,我打开了文件,将数据写入其中,但是在将最终数据写入 .Json 文件时,即 JsonConverter 的字符串。 SerializeObject 返回有反斜杠,原始字符串没有反斜杠(它在仔细查看时没有出现,Text Visualizer但写入 .Json 文件的最终数据仍然有反斜杠。
这就是我正在查看的内容文本可视化器;

{
  "GID_0": "TUR",
  "NAME_0": "Turkey",
  "GID_1": "TUR.1_1",
  "NAME_1": "Adana",
  "NL_NAME_1": "",
  "GID_2": "TUR.1.1_1",
  "NAME_2": "Aladağ",
  "VARNAME_2": "",
  "NL_NAME_2": "",
  "TYPE_2": "District",
  "ENGTYPE_2": "District",
  "CC_2": "",
  "HASC_2": "TR.AA.AL",
  "NUFUS": "16653"
}


但是文件中的真实数据是这样的;

"{\r\n  \"GID_0\": \"TUR\",\r\n  \"NAME_0\": \"Turkey\",\r\n  \"GID_1\": \"TUR.1_1\",\r\n  \"NAME_1\": \"Adana\",\r\n  \"NL_NAME_1\": \"\",\r\n  \"GID_2\": \"TUR.1.10_1\",\r\n  \"NAME_2\": \"Aladağ\",\r\n  \"VARNAME_2\": \"\",\r\n  \"NL_NAME_2\": \"\",\r\n  \"TYPE_2\": \"District\",\r\n  \"ENGTYPE_2\": \"District\",\r\n  \"CC_2\": \"\",\r\n  \"HASC_2\": \"TR.AA.AS\",\r\n  \"NUFUS\": \"16653\"\r\n}"


这就是我尝试在代码中执行此操作的方式;

 using (StreamReader r = new StreamReader(@"D:\districts_of_turkey.json"))
            {
                string json = r.ReadToEnd();
                JObject results = JObject.Parse(json);

                foreach(var result in results["features"])
                {
                    string type = (string)result["type"];
                    string geometryType = (string)result["geometry"]["type"];
                    JArray geometryStr = JArray.FromObject(result["geometry"]["coordinates"]);
                    string properties = result["properties"].ToString();
                    var propertiesArray = JsonConvert.DeserializeObject<PropertiesForJSON>(properties);

                    for (int j = 0; j < districts.Count - 1; j++)
                    {
                        string district = districts[j].Ilce.Split('-')[0].Split('(')[1].TrimEnd(')').ToUpper(turkey);
                        string province = districts[j].Ilce.Split('-')[0].Split('(')[0].ToUpper(turkey);
                        if ((province == propertiesArray.NAME_1.ToUpper(turkey) || province == propertiesArray.NAME_1) && (district == propertiesArray.NAME_2.ToUpper(turkey) || district == propertiesArray.NAME_2))
                        {
                            propertiesArray.NUFUS = districts[j].Nufus;
                            lst.Add(propertiesArray);
                            break;
                        }else if(j == districts.Count - 2)
                        {
                            exceptions.Add("İL = " + propertiesArray.NAME_1 + " // İLÇE = " + propertiesArray.NAME_2);
                        }
                    }
                    /*
                     {"GID_0":"TUR","NAME_0":"Turkey","GID_1":"TUR.32_1","NAME_1":"Eskişehir","NL_NAME_1":"","GID_2":"TUR.32.10_1","NAME_2":"Mihalıççık","VARNAME_2":"","NL_NAME_2":"","TYPE_2":"District","ENGTYPE_2":"District","CC_2":"","HASC_2":"TR.ES.MK"}
                     */
                    string propertyStr = JsonConvert.SerializeObject(propertiesArray);
                    propertyStr = removeBackSlash(JToken.Parse(propertyStr).ToString());

                    string propertyStr = JsonConvert.SerializeObject(propertiesArray);
                    result["properties"] = propertyStr;
                }

                File.WriteAllText(@"D:\districts_with_populationtwo.json", results.ToString());
            }


public class PropertiesForJSON
{
    public string GID_0;
    public string NAME_0;
    public string GID_1;
    public string NAME_1;
    public string NL_NAME_1;
    public string GID_2;
    public string NAME_2;
    public string VARNAME_2;
    public string NL_NAME_2;
    public string TYPE_2;
    public string ENGTYPE_2;
    public string CC_2;
    public string HASC_2;
    public string NUFUS;
}

这也是我将最终数据写入文件的方式(上面的代码是一个结果);

File.WriteAllText(@"D:\districts_with_population.json", results.ToString());


我如何才能将字符串实际写入 JSON 格式的文件?

标签: c#jsonbackslash

解决方案


正如评论中所指出的,问题在于您在此处将对象转换为 JSON 字符串:

string propertyStr = JsonConvert.SerializeObject(propertiesArray);

然后您在此处将该字符串(包括双引号等)设置为 JSON 属性:

result["properties"] = propertyStr;

我相信您不希望它作为字符串属性,而只是作为对象。因此,您希望属性的值是它JToken本身。所以我希望这能奏效:

result["properties"] = JToken.FromObject(propertiesArray);

推荐阅读