[英]Desalinizing JSON Object Contains List of Objects Returns Null for the nested object
This is my first attempt of working with JSON deserialization. 这是我第一次尝试使用JSON反序列化。 I have read many of the posts in Stackoverflow, non of the suggested solutions could match my issue, so my apology in advance.
我已经阅读了Stackoverflow上的许多帖子,没有建议的解决方案可以解决我的问题,因此我提前道歉。 I have created the following objects:
我创建了以下对象:
public class Item
{
public int ID { get; set; }
public int LSum { get; set; }
public int YSum { get; set; }
public int TSum { get; set; }
public int NSum { get; set; }
public int MemberId { get; set; }
}
public class Something
{
public int Id { get; set; }
public string Phone { get; set; }
public bool ExistingMember { get; set; }
public IList<Item> Item { get; set; }
}
And when deserialize the JSON it looks like following: The follwoing JSON what I expect it to be: 反序列化JSON时,其外观如下所示:以下是我期望的JSON:
{
"Id":62,
"Phone":"07",
"ExistingMember":true,
"Item":[
{
"ID":42,
"LSum":0,
"YSum":0,
"TSum":0,
"NSum":0,
"MemberId":12
}
]
}
However the following method it 但是下面的方法呢
some= JsonConvert.DeserializeObject<something>(someResponse);
It prints the json like the following: The following JSON is what is "someResponse" return, 它将按照以下方式打印json:以下JSON是“ someResponse”返回,
{
"Id":62,
"Phone":"07",
"ExistingMember":true,
"Item":null
}
What am I missing that the item list is return null? 我缺少项目列表返回null的问题吗?
If you want to deserialize json string which in your case is someResponse
variable, then you are doing it right. 如果您想反序列化json字符串(在您的情况下是
someResponse
变量),那么您做对了。
To check your code I created a JSON file with a file.json name and put the following on it: 为了检查您的代码,我创建了一个带有file.json名称的JSON文件,并将以下内容放入其中:
{
"Id": 62,
"Phone": "07",
"ExistingMember": true,
"Item": [
{
"ID": 42,
"LSum": 0,
"YSum": 0,
"TSum": 0,
"NSum": 0,
"MemberId": 12
}
]
}
Then below lines of code take the content of JSON file (which in your case is the content of someResponse
) and deserialize it into c# object of Something
type: 然后在下面的代码行中获取JSON文件的内容(在您的情况下为
someResponse
的内容),并将其反序列化为Something
类型的c#对象:
string jsonFilePath = @"C:\test\file.json";
var some = JsonConvert.DeserializeObject<Something>(File.ReadAllText(jsonFilePath));
Then print the ID
property of each element of Item
list: 然后打印
Item
列表的每个元素的ID
属性:
foreach(var item in some.Item)
{
if (item != null)
{
Console.WriteLine($"item ID = {item.ID}");
}
}
The output: 输出:
item ID = 42
So, it is quite possible that someResponse
just does not have an Item
and looks like this: 因此,
someResponse
很可能没有Item
并且看起来像这样:
{
"Id": 62,
"Phone": "07",
"ExistingMember": true
}
UPDATE: 更新:
Also I tried like this: 我也这样尝试过:
var someResponse = @"{
'Id': 62,
'Phone': '07',
'ExistingMember': true,
'Item':[
{
'ID': 42,
'LSum': 0,
'YSum': 0,
'TSum': 0,
'NSum': 0,
'MemberId': 12
}
]
}
";
var some = JsonConvert.DeserializeObject<Something>(someResponse);
And some
has a Item
list with 1 element some
有一个包含1个元素的Item
列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.