Write to JArray in JObject

こ雲淡風輕ζ 提交于 2019-12-11 04:45:44

问题


I have a JSON file.

{
  "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.

json1 = ti.GetTimeItems();

I have 2 methods in another class to write to the JSON file. 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.

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. I managed to write to the file but in root instead of inside the array. 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.

Any help is appriciated


回答1:


Problem solved by ((JArray)json1.GetValue("time")). Selecting the array in the JObject json1 and adding to that instead of the root.

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);


来源:https://stackoverflow.com/questions/42856362/write-to-jarray-in-jobject

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!