Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 取消JSON搜索的问题我的类总是空的_C#_C# 4.0 - Fatal编程技术网

C# 取消JSON搜索的问题我的类总是空的

C# 取消JSON搜索的问题我的类总是空的,c#,c#-4.0,C#,C# 4.0,我得到了以下JSON: {“工作区”:{ “工作区”:[ {“名称”:“柏林”,“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"}, {“名称”:“罗马”,“href”:”http://10.80.14.188:8080/geoser

我得到了以下JSON:

{“工作区”:{ “工作区”:[ {“名称”:“柏林”,“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"}, {“名称”:“罗马”,“href”:”http://10.80.14.188:8080/geoserver/rest/workspaces/Rome.json},{“名称”:“伦敦”,“href”:http://10.80.14.188:8080/geoserver/rest/workspaces/London.json"}, {“名称”:“美国”,“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},{“名称”:“马德里”,“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填充我的类,但是元素总是空的。我正在使用:

ObjJSON test = JsonConvert.DeserializeObject<ObjJSON>(data);
ObjJSON test=JsonConvert.DeserializeObject(数据);
我的环境是Visual Studio 2010 C#


有什么想法吗?我是C#的新手。

您需要创建表示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; }
}
classjsonobj//这个类表示主JSON对象{…}
{
公共工作空间子工作空间{get;set;}
}
类WorkspacesJson//此类表示工作区JSON对象“workspaces”:{…}
{
公共列表工作区{get;set;}//这表示JSON数组“workspace”:[…]
}
类WorkspaceJson//这表示工作区JSON数组{“name”:…,“href”:…}的名称/值对
{
公共字符串名称{get;set;}
公共字符串href{get;set;}
}
然后可以反序列化:

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

您需要创建表示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; }
}
classjsonobj//这个类表示主JSON对象{…}
{
公共工作空间子工作空间{get;set;}
}
类WorkspacesJson//此类表示工作区JSON对象“workspaces”:{…}
{
公共列表工作区{get;set;}//这表示JSON数组“workspace”:[…]
}
类WorkspaceJson//这表示工作区JSON数组{“name”:…,“href”:…}的名称/值对
{
公共字符串名称{get;set;}
公共字符串href{get;set;}
}
然后可以反序列化:

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

尽管这不是对反序列化问题的直接回答;我更喜欢下面使用
动态
的方法,而不是声明很多类

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类的完整实现

使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
运用系统动力学;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;
使用Newtonsoft.Json.Serialization;
命名空间JsonUtils
{
类JsonObject:DynamicObject、IEnumerable、IEnumerator
{
对象(object);;
公共JsonObject(对象jObject)
{
这个._object=jObject;
}
公共对象此[int i]
{
得到
{
如果(!(_对象为JArray))返回null;
objectobj=(_objectas JArray)[i];
if(obj是JValue)
{
return((JValue)obj.ToString();
}
返回新的JsonObject(obj);
}
}
公共重写bool TryGetMember(GetMemberBinder绑定器,输出对象结果)
{
结果=空;
if(_对象是JArray&&binder.Name==“Length”)
{
结果=(_对象为JArray).Count;
返回true;
}
JObject JObject=_对象作为JObject;
object obj=jObject.SelectToken(binder.Name);
if(obj是JValue)
结果=((JValue)obj).ToString();
其他的
结果=新的JsonObject(jObject.SelectToken(binder.Name));
返回true;
}
公共重写字符串ToString()
{
返回_object.ToString();
}
int_指数=-1;
公共IEnumerator GetEnumerator()
{
_指数=-1;
归还这个;
}
公共对象流
{
得到
{
如果(!(_对象为JArray))返回null;
objectobj=(_objectas JArray)[_index];
if(obj是JValue)返回((JValue)obj.ToString();
返回obj;
}
}
公共图书馆
{
如果(!(_对象为JArray))返回false;
_索引++;
返回_索引<(_对象为JArray).Count;
}
公共无效重置()
{
抛出新的NotImplementedException();
}
}
}

尽管这不是对反序列化问题的直接回答;我更喜欢下面使用
动态
的方法,而不是声明很多类

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类的完整实现

使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
运用系统动力学;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;
使用Newtonsoft.Json.Serialization;
命名空间JsonUtils
{
类JsonObject:DynamicObject、IEnumerable、IEnumerator
{
对象(object);;
公共JsonObject(对象jObject)
{
这个._object=jObject;
}
公共对象此[int i]
{
得到
{
如果(!(_对象为JArray))返回null;
objectobj=(_objectas JArray)[i];
if(obj是JValue)
{
返回值((JValue)