反序列化JSON的问题我的类总是空的

本文关键字:我的 JSON 问题 反序列化 | 更新日期: 2023-09-27 18:10:56

我有以下JSON:

{"工作区":{"工作区":({"名称":"柏林"、"href":"http://10.80.14.188:8080/geoserver/休息/工作区/柏林。json "}, {" 名称":"巴黎"、"href":"http://10.80.14.188:8080 geoserver/rest/工作区/Paris.json "},{" name ":"罗马","href":"http://10.80.14.188:8080/geoserver/休息/工作区/罗马。json "}, {" 名称":"伦敦"、"href":"http://10.80.14.188:8080 geoserver/rest/工作区/London.json "},{" name ":"美国","href":"http://10.80.14.188:8080/geoserver/休息/工作区/美国。json "}, {" 名称":"葡京"、"href":"http://10.80.14.188:8080 geoserver/休息/工作区/葡京。json "}, {" 名称":"马德里"、"href":"http://10.80.14.188:8080 geoserver/休息/工作区/Madrid.json"}]}}

下面的类:

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

我试图用json填充我的类,但元素总是空的。我使用´:

ObjJSON test = JsonConvert.DeserializeObject<ObjJSON>(data);

我的环境是Visual Studio 2010 c#

任何想法?我是c#新手。

反序列化JSON的问题我的类总是空的

您需要创建代表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);

虽然这不是直接回答你的反序列化问题;我更喜欢下面使用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();
        }
    }
}