在C#中将新的json对象添加到现有的json对象

时间:2014-09-11 16:06:04

标签: c# json serialization windows-runtime

我想将一个带有歌曲的新播放列表添加到现有的json文件中。 我正在使用Newtonsoft Json.NET创建一个这样的Json文件:

dynamic GroupObj = new JObject();
GroupObj.Playlist = new JArray() as dynamic;
dynamic PlayListObj = new JObject(); 
PlayListObj.UniqueId = "";
PlayListObj.Title = "Playlist name";
GroupObj.Playlist.Add(PlayListObj);

PlayListObj.Songs = new JArray() as dynamic;
dynamic SongObj = new JObject();
SongObj.UniqueId = "";
SongObj.Title = "song name";
PlayListObj.Songs.Add(SongObj);
data = GroupObj.ToString();

输出这个:

{
  "Playlist": [
    {
      "UniqueId": "",
      "Title": "Playlist name",
      "Songs": [
        {
          "UniqueId": "",
          "Title": "song name",
        }        
      ]
    }
  ]
}

如何修改Json.NET以将新对象添加到现有Object以获得如下内容:

{
  "Playlist": [
    {
      "UniqueId": "",
      "Title": "Playlist name",
      "Songs": [
        {
          "UniqueId": "",
          "Title": "song name",
        }
      ]
    },
    {
      "UniqueId": "",
      "Title": "Playlist name",
      "Songs": [
        {
          "UniqueId": "",
          "Title": "song name",
          "Lyrics": "song lyrics",
          "Location": "location"
        }
      ]
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

没关系,我通过重写整个文件找到了解决方案。可能不是非常资源友好,但对于学校项目,它的工作原理。感谢

string data;
JsonValue val = JsonValue.Parse("{\"some json data\": some value,}");
JsonArray Pl = val.GetObject().GetNamedArray("Playlist");
for (uint x = 0; x != Pl.Count; x++)//go through each playlist
{
    var Pl_title = Pl.GetObjectAt(x).GetNamedString("Title"); //get playlist name
    if (Pl_title != txtPlaylistName.Text)//dont do anything if playlist exists
    {
        var Pl_id = Pl.GetObjectAt(x).GetNamedString("UniqueId");
        var Pl_loc = Pl.GetObjectAt(x).GetNamedString("Location");
        var Pl_img = Pl.GetObjectAt(x).GetNamedString("ImagePath");

                    //re-create the playlist
        dynamic PlayListObj = new JObject();
        PlayListObj.UniqueId = Pl_id;
        PlayListObj.Title = Pl_title;
        PlayListObj.Location = Pl_loc;
        PlayListObj.ImagePath = Pl_img;
        GroupObj.Playlist.Add(PlayListObj);

        PlayListObj.Songs = new JArray() as dynamic;

        JsonArray Sng = Pl.GetObjectAt(x).GetNamedArray("Songs");
        for (uint y = 0; y != Sng.Count; y++)
        {
            var Sng_id = Sng.GetObjectAt(y).GetNamedString("UniqueId");
            var Sng_title = Sng.GetObjectAt(y).GetNamedString("Title");
            var Sng_lyr = Sng.GetObjectAt(y).GetNamedString("Lyrics");
            var Sng_loc = Sng.GetObjectAt(y).GetNamedString("Location");
            //re-create the songs in the playlist
            dynamic SongObj = new JObject();
            SongObj.UniqueId = Sng_id;
            SongObj.Title = Sng_title;
            SongObj.Lyrics = Sng_lyr;
            SongObj.Location = Sng_loc;
            PlayListObj.Songs.Add(SongObj);
        }
                }
                else
                    txtBerror.Text = "The name: " + txtPlaylistName.Text + " already exists.";
            }
            if (txtPlaylistName.Text != string.Empty)
            {
                //re-create the playlist
                dynamic newPlayListObj = new JObject();
                newPlayListObj.UniqueId = "PL " + txtPlaylistName.Text;
                newPlayListObj.Title = txtPlaylistName.Text;
                newPlayListObj.Location = "";
                newPlayListObj.ImagePath = "";
                GroupObj.Playlist.Add(newPlayListObj);

                newPlayListObj.Songs = new JArray() as dynamic;

                for (int a = 0; a != 3; a++)//number of songs
                {
                    dynamic SongObj = new JObject();
                    SongObj.UniqueId = "Sng " + "file name";
                    SongObj.Title = "file name";
                    SongObj.Lyrics = "";
                    SongObj.Location = "";
                    newPlayListObj.Songs.Add(SongObj);
                }
                            }
            else
                txtBerror.Text = "Enter a playlist name";

data = GroupObj.ToString();
await Windows.Storage.FileIO.WriteTextAsync(newFile, data);//write to the file