Newtonsoft.Json.JsonSerializationException

时间:2013-12-31 12:21:21

标签: c# json serialization

我正试图通过语句

将json转换为C#对象
 var organizations = response.Content.ReadAsAsync<IEnumerable<Organization>>().Result;

我发现了很多问题,就像我现在面对的一样,但我没有找到正确的答案,我可能知道这个例外的原因

  

无法将当前JSON对象(例如{“name”:“value”})反序列化为类型'System.Collections.Generic.IEnumerable`1 [ConsoleApplication2.Organization]',因为该类型需要JSON数组(例如[1] ,2,3]正确地反序列化。

     

要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是可以从JSON对象反序列化的集合类型,如数组或List)。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。

我正试图通过语句将json转换为C#对象 我发现了很多问题,就像我现在面对的一样,但我没有找到正确的答案,我可能知道这个例外的原因 我的Json是

{
    "code": 0,
    "message": "success",
    "organizations": [
        {
        "organization_id": "10234695",
        "name": "Zillium Inc",
        "contact_name": "John Smith",
        "email": "johnsmith@zilliuminc.com",
        "is_default_org": false,
        "plan_type": 0,
        "tax_group_enabled": true,
        "plan_name": "TRIAL",
        "plan_period": "",
        "language_code": "en",
        "fiscal_year_start_month": 0,
        "account_created_date": "2012-02-18",
        "account_created_date_formatted": "18 Feb 2012",
        "time_zone": "Asia/Calcutta",
        "is_org_active": true,
        "currency_id": "460000000000097",
        "currency_code": "USD",
        "currency_symbol": "$",
        "currency_format": "###,##0.00",
        "price_precision": 2
        },
        {
        "organization_id": "10407630",
        "name": "Winston Longbridge",
        "contact_name": "John Smith",
        "email": "johnsmith@zilliuminc.com",
        "is_default_org": false,
        "plan_type": 0,
        "tax_group_enabled": true,
        "plan_name": "TRIAL",
        "plan_period": "",
        "language_code": "en",
        "fiscal_year_start_month": 0,
        "account_created_date": "2012-07-11",
        "account_created_date_formatted": "11 Jul 2012",
        "time_zone": "Asia/Calcutta",
        "is_org_active": true,
        "currency_id": "541000000000099",
        "currency_code": "INR",
        "currency_symbol": "Rs.",
        "currency_format": "###,##0.00",
        "price_precision": 2
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

您正在尝试反序列化为IEnumerable<Organization>>这是一个列表,因此序列化程序需要一个定义列表/数组的json字符串,如[{a:b}, {a:n}]

您的json数据似乎是一个普通的json对象而不是数组。

这导致了异常。