我不得不修改我的JSON文件在我的C#項目,但我得到了一些problems.Moreover,我使用json.net如何寫C#查詢到JSON文件
我的JSON文件deathGod.json:
{
"toKillList": [
{
"Name": "John",
"dieWith": "eat hot dog",
"Date": "date",
"State": "state"
},
{
"Name": "peter",
"dieWith": "swim",
"Date": "date",
"State": "state"
}
]
}
我的編碼:
string path = @"\users\deathGod\documents\visual studio 2012\Projects\killApp\killApp\deathGod.json";
string jsonData = File.ReadAllText(path);
dynamic result = JsonConvert.DeserializeObject(jsonData);
JArray items = (JArray)result["toKillList"];
問題:
我需要向JArray添加查詢,然後將Jarray添加到JObject。但是,引號和換行符會殺死我的文件。
代碼:
string element = @"{'Name':’may’,
‘Path’:’driving’,
‘Date’:’date’,
‘State’:’state’}";
JValue value = new JValue(element);
items .Add(value);
//JArray => JObject => json file
結果:
{
"toKillList": [
{
"Name": "John",
"dieWith": "eat hot dog",
"Date": "date",
"State": "state"
},
{
"Name": "peter",
"dieWith": "swim",
"Date": "date",
"State": "state"
},
"{'Name':’may’, \r\n‘Path’:’driving’,\r\n ‘Date’:’date’,\r\n ‘State’:’state’\r\n}"
]
}
什麼想法?如果你想字符串轉換成JSON首先與'
取代’
var element = JObject.FromObject(new
{
Name = "may",
Path = "driving",
Date = "date",
State = "state"
});
items.Add(element);
改變一切' '''到'',也增加了'items'或更好地利用匿名類型 –
tryed之前沒有反序列化使用@但使用轉義? like string element =「{\」Name \「:\」may \「...並且不要使用''但是使用' – Lorenzo
@AlekseyL。」\ r \ n「怎麼樣?我想要的是斷線,不是「\ r \ n」 –