【发布时间】:2019-02-27 01:54:10
【问题描述】:
如果我尝试使用从 List 或 Dictionary 派生的根类来反序列化一系列类,我会收到以下异常:
"值 \"System.Collections.Generic.Dictionary`2[System.String,System.Object]\" 不是 \"JSONTesting.UserInformation\" 类型,不能用于 这个通用集合。\r\n参数名称:值"
这是我用来创建 JSON 文件并对其进行反序列化的代码。这只是一个演示问题的小示例项目:
private void BuildUserInfo(object sender, EventArgs e)
{
Users users = new Users();
UserInformation userInformation;
//Item 1
userInformation = new UserInformation();
userInformation.Name = "John Doe";
userInformation.Age = "50";
userInformation.Addresses.Add(new Address("11234 Smith Pl.", "Orlando", "FL", "32789"));
userInformation.Addresses.Add(new Address("553 Park St.", "Boston", "MA", "02115"));
userInformation.PhoneNumbers.Add(new PhoneNumber("617", "111-2222", string.Empty));
userInformation.PhoneNumbers.Add(new PhoneNumber("508", "236-0173", "22734"));
users.Add(userInformation);
//Serialize
JavaScriptSerializer serializer = new JavaScriptSerializer();
StreamWriter streamOut = null;
string outString = string.Empty;
outString = serializer.Serialize(users);
streamOut = new StreamWriter("OUTFILE.JSON");
streamOut.Write(outString);
streamOut.Close();
}
private void ReadUserInfo(object sender, EventArgs e)
{
JavaScriptSerializer serializer;
StreamReader streamIn = null;
Users retrievedUsers = null;
if (File.Exists("OUTFILE.JSON"))
{
streamIn = new StreamReader("OUTFILE.JSON");
serializer = new JavaScriptSerializer();
retrievedUsers = serializer.Deserialize<Users>(streamIn.ReadToEnd());
streamIn.Close();
}
}
}
public class Users: List<UserInformation>
{
public UserInformation getUserByName(string name)
{
foreach (UserInformation user in this)
{
if (name == user.Name)
return user;
}
return null;
}
}
public class UserInformation
{
public string Name;
public string Age;
public List<Address> Addresses;
public List<PhoneNumber> PhoneNumbers;
public UserInformation()
{
Name = string.Empty;
Age = string.Empty;
Addresses = new List<Address>();
PhoneNumbers = new List<PhoneNumber>();
}
public UserInformation(string Name, string Age)
{
this.Name = Name;
this.Age = Age;
Addresses = new List<Address>();
PhoneNumbers = new List<PhoneNumber>();
}
}
public class Address
{
public string Street;
public string City;
public string State;
public string PostalCode;
public Address()
{
Street = string.Empty;
City = string.Empty;
State = string.Empty;
PostalCode = string.Empty;
}
public Address(string Street, string City, string State, string PostalCode)
{
this.Street = Street;
this.City = City;
this.State = State;
this.PostalCode = PostalCode;
}
}
public class PhoneNumber
{
public string AreaCode;
public string Number;
public string Extension;
public PhoneNumber()
{
AreaCode = string.Empty;
Number = string.Empty;
Extension = string.Empty;
}
public PhoneNumber(string AreaCode, string Number, string Extension)
{
this.AreaCode = AreaCode;
this.Number = Number;
this.Extension = Extension;
}
}
这是由 BuildUserInfo 函数生成的 JSON:
[
{
"Name": "John Doe",
"Age": "50",
"Addresses": [
{
"Street": "11234 Smith Pl.",
"City": "Orlando",
"State": "FL",
"PostalCode": "32789"
},
{
"Street": "553 Park St.",
"City": "Boston",
"State": "MA",
"PostalCode": "02115"
}
],
"PhoneNumbers": [
{
"AreaCode": "617",
"Number": "111-2222",
"Extension": ""
},
{
"AreaCode": "508",
"Number": "236-0173",
"Extension": "22734"
}
]
}
]
如果我更改 Users 类,使其不是从 List 派生的,而只是将其设为具有内部 List 集合的类,它就可以正常工作。一个例子:
public class Users
{
public List<UserInformation> UserItems = new List<UserInformation>();
}
我需要更改哪些内容才能反序列化此 JSON?
编辑:尝试下面评论中的链接,我更改为以下内容:
public class Users(IEnumerable<UserInformation> collection) : base (collection) {
}
我收到构建错误:
错误 1 类、结构或接口成员中的无效标记 '(' 声明 c:\Local .NET 项目\2010\JSONTesting1\Form1.cs 66 25 JSONTesting1 错误 2 { 预期 c:\Local .NET 项目\2010\JSONTesting1\Form1.cs 66 25 JSONTesting1 错误 3; 预期 c:\Local .NET Projects\2010\JSONTesting1\Form1.cs 66 69 JSONTesting1 错误 4 无效 类、结构或接口成员声明 c:\Local 中的标记 ')' .NET 项目\2010\JSONTesting1\Form1.cs 66 93 JSONTesting1 错误 5 } 预期 c:\Local .NET 项目\2010\JSONTesting1\Form1.cs 153 2 JSONTesting1
【问题讨论】:
-
它没有用。我在帖子中添加了更多信息。
-
您试图将 JSON 数组放入单个对象中。尝试将检索到的用户更改为列表,然后反序列化为列表
List<Users> retrievedUsers = new List<Users>();retrievedUsers = serializer.Deserialize<IList<Users>>streamIn.ReadToEnd());
标签: c# json deserialization