如何在C#中将查询写入JSON文件

时间:2022-02-13 14:46:55

I have to edit my JSON file in my C# project, but i got some problems.Moreover, i am using json.net

我必须在我的C#项目中编辑我的JSON文件,但是我遇到了一些问题。另外,我正在使用json.net

my JSON file deathGod.json :

我的JSON文件deathGod.json:

{
  "toKillList": [
    {
      "Name": "John",
      "dieWith": "eat hot dog",
      "Date": "date",
      "State": "state"
    },
    {
      "Name": "peter",
      "dieWith": "swim",
      "Date": "date",
      "State": "state"
    }
  ]
}

my coding :

我的编码:

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"];

Problem:

i need to add a query to the JArray, then add the Jarray to JObject. However, the quotation mark and line break are kill my file.

我需要向JArray添加一个查询,然后将Jarray添加到JObject。但是,引号和换行符会杀死我的文件。

code:

string element = @"{'Name':’may’,
                     ‘Path’:’driving’,
                     ‘Date’:’date’, 
                     ‘State’:’state’}";

JValue value = new JValue(element);
items .Add(value);
//JArray => JObject => json file

result:

{
  "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}"
  ]
}

any idea?

2 个解决方案

#1


1  

You should build JObject and not JValue:

你应该构建JObject而不是JValue:

var element = JObject.FromObject(new
{
    Name = "may",
    Path = "driving",
    Date = "date",
    State = "state"
});
items.Add(element);

#2


0  

If you want to convert string into JSon firstly replace with '. Then use the following code:

如果你想将字符串转换为JSon,首先要替换'with'。然后使用以下代码:

var element = @"{'Name':'may','Path':'driving', 'Date':'date', 'State':'state'}";

var value = (JToken)JsonConvert.DeserializeObject(element);
items.Add(value);

var res = JsonConvert.SerializeObject(result);

The final result is:

最终结果是:

{
  "toKillList":[
     {"Name":"John","dieWith":"eat hot dog","Date":"date","State":"state"},
     {"Name":"peter","dieWith":"swim","Date":"date","State":"state"},
     {"Name":"may","Path":"driving","Date":"date","State":"state"}
  ]
}

#1


1  

You should build JObject and not JValue:

你应该构建JObject而不是JValue:

var element = JObject.FromObject(new
{
    Name = "may",
    Path = "driving",
    Date = "date",
    State = "state"
});
items.Add(element);

#2


0  

If you want to convert string into JSon firstly replace with '. Then use the following code:

如果你想将字符串转换为JSon,首先要替换'with'。然后使用以下代码:

var element = @"{'Name':'may','Path':'driving', 'Date':'date', 'State':'state'}";

var value = (JToken)JsonConvert.DeserializeObject(element);
items.Add(value);

var res = JsonConvert.SerializeObject(result);

The final result is:

最终结果是:

{
  "toKillList":[
     {"Name":"John","dieWith":"eat hot dog","Date":"date","State":"state"},
     {"Name":"peter","dieWith":"swim","Date":"date","State":"state"},
     {"Name":"may","Path":"driving","Date":"date","State":"state"}
  ]
}