将字典转换为c#对象
我有一个嵌套的dictionary对象输出,如下所示将字典转换为c#对象,c#,asp.net,.net,model-view-controller,c#-4.0,C#,Asp.net,.net,Model View Controller,C# 4.0,我有一个嵌套的dictionary对象输出,如下所示 { “名称”:“测试”, “id”:1, “家长”:{ “名称”:“test1”, “id”:2, “家长”:{ “名称”:“test2”, “id”:3, “家长”:{ “名称”:“test3”, “id”:4, “家长”:{ .... } } } } } 以下是返回dictionary对象的代码: 类获取数据 { 公共HttpWebResponse WebResponse{get;private set;} 公共字典数据{get;pri
{
“名称”:“测试”,
“id”:1,
“家长”:{
“名称”:“test1”,
“id”:2,
“家长”:{
“名称”:“test2”,
“id”:3,
“家长”:{
“名称”:“test3”,
“id”:4,
“家长”:{
....
}
}
}
}
}
以下是返回dictionary对象的代码:
类获取数据
{
公共HttpWebResponse WebResponse{get;private set;}
公共字典数据{get;private set;}
公共GetData(HttpWebResponse、webResponse、字典数据)
{
WebResponse=WebResponse;
数据=数据;
}
}
我有一个对象包含上述响应,如下所示:
var responseData=GetResponseData(请求);
现在我想把上面的dictionary对象转换成c#对象。以下是我的c#对象
class-SomeClass
{
公共int Id{get;}
公共字符串名称{get;}
公共类父级{get;}
公共IEnumerable GetGenestory()
{
if(父项不为空)
{
收益母公司;
foreach(Parent.get祖先()中的var祖先)产生返回祖先;
}
}
}
我正在尝试以下方法将dictionary对象转换为“SomeClass”对象,但它对我不起作用。有人能帮我吗
List smClass=新列表(responseData.Values);
尝试创建结构(或类),例如:
然后,您可以通过Newtonsoft.json.JsonConvert.DeserializeObject反序列化json(您需要Newtonsoft.json库或NuGet包)
NamedObject NamedObject=JsonConvert.DeserializeObject(json);
工作示例(来自您之前的示例):
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Newtonsoft.Json;
名称空间控制台EAPP1
{
内部课程计划
{
私有静态void Main(字符串[]args)
{
控制台。WriteLine(“你好,世界!”);
字符串s=“{\'name\':\'test\',\'id\':1,\'parent\':{\'name\':\'test1\',\'id\':2,\'parent\':{\'name\':\'test2\',\'id\':3,\'parent\':{\'name\':'test3\',\'id\'4,\'parent\'null}”;
TreeNode节点=JsonConvert.DeserializeObject;
}
}
公共级树节点
{
[JsonProperty(“名称”)]
公共字符串名称{get;set;}
[JsonProperty(“id”)]
公共int ID{get;set;}
公共节点类型{get;}
公共字典Childern{get;}
公共树节点getChilderByName(字符串名称)=>Childern.First(x=>x.Key.name==name).Key;
[JsonProperty(“母公司”)]
公共树节点父{get;private set;}
公共字符串FullName=>IsRoot?string.Empty:this.Parent.FullName+“/”+Name;
公共树节点(字符串名称、节点类型、树节点父节点)
{
this.Name=Name;
this.Type=Type;
这个。父=父;
this.Childern=新字典();
}
公共树节点()
{
}
publictreenode(字符串名):这个(名称,NodeType.Folder,null){}
公共树节点(字符串名称,节点类型):this(名称,类型,null){}
公共bool IsRoot=>Parent==null;
public void AddChild(params TreeNode[]节点)=>AddChild(true,节点);
public void AddChild(bool setParent,params TreeNode[]节点)
{
foreach(节点中的树节点)
{
添加(节点,节点类型);
如果(setParent)node.Parent=this;
}
}
公共IEnumerable GetFullPath()
{
列表父项=新列表();
如果(this.Parent!=null)
{
parents.AddRange(Parent.GetFullPath());
}
父母。加上(这个);
返回父母;
}
公共重写字符串ToString()=>名称;
}
公共枚举节点类型
{
文件夹,
文件
}
}
可以对公共字段使用与json中相同的名称,也可以根据需要设置JsonProperty属性。请记住,您必须使用不带参数的构造函数,并且必须使用可为空的类型(例如int?
GetData
是类的可怕名称。您发布的是JSON字符串,而不是字典。您发布的内容不会执行任何类型的JSON解析。GetData
类不返回任何内容,它只是将构造函数参数作为属性公开。为什么要使用对象字典而不是类型化字典。听起来你对体系结构有误解,你应该描述你试图做什么,而不是你想要的解决方案将my dictionary对象转换为SomeClass
对象为什么要创建一个列表然后list(responseData.Values)
?你尝试过反序列化这个JSON字符串吗?例如,JsonConvert.DeserializeObject(json)。不需要使用GetData
类或Dictionary
公共字段作为属性。属性是类接口的一部分,字段始终被视为实现细节、公共或not@PanagiotisKanavos谢谢,我错过了这个。更正。您也不需要JsonProperty
,JSON.NET默认使用驼峰大小写,也只是一个快速观察,NamedObject NamedObject=JsonConvert.DeserializeObject(JSON)
应该是NamedObject NamedObject=JsonConvert.DeserializeObject(json)
@PanagiotisKanavos,这对我来说是一个新的观点,我只在一个项目中使用json,在那里我有驼峰大小写名称,所以我从不详细讨论这个问题。我只知道当名称不匹配时存在JsonProperty。
public class NamedObject
{
int id {get; set;}
string name {get; set;}
NamedObject parent {get; set;}
}
NamedObject namedObject = JsonConvert.DeserializeObject<NamedObject>(json);
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
namespace ConsoleApp1
{
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Hello World!");
string s = "{ \"name\":\"test\", \"id\":1, \"parent\": { \"name\":\"test1\", \"id\":2, \"parent\":{ \"name\":\"test2\", \"id\":3, \"parent\":{ \"name\":\"test3\", \"id\":4, \"parent\": null } } } }";
TreeNode node = JsonConvert.DeserializeObject<TreeNode>(s);
}
}
public class TreeNode
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("id")]
public int ID { get; set; }
public NodeType Type { get; }
public Dictionary<TreeNode, NodeType> Childern { get; }
public TreeNode GetChildernByName(string name) => Childern.First(x => x.Key.Name == name).Key;
[JsonProperty("parent")]
public TreeNode Parent { get; private set; }
public string FullName => IsRoot ? string.Empty : this.Parent.FullName + "/" + Name;
public TreeNode(string name, NodeType type, TreeNode parent)
{
this.Name = name;
this.Type = type;
this.Parent = parent;
this.Childern = new Dictionary<TreeNode, NodeType>();
}
public TreeNode()
{
}
public TreeNode(string name) : this(name, NodeType.Folder, null) { }
public TreeNode(string name, NodeType type) : this(name, type, null) { }
public bool IsRoot => Parent == null;
public void AddChild(params TreeNode[] nodes) => AddChild(true, nodes);
public void AddChild(bool setParent, params TreeNode[] nodes)
{
foreach (TreeNode node in nodes)
{
Childern.Add(node, node.Type);
if (setParent) node.Parent = this;
}
}
public IEnumerable<TreeNode> GetFullPath()
{
List<TreeNode> parents = new List<TreeNode>();
if (this.Parent != null)
{
parents.AddRange(Parent.GetFullPath());
}
parents.Add(this);
return parents;
}
public override string ToString() => Name;
}
public enum NodeType
{
Folder,
File
}
}