C# 在一个Json中创建多个表而不使用JArray?
我正在做一个IMDB类型的项目,我列出了有电影记录、唱片记录和类似记录的人。所以我已经在MVVM中创建了我的视图,现在我正在尝试序列化json以匹配它。现在我正在整理我的桌子,就像这样:C# 在一个Json中创建多个表而不使用JArray?,c#,arrays,json,serialization,mvvm,C#,Arrays,Json,Serialization,Mvvm,我正在做一个IMDB类型的项目,我列出了有电影记录、唱片记录和类似记录的人。所以我已经在MVVM中创建了我的视图,现在我正在尝试序列化json以匹配它。现在我正在整理我的桌子,就像这样: dynamic discography = new JObject(new JProperty("DiscographyVM", jsonDiscography)); dynamic filmography = new JObject(new JProperty("FilmographyVM", jsonFil
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);
这将给我一个json,如下所示:
[
{
"Discography" : [
{
"Album" : "MyAlbum",
"Year" : 2017
},
{ "Album" : "MySecondAlbum",
"Year" : 2018
}
],
"Filmography" : [
{
"Film" : "MyFirstMovie",
"Year" : 2017
},
{
"Film" : "MySecondMovie",
"Year" : 2018
}
]
}
]
我想做的是去掉外部的[]
括号。所以我想我需要将我的动态fullJson
变量更改为其他变量,但我不知道是什么。有什么想法吗
更新,我正在尝试获取此结构:
{
"Discography" : [
{
"Album" : "MyAlbum",
"Year" : 2017
},
{ "Album" : "MySecondAlbum",
"Year" : 2018
}
],
"Filmography" : [
{
"Film" : "MyFirstMovie",
"Year" : 2017
},
{
"Film" : "MySecondMovie",
"Year" : 2018
}
]
}
使用
JObject
而不是JArray
。让我们看看:
dynamic discography = new JProperty("Discography", jsonDiscography);
dynamic filmography = new JProperty("Filmography", jsonFilmography);
dynamic fulljson = new JObject { discography, filmography };
注意,我还替换了
dynamic fullJson = new JObject();
fullJson.Add(discography);
fullJson.Add(filmography);
与
您选择JObject方式和动态对象是否有特定的原因?如果没有:如果您已经在一个普通C#类实例中拥有所有这些信息,您可以在Web API中返回这些信息,并将调用JSON.NET自动转换这些信息代码>(和从影记录)和所有电影/专辑。我希望将其转换为json,以便在我的客户机中使用,以防将来出现问题。我只是很难在我的客户机中反序列化它,因为我现在有一个外部数组。JSON约定是将数组序列化为[…],来源:是的,我知道,我用我正在尝试的方法更新了这个问题。我正试图找到一种替代外部阵列的方法,或者一种将其全部丢弃的方法。太棒了!我不认为椎间盘摄影术和电影摄影术不需要成为工作对象。这正是我想用最简单的方式做到的。Newtonsoft.Json是一个很棒的库:)哦,看起来更整洁了。再次欢呼:)
dynamic fulljson = new JObject { discography, filmography };