在JObject中写入JArray [英] Write to JArray in JObject

查看:537
本文介绍了在JObject中写入JArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON文件.

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
}

我还有一个JObject和一个获取所有JSON数据并将其存储在此对象中的方法.

I also have a JObject and a method to get all the JSON data and store it in this object.

json1 = ti.GetTimeItems();

我在另一个类中有2个方法可写入JSON文件.数据文件夹是路径.

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

然后我有一个Windows窗体,我想从2个文本框中提取文本并将其添加到JSON文件中.

Then i have a windows form where i want to take the text from 2 textboxes and add these to the JSON file.

最后我有我的点击事件

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

我的问题是我似乎无法在JObject中写入数组时间". 我设法写入文件,但是在根目录而不是在数组内部. 我已经尝试过json1.SelectToken("time")和许多其他方法,例如 http://stackoverflow. com/questions/15413825/how-do-you-add-a-jtoken-to-an-jobject#15782238 以及Newtonsoft文档中的一些方法.

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.

任何帮助都可以得到

推荐答案

((JArray)json1.GetValue("time"))解决的问题.在JObject json1中选择数组,然后添加到其中而不是根中.

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

这篇关于在JObject中写入JArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆