使用JsonConvert.DeserializeObject将Json反序列化为C#POCO类
这是我的简单的使用JsonConvert.DeserializeObject将Json反序列化为C#POCO类,c#,json,serialization,json.net,poco,C#,Json,Serialization,Json.net,Poco,这是我的简单的UserPOCO类: // ///User类表示Coderwall用户。 /// 公共类用户 { /// ///用户的用户名。例如:“sergiotapia、Mrkibles、matumbo” /// 公共字符串用户名{get;set;} /// ///用户名。例如:“塞尔吉奥·塔皮亚、约翰·科萨克、露西·麦克米兰” /// 公共字符串名称{get;set;} /// ///用户的位置。呃:“玻利维亚、美国、法国、意大利” /// 公共字符串位置{get;set;}
User
POCO类:
//
///User类表示Coderwall用户。
///
公共类用户
{
///
///用户的用户名。例如:“sergiotapia、Mrkibles、matumbo”
///
公共字符串用户名{get;set;}
///
///用户名。例如:“塞尔吉奥·塔皮亚、约翰·科萨克、露西·麦克米兰”
///
公共字符串名称{get;set;}
///
///用户的位置。呃:“玻利维亚、美国、法国、意大利”
///
公共字符串位置{get;set;}
公共整数背书{get;set;}//Todo。
公共字符串团队{get;set;}//Todo。
///
///用户链接帐户的集合。
///
公众名单):
private User LoadUserFromJson(字符串响应)
{
var outObject=JsonConvert.DeserializeObject(响应);
返回对象;
}
这引发了一个异常:
无法反序列化当前JSON对象(例如{“名称”:“值”})
打字
'System.Collections.Generic.List'1[CoderwallDotNet.Api.Models.Account]'
因为该类型需要一个JSON数组(例如[1,2,3])来反序列化
没错
要修复此错误,请将JSON更改为JSON数组
(例如[1,2,3])或更改反序列化类型,使其成为正常类型
.NET类型(例如,不是integer之类的基元类型,也不是集合
可以从JSON反序列化的类型(如数组或列表)
还可以将object.JsonObjectAttribute添加到类型以强制它
要从JSON对象.Path“accounts.github”第1行反序列化,
第129位
我以前从未使用过这个反序列化对象方法,所以我被困在这里了
我已经确保POCO类中的属性名称与JSON响应中的名称相同
如何将JSON反序列化到此POCO类中?accounts属性的定义如下:
"accounts":{"github":"sergiotapia"}
class Account {
public string github { get; set; }
}
您的POCO声明如下:
public List<Account> Accounts { get; set; }
项目数组(将映射到列表)始终用方括号括起来
编辑:账户Poco如下:
"accounts":{"github":"sergiotapia"}
class Account {
public string github { get; set; }
}
也许还有其他财产
编辑2:要不使用数组,请按如下方式使用属性:
public Account Accounts { get; set; }
类似于我在第一次编辑中发布的示例类。accounts属性的定义如下:
"accounts":{"github":"sergiotapia"}
class Account {
public string github { get; set; }
}
to fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the
deserialized type so that it is a normal .NET type (e.g. not a primitive type like
integer, not a collection type like an array or List) that can be deserialized from a
JSON object.`
您的POCO声明如下:
public List<Account> Accounts { get; set; }
项目数组(将映射到列表)始终用方括号括起来
编辑:账户Poco如下:
"accounts":{"github":"sergiotapia"}
class Account {
public string github { get; set; }
}
也许还有其他财产
编辑2:要不使用数组,请按如下方式使用属性:
public Account Accounts { get; set; }
类似于我在第一次编辑中发布的示例类
to fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the
deserialized type so that it is a normal .NET type (e.g. not a primitive type like
integer, not a collection type like an array or List) that can be deserialized from a
JSON object.`
整个消息表明可以序列化为列表对象,但输入必须是JSON列表。
这意味着您的JSON必须包含
"accounts" : [{<AccountObjectData}, {<AccountObjectData>}...],
其中accounts是一个JSON对象(用花括号表示),而不是一个JSON对象数组(数组用括号表示),这正是您想要的。请尝试
"accounts" : [{"github":"sergiotapia"}]
整个消息表明可以序列化为列表对象,但输入必须是JSON列表。
这意味着您的JSON必须包含
"accounts" : [{<AccountObjectData}, {<AccountObjectData>}...],
其中accounts是一个JSON对象(用花括号表示),而不是一个JSON对象数组(数组用括号表示),这正是您想要的。请尝试
"accounts" : [{"github":"sergiotapia"}]
您可以创建一个JsonConverter
。请参阅以获取与您的问题类似的示例。您可以创建一个JsonConverter
。请参阅以获取与您的问题类似的示例。这里是一个工作示例
重点是:
账户声明
- 使用
JsonProperty
属性
使用(WebClient wc=new WebClient())
{
var json=wc.DownloadString(“http://coderwall.com/mdeiters.json");
var user=JsonConvert.DeserializeObject(json);
}
-
公共类用户
{
///
///用户的用户名。例如:“sergiotapia、Mrkibles、matumbo”
///
[JsonProperty(“用户名”)]
公共字符串用户名{get;set;}
///
///用户名。例如:“塞尔吉奥·塔皮亚、约翰·科萨克、露西·麦克米兰”
///
[JsonProperty(“名称”)]
公共字符串名称{get;set;}
///
///用户的位置。呃:“玻利维亚、美国、法国、意大利”
///
[JsonProperty(“位置”)]
公共字符串位置{get;set;}
[JsonProperty(“背书”)]
公共整数背书{get;set;}//Todo。
[JsonProperty(“团队”)]
公共字符串团队{get;set;}//Todo。
///
///用户链接帐户的集合。
///
[JsonProperty(“账户”)]
公共帐户帐户{get;set;}
///
///用户授予的徽章的集合。
///
[JsonProperty(“徽章”)]
公共列表标记{get;set;}
}
公共类帐户
{
公共字符串github;
}
公众阶级徽章
{
[JsonProperty(“名称”)]
公共字符串名称;
[JsonProperty(“描述”)]
公共字符串描述;
[JsonProperty(“已创建”)]
创建公共字符串;
[JsonProperty(“徽章”)]
公共字符串地址;
}
以下是一个工作示例
重点是:
账户声明
- 使用
JsonProperty
属性
使用(WebClient wc=new WebClient())
{
var json=wc.DownloadString(“http://coderwall.com/mdeiters.json");
var user=JsonConvert.DeserializeObject(json);
}
-
公共类用户
{
///
///用户的用户名。例如:“sergiotapia、Mrkibles、matumbo”
///
[JsonProperty(“用户名”)]
公共字符串用户名{get;set;}
///
///用户名。例如:“塞尔吉奥·塔皮亚、约翰·科萨克、露西·麦克米兰”
///
[JsonProperty(“名称”)]
公共字符串名称{get;set;}
///
///用户的位置。呃:“玻利维亚