使用JSON.NET和C#阅读文件和反序列化JSON失败 [英] Using JSON.NET and C# reading file and deserialize the JSON fails
问题描述
我有一个名为movies.json一个JSON文件,其中包含的对象和3电影的数组。在movies.json的JSON看起来是这样的:
I have a JSON file named 'movies.json' which contains an object and an array of 3 movies. The JSON in movies.json looks like this:
{
"theMovies": [
{
"name": "Starship Troopers",
"year": 1997
},
{
"name": "Ace Ventura: When Nature Calls",
"year": 1995
},
{
"name": "Big",
"year": 1988
}
]
}
我也有一个名为Movie类
I also have a class named Movie
public class Movie
{
public string Name { get; set; }
public int Year { get; set; }
}
我想将文件读入一个字符串和反序列化的JSON来\\:;电影>(File.ReadAllText(@C
I am trying to read the file into a string and deserialize the JSON to a type of Movie like this:
Movie movie1 = JsonConvert.DeserializeObject<Movie>(File.ReadAllText(@"c:\Temp\movies.json"));
和这样的:
using (StreamReader file = File.OpenText(@"c:\Temp\movies.json"))
{
JsonSerializer serializer = new JsonSerializer();
Movie movie2 = (Movie)serializer.Deserialize(file, typeof(Movie));
Console.WriteLine(movie2.Name);
Console.WriteLine(movie2.Year);
}
无论是一期工程。无论MOVIE1和MOVIE2显示没有真正的数据观察窗口:
Neither one works. Both movie1 and movie2 show no real data the Watch window:
movie1.Name = null
movie1.Year = 0
movie2.Name = null
movie2.Year = 0
显然有一个里面的数组是行不通的对象。有什么建议么?
Apparently having an object with an array inside is not working. Any suggestions?
推荐答案
在JSON,什么都放在 []
表示一个集合,任何包含在 {}
表示对象(请参阅我的答案这里 )。所以,你可以看到你有一个包含名为 theMovies
集合,每2个属性包含3个对象(在这种情况下,电影)的对象。如果你觉得这些在C#来说,你可以有一个类(将作为根对象,姑且称之为 MyJsonDocument
)包含名单,LT ;电影>
集合(这将作为 theMovies
集合)来保存每个电影
实例在
In JSON, anything enclosed in [ ]
denotes a collection and anything enclosed in { }
denotes an object (see my answer here). So, you can see that you have an object containing a collection called theMovies
, consisting of 3 objects (in this case, movies) with 2 properties each. If you think of these in C# terms, you could have a class (which would act as the root object, let's call it MyJsonDocument
) containing a List<Movie>
collection (which would act as theMovies
collection) to hold each Movie
instance in.
public class MyJsonDocument
{
// JsonProperty indicates how each variable appears in the JSON, _
// so the deserializer knows that "theMovies" should be mapped to "Movies". _
// Alternatively, your C# vars should have the same name as the JSON vars.
[JsonProperty("theMovies")]
public List<Movie> Movies { get; set; }
}
public class Movie
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("year")]
public int Year { get; set; }
}
现在,你有你的结构设置:
Now that you have your structure set up:
var myJsonDocument = JsonConvert.DeserializeObject<MyJsonDocument>(File.ReadAllText(@"C:\Users\trashr0x\Desktop\movies.json"));
foreach(Movie movie in myJsonDocument.Movies)
{
Console.WriteLine(string.Format("{0} ({1})", movie.Name, movie.Year));
}
输出:
Output:
星河战队(1997)
艾丝:当自然召唤(1995)
Ace Ventura: When Nature Calls (1995)
大(1988)
顺便说一句,你还可能要考虑当一些JSON属性是空的会发生什么(例如,没有上市一年为一个特定的电影)。你将不得不转换年度
是一个 INT?
(空整型),而不是一个 INT
。如果你有信心100%,在JSON属性将总是被填充,那么你可以忽略这一点。
As an aside, you might also want to consider what happens when some of the JSON properties are empty (for example, no year listed for a particular movie). You would have to convert Year
to be an int?
(nullable integer) instead of an int
. If you are 100% confident that all properties in the JSON will be always populated then you can ignore this.
这篇关于使用JSON.NET和C#阅读文件和反序列化JSON失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!