C# Json.Net 可以处理 List<object> 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1208030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can Json.Net handle a List<object>?
提问by mrblah
List<User> list = LoadUsers();
JObject json = new JObject();
json["users"] = new JValue(list);
Doesn't seem to be working?
似乎没有工作?
Error:
错误:
Could not determine JSON object type for type System.Collections.Generic.List`1
回答by Brian Rogers
A JValue
can only contain simple values like strings, ints, booleans, dates and the like. It cannot contain a complex object. I suspect what you really want is this:
AJValue
只能包含简单的值,如字符串、整数、布尔值、日期等。它不能包含复杂的对象。我怀疑你真正想要的是这个:
List<User> list = LoadUsers();
JObject json = new JObject();
json["users"] = JToken.FromObject(list);
The above will convert the list of User
objects into a JArray
of JObjects
representing the users, then assign that to the users
property on the new JObject
. You can confirm this by examining the Type
property of json["users"]
and see that it is Array
.
以上将User
对象列表转换JArray
为JObjects
代表用户的 ,然后将其分配给users
新的属性JObject
。您可以通过检查 的Type
属性json["users"]
并查看它是来确认这一点 Array
。
In contrast, if you do json["users"] = new JValue(JsonConvert.SerializeObject(list))
as was suggested in another answer to this question (now deleted), you will probably not get the result you are looking for. That approach will serialize the list of users to a string, create a simple JValue
from that, and then assign the JValue
to the users
property on the JObject
. If you examine the Type
property of json["users"]
, you will see that it is String
. What this means is, if you later try to convert the JObject
to JSON by using json.ToString()
, you will get double-serialized output instead of the JSON you probably expect.
相比之下,如果您json["users"] = new JValue(JsonConvert.SerializeObject(list))
按照此问题的另一个答案(现已删除)中的建议进行操作,您可能不会得到您正在寻找的结果。这一方法将序列化的用户列表为字符串,创建一个简单的JValue
从,然后分配JValue
到users
的财产JObject
。如果您检查 的Type
属性json["users"]
,您将看到它是String
。这意味着,如果您稍后尝试使用 将 转换JObject
为 JSON json.ToString()
,您将获得双序列化输出,而不是您可能期望的 JSON。
Here is a short demo to illustrate the difference:
这是一个简短的演示来说明差异:
class Program
{
static void Main(string[] args)
{
List<User> list = new List<User>
{
new User { Id = 1, Username = "john.smith" },
new User { Id = 5, Username = "steve.martin" }
};
JObject json = new JObject();
json["users"] = JToken.FromObject(list);
Console.WriteLine("First approach (" + json["users"].Type + "):");
Console.WriteLine();
Console.WriteLine(json.ToString(Formatting.Indented));
Console.WriteLine();
Console.WriteLine(new string('-', 30));
Console.WriteLine();
json["users"] = new JValue(JsonConvert.SerializeObject(list));
Console.WriteLine("Second approach (" + json["users"].Type + "):");
Console.WriteLine();
Console.WriteLine(json.ToString(Formatting.Indented));
}
class User
{
public int Id { get; set; }
public string Username { get; set; }
}
}
Output:
输出:
First approach (Array):
{
"users": [
{
"Id": 1,
"Username": "john.smith"
},
{
"Id": 5,
"Username": "steve.martin"
}
]
}
------------------------------
Second approach (String):
{
"users": "[{\"Id\":1,\"Username\":\"john.smith\"},{\"Id\":5,\"Username\":\"steve.martin\"}]"
}
回答by polydegmon
I had this issue, you can now use JArray to get this done, if you just want the array items with no root name.
我遇到了这个问题,如果您只想要没有根名称的数组项,您现在可以使用 JArray 来完成此操作。
var json = JArray.FromObject(LoadUsers());
If you want the root name of the json array to be "users", you can use
如果您希望 json 数组的根名称为“users”,则可以使用
var json = new JObject { ["users"] = JToken.FromObject(LoadUsers()) };