反序列化 JSON 时出现问题我的类始终为 null
我有以下 JSON:
{“workspaces”:{ “工作区”:[ {“名称”:“柏林”,“href”:“http://10.80.14.188:8080/geoserver/rest/workspaces/Berlin.json”},{“名称”:“巴黎”,“href”:“ http://10.80.14.188:8080/geoserver/rest/workspaces/Paris.json"}, {“名称”:“罗马”,“href”:“http://10.80.14.188:8080/geoserver/rest/workspaces/Rome.json”},{“名称”:“伦敦”,“href”:“ http://10.80.14.188:8080/geoserver/rest/workspaces/London.json"}, {"name":"usa","href":"http://10.80.14.188:8080/geoserver/rest/workspaces/usa.json"}, {"name":"里斯本","href":" http://10.80.14.188:8080/geoserver/rest/workspaces/Lisboa.json"}, {"name":"马德里","href":"http://10.80.14.188:8080/geoserver/rest/workspaces/Madrid.json"} ]}}
以下类:
public class elementosJSON
{
[DataMember(Name = "name")]
public string name { get; set; }
[DataMember(Name = "href")]
public string href { get; set; }
}
我试图用 json 填充我的类,但元素始终为 null。我正在使用:
ObjJSON test = JsonConvert.DeserializeObject<ObjJSON>(data);
我的环境是 Visual Studio 2010 C#。
有什么想法吗?我是 C# 新手。
I've got the following JSON:
{"workspaces":{
"workspace":[
{"name":"Berlin","href":"http://10.80.14.188:8080/geoserver/rest/workspaces/Berlin.json"}, {"name":"Paris","href":"http://10.80.14.188:8080/geoserver/rest/workspaces/Paris.json"},
{"name":"Rome","href":"http://10.80.14.188:8080/geoserver/rest/workspaces/Rome.json"}, {"name":"London","href":"http://10.80.14.188:8080/geoserver/rest/workspaces/London.json"},
{"name":"usa","href":"http://10.80.14.188:8080/geoserver/rest/workspaces/usa.json"}, {"name":"Lisboa","href":"http://10.80.14.188:8080/geoserver/rest/workspaces/Lisboa.json"}, {"name":"Madrid","href":"http://10.80.14.188:8080/geoserver/rest/workspaces/Madrid.json"}
]}}
The following class:
public class elementosJSON
{
[DataMember(Name = "name")]
public string name { get; set; }
[DataMember(Name = "href")]
public string href { get; set; }
}
And I´m trying to fill my class with the json but the elements are always null. I´m using:
ObjJSON test = JsonConvert.DeserializeObject<ObjJSON>(data);
My environment is Visual Studio 2010 C#.
Any ideas? I´m a newbie with C#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要创建代表 JSON 确切结构的类。像这样:
然后你可以反序列化:
You need to create classes that represent the exact structure of your JSON. Something like:
Then you can deserialize:
虽然这不是您的反序列化问题的直接答案;我更喜欢下面的方法利用
dynamic
而不是声明很多类这是 JsonObject 类的完整实现,我之前发布了 此处
Although this is not direct answer to your deserialization question; I prefer below method utilizing
dynamic
instead of declaring a lot of classesHere is the full implementation of JsonObject class, I previously posted here