开发者

Problems to desearilize a JSON my class is always null

开发者 https://www.devze.com 2023-04-12 13:34 出处:网络
I\'ve got the followingJSON: {\"workspaces\":{ \"workspace\":[ {\"name\":\"Berlin\",\"href\":\"http://10.80.14.188:8080/geoserver/rest/workspaces/Berlin.json\"},{\"name\":\"Paris\",\"href\":\"http://

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:808开发者_JS百科0/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#.


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


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();
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号