简体   繁体   English

在JObject中写入JArray

[英]Write to JArray in JObject

I have a JSON file. 我有一个JSON文件。

{
  "time": [
    {
      "id": "9999",
      "name": "Foo",
      "subitem": [
        {
          "name": "Bar",
          "id": "99990",
          "visible": true,
          "subitem": [
            {
              "id": "999901",
              "name": "Flex",
              "visible": true
            },
            {
              "name": "Bear",
              "id": "999902",
              "visible": true
            },
            {
              "name": "James",
              "id": "999903",
              "visible": true
            }
          ]
        },
        {
          "name": "Smith",
          "id": "999966",
          "visible": true
        },
        {
          "name": "John",
          "id": "999933",
          "visible": true
        }
      ],
      "visible": true
    },
    {
      "name": "Doe",
      "id": "1111",
      "visible": true,
      "subitem": [
        {
          "name": "Jack",
          "id": "111111",
          "visible": true
        },
        {
          "name": "Wilson",
          "id": "111188",
          "visible": true
        },
        {
          "name": "Andy",
          "id": "111144",
          "visible": true
        },
        {
          "name": "Gibbs",
          "id": "111155",
          "visible": true
        }
      ]
    }
  ],
  "name": "asdf",
  "id": "13",
  "visible": true
}

I also have a JObject and a method to get all the JSON data and store it in this object. 我还有一个JObject和一个方法来获取所有JSON数据并将其存储在此对象中。

json1 = ti.GetTimeItems();

I have 2 methods in another class to write to the JSON file. 我在另一个类中有2个方法来写入JSON文件。 Where datafolder is the path. 数据文件夹是路径的位置。

public void WriteToJson(JObject obj)
{
    string fileName = dataFolder + "json1.json";
    WriteToJson(fileName, obj);
}

private void WriteToJson(string fileName, JObject obj)
{
    using (StreamWriter file = File.CreateText(fileName))
    using (JsonTextWriter writer = new JsonTextWriter(file))
    {
        obj.WriteTo(writer);
    }         
}//end WriteToJson

Then i have a windows form where i want to take the text from 2 textboxes and add these to the JSON file. 然后我有一个Windows窗体,我想从2个文本框中取出文本并将它们添加到JSON文件中。

Finally i have my click event 最后我有我的点击事件

private void button1_Click_1(object sender, EventArgs e)
{
    //string json = File.ReadAllText(url);
    //JArray time = (JArray)json1.SelectToken("time");

    json1.Add(new JObject(
    new JProperty("name", textBoxName.Text),
    new JProperty("id", textBoxId.Text),
    new JProperty("visible", true)));
    ti.WriteToJson(json1);
}

My problem is that i cannot seem to write to the array "time" in the JObject. 我的问题是我似乎无法写入JObject中的数组“time”。 I managed to write to the file but in root instead of inside the array. 我设法写入文件但在root中而不是在数组内。 I have tried json1.SelectToken("time") and lots of different approaches, like this one http://stackoverflow.com/questions/15413825/how-do-you-add-a-jtoken-to-an-jobject#15782238 and also some approaches from the Newtonsoft documentation. 我尝试了json1.SelectToken(“时间”)和许多不同的方法,比如这个http://stackoverflow.com/questions/15413825/how-do-you-add-a-jtoken-to-an-jobject# 15782238以及Newtonsoft文档中的一些方法。

Any help is appriciated 任何帮助都是适当的

Problem solved by ((JArray)json1.GetValue("time")) . 问题解决了((JArray)json1.GetValue("time")) Selecting the array in the JObject json1 and adding to that instead of the root. 在JObject json1中选择数组并添加到该数组而不是根目录。

Hope this will help someone. 希望这会对某人有所帮助。

  ((JArray)json1.GetValue("time")).Add(
                new JObject(
                     new JProperty("name", textBoxName.Text),
                     new JProperty("id", textBoxId.Text),
                     new JProperty("visible", true)));



            ti.WriteToJson(json1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM