反序列化 JSON 时出现问题我的类始终为 null

发布于 2024-12-09 05:26:04 字数 1102 浏览 0 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小瓶盖 2024-12-16 05:26:04

您需要创建代表 JSON 确切结构的类。像这样:

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

然后你可以反序列化:

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

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);
想念有你 2024-12-16 05:26:04

虽然这不是您的反序列化问题的直接答案;我更喜欢下面的方法利用 dynamic 而不是声明很多类

JObject o = (JObject)JsonConvert.DeserializeObject(jsonstr);
dynamic json = new JsonUtils.JsonObject(o);

foreach (var x in json.workspaces.workspace)
{
    Console.WriteLine(x.name + " " + x.href);
}

这是 JsonObject 类的完整实现,我之前发布了 此处

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Dynamic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;

namespace JsonUtils
{
    class JsonObject : DynamicObject, IEnumerable, IEnumerator
    {
        object _object;

        public JsonObject(object jObject)
        {
            this._object = jObject;
        }

        public object this[int i]
        {
            get
            {
                if (!(_object is JArray)) return null;

                object obj = (_object as JArray)[i];
                if (obj is JValue)
                {
                    return ((JValue)obj).ToString();
                }
                return new JsonObject(obj);
            }
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = null;

            if (_object is JArray && binder.Name == "Length")
            {
                result = (_object as JArray).Count;
                return true;
            }

            JObject jObject = _object as JObject;
            object obj = jObject.SelectToken(binder.Name);

            if (obj is JValue)
                result = ((JValue)obj).ToString();
            else
                result = new JsonObject(jObject.SelectToken(binder.Name));

            return true;
        }

        public override string ToString()
        {
            return _object.ToString();
        }

        int _index = -1;

        public IEnumerator GetEnumerator()
        {
            _index = -1;
            return this;
        }

        public object Current
        {
            get
            {
                if (!(_object is JArray)) return null;
                object obj = (_object as JArray)[_index];
                if (obj is JValue) return ((JValue)obj).ToString();
                return obj;
            }
        }

        public bool MoveNext()
        {
            if (!(_object is JArray)) return false;
            _index++;
            return _index < (_object as JArray).Count;
        }

        public void Reset()
        {
            throw new NotImplementedException();
        }
    }
}

Although this is not direct answer to your deserialization question; I prefer below method utilizing dynamic instead of declaring a lot of classes

JObject o = (JObject)JsonConvert.DeserializeObject(jsonstr);
dynamic json = new JsonUtils.JsonObject(o);

foreach (var x in json.workspaces.workspace)
{
    Console.WriteLine(x.name + " " + x.href);
}

Here is the full implementation of JsonObject class, I previously posted here

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Dynamic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;

namespace JsonUtils
{
    class JsonObject : DynamicObject, IEnumerable, IEnumerator
    {
        object _object;

        public JsonObject(object jObject)
        {
            this._object = jObject;
        }

        public object this[int i]
        {
            get
            {
                if (!(_object is JArray)) return null;

                object obj = (_object as JArray)[i];
                if (obj is JValue)
                {
                    return ((JValue)obj).ToString();
                }
                return new JsonObject(obj);
            }
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = null;

            if (_object is JArray && binder.Name == "Length")
            {
                result = (_object as JArray).Count;
                return true;
            }

            JObject jObject = _object as JObject;
            object obj = jObject.SelectToken(binder.Name);

            if (obj is JValue)
                result = ((JValue)obj).ToString();
            else
                result = new JsonObject(jObject.SelectToken(binder.Name));

            return true;
        }

        public override string ToString()
        {
            return _object.ToString();
        }

        int _index = -1;

        public IEnumerator GetEnumerator()
        {
            _index = -1;
            return this;
        }

        public object Current
        {
            get
            {
                if (!(_object is JArray)) return null;
                object obj = (_object as JArray)[_index];
                if (obj is JValue) return ((JValue)obj).ToString();
                return obj;
            }
        }

        public bool MoveNext()
        {
            if (!(_object is JArray)) return false;
            _index++;
            return _index < (_object as JArray).Count;
        }

        public void Reset()
        {
            throw new NotImplementedException();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文