多行JSON到数据表

时间:2019-06-19 00:31:51

标签: c# json json.net

我正在使用Newtonsoft的以下内容将一些JSON数据反序列化为数据表(以最终保存到电子表格的最终目的为前提);

var dt = (DataTable)JsonConvert.DeserializeObject(returnData, (typeof(DataTable)));

虽然这很好用,但存在嵌套行丢失的问题。以下是类似格式的示例数据。在评级部分中,仅保存了“ Internet电影数据库”,转换中丢失了“烂番茄”和“元批评”。有没有反序列化方法可以保留这些内容?我愿意考虑将结果分成多行或将“收视率”部分串联到一个字段中的选项。

{
    "Title": "Guardians of the Galaxy Vol. 2",
    "Year": "2017",
    "Rated": "PG-13",
    "Released": "05 May 2017",
    "Runtime": "136 min",
    "Genre": "Action, Adventure, Comedy, Sci-Fi",
    "Director": "James Gunn",
    "Writer": "James Gunn, Dan Abnett (based on the Marvel comics by), Andy Lanning (based on the Marvel comics by), Steve Englehart (Star-Lord created by), Steve Gan (Star-Lord created by), Jim Starlin (Gamora and Drax created by), Stan Lee (Groot created by), Larry Lieber (Groot created by), Jack Kirby (Groot created by), Bill Mantlo (Rocket Raccoon created by), Keith Giffen (Rocket Raccoon created by), Steve Gerber (Howard the Duck created by), Val Mayerik (Howard the Duck created by)",
    "Actors": "Chris Pratt, Zoe Saldana, Dave Bautista, Vin Diesel",
    "Plot": "The Guardians struggle to keep together as a team while dealing with their personal family issues, notably Star-Lord's encounter with his father the ambitious celestial being Ego.",
    "Language": "English",
    "Country": "USA",
    "Awards": "Nominated for 1 Oscar. Another 12 wins & 42 nominations.",
    "Poster": "https://m.media-amazon.com/images/M/MV5BMTg2MzI1MTg3OF5BMl5BanBnXkFtZTgwNTU3NDA2MTI@._V1_SX300.jpg",
    "Ratings": [{
            "Source": "Internet Movie Database",
            "Value": "7.7/10"
        }, {
            "Source": "Rotten Tomatoes",
            "Value": "84%"
        }, {
            "Source": "Metacritic",
            "Value": "67/100"
        }
    ],
    "Metascore": "67",
    "imdbRating": "7.7",
    "imdbVotes": "482,251",
    "imdbID": "tt3896198",
    "Type": "movie",
    "DVD": "22 Aug 2017",
    "BoxOffice": "$389,804,217",
    "Production": "Walt Disney Pictures",
    "Website": "https://marvel.com/guardians",
    "Response": "True"
}

更新

感谢您的解决方案,我到家后将尝试这些解决方案。同时,也许更清楚(或更复杂),我愿意将Ratings部分连接到单个分隔的字符串/字段。理想的情况如下所示。

enter image description here

2 个答案:

答案 0 :(得分:1)

您要反序列化的DataTable类型无法处理电影与其评级之间的一对多关系。

尝试反序列化为更适合您的JSON对象的更特定的类型。

您可以使用json2csharp.com从JSON对象中制作C#类。

一旦有了C#类型,就可以反序列化为该类型,并获得等效于对象的C#。

var obj = (RootObject)JsonConvert.DeserializeObject(returnData, (typeof(RootObject)));

或者如果您的JSON数据是这些对象的数组:

var list = (RootObject[])JsonConvert.DeserializeObject(returnData, (typeof(RootObject[])));

答案 1 :(得分:0)

如果您不想声明一个类,这对您有用。

            var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);

            string rating = Convert.ToString(dict["Ratings"]);

            var dtScore = JsonConvert.DeserializeObject<DataTable>(rating);

            string MetacriticScore = dtScore.Rows[2]["Value"].ToString();

还有另一种简单的方法

            var jsonObj = JsonConvert.DeserializeObject<JObject>(json);

            string MetacriticScore = Convert.ToString(jsonObj["Ratings"][2]["Value"]);