代码之家  ›  专栏  ›  技术社区  ›  Tom

在一个Json中创建多个表而不使用JArray?

  •  1
  • Tom  · 技术社区  · 7 年前

    我正在做一个IMDB类型的程序,在那里我列出的人谁有一个电影,唱片,和类似的。所以我已经在MVVM中创建了我的视图,现在我正在尝试序列化json以匹配它。现在我正在整理我的桌子,就像这样:

    dynamic discography = new JObject(new JProperty("DiscographyVM", jsonDiscography));
    dynamic filmography = new JObject(new JProperty("FilmographyVM", jsonFilmography));
    
    dynamic fullJson = new JArray();
    fullJson.Add(discography);
    fullJson.Add(filmography);
    

    [
      {
        "Discography" : [
          { 
            "Album" : "MyAlbum",
            "Year" : 2017
          },
          { "Album" : "MySecondAlbum",
            "Year" : 2018
          }
        ],
        "Filmography" : [
          {
            "Film" : "MyFirstMovie",
            "Year" : 2017
          },
          {
            "Film" : "MySecondMovie",
            "Year" : 2018
          }
        ]
      }
    ]
    

    我要做的是去掉外面的 [ ] 括号。所以我想我需要改变我的想法 dynamic fullJson

    更新,我正在尝试获取以下结构:

      {
        "Discography" : [
          { 
            "Album" : "MyAlbum",
            "Year" : 2017
          },
          { "Album" : "MySecondAlbum",
            "Year" : 2018
          }
        ],
        "Filmography" : [
          {
            "Film" : "MyFirstMovie",
            "Year" : 2017
          },
          {
            "Film" : "MySecondMovie",
            "Year" : 2018
          }
        ]
      }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   aloisdg    7 年前

    使用 JObject 而不是 JArray . 让我们看看:

    dynamic discography = new JProperty("Discography", jsonDiscography);
    dynamic filmography = new JProperty("Filmography", jsonFilmography);
    
    dynamic fulljson = new JObject { discography, filmography };
    

    Try it Online!

    dynamic fullJson = new JObject();
    fullJson.Add(discography);
    fullJson.Add(filmography);
    

    具有

    dynamic fulljson = new JObject { discography, filmography };