如何将 JSON 文件直接映射到当前实例?
How can I map a JSON file directly into current instance?
我有一个包含多个成员的 class,我可以通过使用 JSON.net.
反序列化 JSON 直接创建一个实例
如何才能获得与 class 的当前实例相同的结果?
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
}
}
不要将 LoadInfo
方法添加到您尝试 deserialize
的 class,为此创建一个单独的包装器 class。
然后添加正确的属性。
类似于:
[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;
}
}
如果你想把它保留在实例中,你可以使用反射做这样的事情:
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));
}
}
}
您不能分配给 this
,因为它是一个引用并且不允许重新分配自己的引用。所以如果你想映射属性或创建一个新的 Person 那么我想在这里建议一些选项:
- 手动映射属性
这意味着在你得到反序列化的对象后,像这样映射新的 Person:
this.Name = p.Name 等等..
- 使用像 Automapper 这样的工具
Automapper 是一个对象到对象映射器。 https://github.com/AutoMapper/AutoMapper/wiki/Getting-started
或
像@JanR 一样创建一个包装器 Class 或使用从反序列化对象创建 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 做到了!
我有一个包含多个成员的 class,我可以通过使用 JSON.net.
反序列化 JSON 直接创建一个实例如何才能获得与 class 的当前实例相同的结果?
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
}
}
不要将 LoadInfo
方法添加到您尝试 deserialize
的 class,为此创建一个单独的包装器 class。
然后添加正确的属性。
类似于:
[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;
}
}
如果你想把它保留在实例中,你可以使用反射做这样的事情:
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));
}
}
}
您不能分配给 this
,因为它是一个引用并且不允许重新分配自己的引用。所以如果你想映射属性或创建一个新的 Person 那么我想在这里建议一些选项:
- 手动映射属性
这意味着在你得到反序列化的对象后,像这样映射新的 Person: this.Name = p.Name 等等..
- 使用像 Automapper 这样的工具
Automapper 是一个对象到对象映射器。 https://github.com/AutoMapper/AutoMapper/wiki/Getting-started
或
像@JanR 一样创建一个包装器 Class 或使用从反序列化对象创建 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 做到了!