我试图将一些数据添加到一个大的json文件中,所以我试图在C#中进行操作,我已经打开了文件,将数据写入其中,但同时将最终数据写入了.Json文件中,该字符串JsonConverter.SerializeObject返回的字符串带有反斜杠,原始字符串不带反斜杠(在仔细查看Text Visualizer
时不会出现,但是写入.Json文件的最终数据仍然带有反斜杠。
这就是我看着Text Visualizer时的样子;
{
"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格式的文件中?
答案 0 :(得分:5)
正如评论中所指出的那样,问题在于您要将对象转换成JSON字符串:
string propertyStr = JsonConvert.SerializeObject(propertiesArray);
然后您在此处将该字符串(包括双引号等)设置为JSON属性:
result["properties"] = propertyStr;
我相信您不希望它作为字符串属性,而只是作为对象。因此,您希望属性的值本身是JToken
。所以我希望它能起作用:
result["properties"] = JToken.FromObject(propertiesArray);