可以Json.Net处理列表<对象&gt ;? [英] Can Json.Net handle a List<object>?

查看:99
本文介绍了可以Json.Net处理列表<对象&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 列表<使用者>清单= LoadUsers(); 

JObject JSON =新JObject();

json的[用户] =新JValue(名单);



似乎没有奏效?



错误:

 无法确定类型System.Collections.Generic.List`1 
JSON对象类型


解决方案

A JValue CAN只包含如字符串,整数,布尔值,日期等的简单的值。它不能包含一个复杂的对象。我怀疑你真正想要的是这样的:

 列表<使用者>清单= LoadUsers(); 

JObject JSON =新JObject();

json的[用户] = JToken.FromObject(名单);



以上,将转换用户列表对象成 JArray JObjects 代表用户,然后分配到用户上新的属性 JObject 。您可以通过检查键入 的JSON财产[用户] 确认这一点,并认为它是阵列



在此相反,如果你这样做 json的[用户] =新JValue(JsonConvert .SerializeObject(列表))作为另一个回答这个问题(现已删除)建议,你可能不会得到你正在寻找的结果。这一方法将用户列表序列化到一个字符串,从创建一个简单的 JValue ,然后分配 JValue 用户的属性 JObject 。如果检查键入 的JSON财产[用户] ,你会看到它是字符串。这意味着,如果以后尝试将 JObject 使用到JSON json.ToString(),你转换。将得到双倍序列化输出,而不是你可能期望的JSON



下面是一个简短的演示来说明的区别:

 类节目
{
静态无效的主要(字串[] args)
{
名单<使用者>名单=新名单<使用者>
{
新用户{ID = 1,用户名=和John.Smith},
新用户{n = 5,用户名=steve.martin}
};

JObject JSON =新JObject();

json的[用户] = JToken.FromObject(名单);
Console.WriteLine(第一种方法(+ JSON [用户]类型+));
Console.WriteLine();
Console.WriteLine(json.ToString(Formatting.Indented));

Console.WriteLine();
Console.WriteLine(新的字符串( - ,30));
Console.WriteLine();

json的[用户] =新JValue(JsonConvert.SerializeObject(列表));
Console.WriteLine(第二条本办法(+ JSON [用户]类型+));
Console.WriteLine();
Console.WriteLine(json.ToString(Formatting.Indented));
}

类用户
{
公众诠释标识{搞定;组; }
公共字符串用户名{获得;组; }
}
}



输出:

 第一种方法(阵列):

{
用户:[
{
ID:1,
用户名:或John.Smith
},
{
标识:5,
用户名:史蒂夫。马丁
}
]
}

------------------------- -----

第二条本办法(字符串):

{
用户:[{\Id\:1,\\ \\Username\:\john.smith\},{\Id\:5,\Username\:\steve.martin\}]
}


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

解决方案

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:

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.

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.

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\"}]"
}

这篇关于可以Json.Net处理列表&LT;对象&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆