问题desearilize一个JSON我的课总是空 [英] Problems to desearilize a JSON my class is always null

查看:128
本文介绍了问题desearilize一个JSON我的课总是空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSON:

{工作区:{
工作区:[
{名: 柏林,HREF:http://10.80.14.188:8080/geoserver/rest/workspaces/Berlin.json},{名:巴黎,HREF:HTTP://10.80 .14.188:8080 /的GeoServer /休息/工作区/ 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},
{名称:USA,HREF:http://10.80.14.188:8080/geoserver/rest/workspaces/usa.json},{名:葡京,HREF:HTTP:/ /10.80.14.188:8080/geoserver/rest/workspaces/Lisboa.json},{名:马德里,HREF:http://10.80.14.188:8080/geoserver/rest/workspaces/Madrid以.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"} ]}}

下面的类:

    public class elementosJSON
    {
        [DataMember(Name = "name")]
        public string name { get; set; }

        [DataMember(Name = "href")]
        public string href { get; set; }
    }

和我正尝试以填补我的课的JSON但是元素总是空。 I'm使用:

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);



我的环境是Visual Studio 2010的C#。

My environment is Visual Studio 2010 C#.

任何想法? I'm用C#一个新手。

Any ideas? I´m a newbie with C#.

推荐答案

您需要创建一个代表你的JSON的确切结构类。是这样的:

You need to create classes that represent the exact structure of your JSON. Something like:

class JsonObj // this class represents the main JSON object { ... }
{
    public WorkspacesJson workspaces { get;set; }
}

class WorkspacesJson // this class represents the workspaces JSON object "workspaces": { ... }
{
    public List<WorkspaceJson> workspace { get;set; } // this represents the JSON array "workspace": [ ... ]
}

class WorkspaceJson // this represents the name/value pair for the workspace JSON array { "name": ..., "href": ... }
{
    public string name { get;set; }
    public string href { get;set; }
}



然后就可以反序列化:

Then you can deserialize:

var jsonInfo = JsonConvert.DeserializeObject<JsonObj>(data);

这篇关于问题desearilize一个JSON我的课总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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