Reputation: 99
I am trying to add some data into a big json file so I'm trying to do it in C#, I have opened the file, wrote my data into it but while writing the final data into .Json file, the string that JsonConverter.SerializeObject returns has backslashes, the original string does not have it (it doesn't appear while looking thorough the Text Visualizer
but the final data that is written into the .Json file still has the backslashes.
This is what it is when I'm looking at 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"
}
But the real data in the file is this;
"{\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}"
This is how I try to do this in code;
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;
}
Also this is how I write the final data into the file (The code above is one result);
File.WriteAllText(@"D:\districts_with_population.json", results.ToString());
How can I actually write the string into the file with JSON format?
Upvotes: 1
Views: 1871
Reputation: 1500385
As has been noted in comments, the problem is that you're converting an object into a JSON string here:
string propertyStr = JsonConvert.SerializeObject(propertiesArray);
And then you're setting that string (which includes double quotes etc) as a JSON property here:
result["properties"] = propertyStr;
I believe you don't want it as a string property, but just as the object. So instead, you want the value of the property to be the JToken
itself. So I'd expect this to work:
result["properties"] = JToken.FromObject(propertiesArray);
Upvotes: 4