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",
  "JToken": "",
  "HASC_2": "TR.AA.AL",
  "NUFUS": "16653"
}


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

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


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

 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 格式的文件?

正如注释中指出的,问题是您将对象转换为JSON字符串:

string propertyStr = JsonConvert.SerializeObject(propertiesArray);

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

result["properties"] = propertyStr;

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

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

相关内容

  • 没有找到相关文章

最新更新