C# 如何将JSON文件直接映射到当前实例?

C# 如何将JSON文件直接映射到当前实例?,c#,json,oop,json.net,this,C#,Json,Oop,Json.net,This,我有一个包含许多成员的类,我可以通过使用JSON.net将JSON反序列化到该类中来直接创建实例 如何对类的当前实例实现相同的结果 class Person { public string Name; public int Age; public string[] NickNames; public Person(){} public void LoadInfo(string textFile){ Person p = this;

我有一个包含许多成员的类,我可以通过使用JSON.net将JSON反序列化到该类中来直接创建实例

如何对类的当前实例实现相同的结果

class Person {
    public string Name;
    public int Age;
    public string[] NickNames;

    public Person(){}

    public void LoadInfo(string textFile){
        Person p = this;
        p = JsonConvert.DeserializeObject<Person>(textFile);

        // I want to do something like: this = p;
        // but I can't since this operator is read-only
    }
}
班级人员{
公共字符串名称;
公共信息;
公共字符串[]昵称;
公众人物(){}
公共无效加载信息(字符串文本文件){
人p=这个;
p=JsonConvert.DeserializeObject(文本文件);
//我想做这样的事情:这个=p;
//但是我不能,因为这个操作符是只读的
}
}

不要将
LoadInfo
方法添加到您试图
反序列化的类中,为此创建一个单独的包装类

然后添加正确的属性

比如:

[DataContract]
public class Person {
    [DataMember]
    public string Name {get; set;}
    [DataMember]
    public int Age {get; set;}
    [DataMember]
    public string[] NickNames {get; set;}

    public Person(){}
}

public class PersonController
{
    public Person LoadInfo(string textFile){

        var p = JsonConvert.DeserializeObject<Person>(textFile);

        //return an instance of person, mapped from the textfile.
        return p;
    }
}
[DataContract]
公共阶层人士{
[数据成员]
公共字符串名称{get;set;}
[数据成员]
公共整数{get;set;}
[数据成员]
公共字符串[]昵称{get;set;}
公众人物(){}
}
公共类个人控制器
{
公众人物加载信息(字符串文本文件){
var p=JsonConvert.DeserializeObject(文本文件);
//返回从文本文件映射的person实例。
返回p;
}
}
如果要将其保留在实例中,可以使用反射执行以下操作:

 using System.Reflection;

 class Person {
    public string Name {get; set;}
    public int Age  {get; set;}
    public string[] NickNames  {get; set;}

    public Person(){}

    public void LoadInfo(string textFile)
    {
            //assign the values to a new instance. 
            Person p = new Person();
            p = JsonConvert.DeserializeObject<Person>(textFile);
            //get all the properties of the person class.
            var properties = this.GetType().GetProperties();
            //loop through the properties.
            foreach (var property in properties)
            {
                //Get the type (Person)
                Type type = this.GetType();
                //set the value for the property.
                type.GetProperty(property.Name).SetValue(this, type.GetProperty(property.Name).GetValue(p));
            }

     }
}
使用系统反射;
班主任{
公共字符串名称{get;set;}
公共整数{get;set;}
公共字符串[]昵称{get;set;}
公众人物(){}
公共无效加载信息(字符串文本文件)
{
//将值指定给新实例。
人员p=新人员();
p=JsonConvert.DeserializeObject(文本文件);
//获取person类的所有属性。
var properties=this.GetType().GetProperties();
//循环浏览属性。
foreach(属性中的var属性)
{
//获取类型(个人)
Type Type=this.GetType();
//设置属性的值。
type.GetProperty(property.Name).SetValue(这个,type.GetProperty(property.Name).GetValue(p));
}
}
}

不要将
LoadInfo
方法添加到您试图
反序列化的类中,为此创建一个单独的包装类

然后添加正确的属性

比如:

[DataContract]
public class Person {
    [DataMember]
    public string Name {get; set;}
    [DataMember]
    public int Age {get; set;}
    [DataMember]
    public string[] NickNames {get; set;}

    public Person(){}
}

public class PersonController
{
    public Person LoadInfo(string textFile){

        var p = JsonConvert.DeserializeObject<Person>(textFile);

        //return an instance of person, mapped from the textfile.
        return p;
    }
}
[DataContract]
公共阶层人士{
[数据成员]
公共字符串名称{get;set;}
[数据成员]
公共整数{get;set;}
[数据成员]
公共字符串[]昵称{get;set;}
公众人物(){}
}
公共类个人控制器
{
公众人物加载信息(字符串文本文件){
var p=JsonConvert.DeserializeObject(文本文件);
//返回从文本文件映射的person实例。
返回p;
}
}
如果要将其保留在实例中,可以使用反射执行以下操作:

 using System.Reflection;

 class Person {
    public string Name {get; set;}
    public int Age  {get; set;}
    public string[] NickNames  {get; set;}

    public Person(){}

    public void LoadInfo(string textFile)
    {
            //assign the values to a new instance. 
            Person p = new Person();
            p = JsonConvert.DeserializeObject<Person>(textFile);
            //get all the properties of the person class.
            var properties = this.GetType().GetProperties();
            //loop through the properties.
            foreach (var property in properties)
            {
                //Get the type (Person)
                Type type = this.GetType();
                //set the value for the property.
                type.GetProperty(property.Name).SetValue(this, type.GetProperty(property.Name).GetValue(p));
            }

     }
}
使用系统反射;
班主任{
公共字符串名称{get;set;}
公共整数{get;set;}
公共字符串[]昵称{get;set;}
公众人物(){}
公共无效加载信息(字符串文本文件)
{
//将值指定给新实例。
人员p=新人员();
p=JsonConvert.DeserializeObject(文本文件);
//获取person类的所有属性。
var properties=this.GetType().GetProperties();
//循环浏览属性。
foreach(属性中的var属性)
{
//获取类型(个人)
Type Type=this.GetType();
//设置属性的值。
type.GetProperty(property.Name).SetValue(这个,type.GetProperty(property.Name).GetValue(p));
}
}
}

您不能分配给
,因为它是一个引用,不允许重新分配自己的引用。因此,如果您想映射属性或创建一个新人,那么我想在此建议一些选项:

  • 手动映射属性
  • 这意味着在获得反序列化对象后,映射新的Person,如: this.Name=p.Name等等

  • 使用像Automapper这样的工具
  • Automapper是一个对象到对象的映射器

    创建一个像@JanR这样的包装类,或者使用静态方法从反序列化对象创建Person


    我希望这有帮助。

    您不能分配给
    ,因为它是一个引用,不允许重新分配自己的引用。因此,如果您想映射属性或创建一个新人,那么我想在此建议一些选项:

  • 手动映射属性
  • 这意味着在获得反序列化对象后,映射新的Person,如: this.Name=p.Name等等

  • 使用像Automapper这样的工具
  • Automapper是一个对象到对象的映射器

    创建一个像@JanR这样的包装类,或者使用静态方法从反序列化对象创建Person


    我希望这能有所帮助。

    我想您要搜索的是JsonConvert.PopulateObject:

    class Person {
        public string Name;
        public int Age;
        public string[] NickNames;
    
        public Person(){}
    
        public void LoadInfo(string textFile){
            JsonConvert.PopulateObject(textFile, this);
        }
    }
    

    一句话:如果是关于Json的,Json.Net就是这样做的

    我想您要搜索的是JsonConvert.PopulateObject:

    class Person {
        public string Name;
        public int Age;
        public string[] NickNames;
    
        public Person(){}
    
        public void LoadInfo(string textFile){
            JsonConvert.PopulateObject(textFile, this);
        }
    }
    

    一句话:如果是关于Json的,Json.Net就是这样做的

    只是想确定一下,您是否正在尝试将属性和值从json映射到类?无法赋值“”,因为它是只读的[this=p is forbeden]@KrishnaDhungana是的,json文件存储属性的值,我希望将它们加载到当前实例中。是否有特殊原因使您不只是使用接受JSON(或静态工厂方法)的构造函数?@lc。不,这个例子甚至不需要它。但对于我的实际代码,它用于在不加载文件的情况下实例化它。只是想确定一下,您是否正在尝试将属性和值从json映射到类?不能