映射 JSON 到现有的深层对象结构
Map JSON to existing deep object structure
假设我有以下 Typescript 模型:
class Person{
public Address: Address;
public FirstName: string;
public LastName: string;
constructor(){
this.Address = new Address();
}
}
我通过 JSON 从服务器获得了这个对象的精确表示。
我将如何着手设置 Person 和 Address 的属性,但保持现有对象不变。
与此非常相似,但一般来说:
public SetData(json:any){
this.Address.City = json.Address.City;
this.Address.Province = json.Address.Province;
this.FirstName = json.FirstName;
}
问题是原始对象必须保留并调用 setter,因为它们是 Mobx 可观察对象。这排除了 Object.assign 和我发现的任何 'extend' 方法。
谢谢。
在稍微简化的情况下,您可以手动完成而不需要太多 effort:
class Address
{
public City: string;
public Province: string;
}
class Person{
public Address: Address;
public FirstName: string;
public LastName: string;
constructor() {
this.Address = new Address();
}
private SetDataInternal(target: any, json: any)
{
if (typeof json === "undefined" || json === null)
{
return;
}
for (let propName of Object.keys(json))
{
const val = target[propName];
if (typeof val === "object")
{
this.SetDataInternal(val, json[propName]);
}
else
{
target[propName] = json[propName];
}
}
}
public SetData(json: any)
{
this.SetDataInternal(this, json);
}
}
const json = {
Address: {
City: "AAA",
Province: "BBB"
},
FirstName: "CCC"
}
const p = new Person();
p.SetData(json);
console.log(p);
它肯定会遗漏一些检查和特殊情况验证,但除此之外它会满足您的要求。
我基于 Amids 的最终实现:
从 "underscore" 导入 * 作为 _;
export class ObjectMapper
{
public static MapObject(source: any, destination: any) {
_.mapObject(source, (val, key) => {
if(_.isObject(val))
{
this.MapObject(val, destination[key]);
}
else if(_.isArray(val))
{
const array = destination[key];
for(var i in val)
{
const newObject = {};
_.extend(newObject, val[i]);
array.push(newObject);
}
}
else
{
destination[key] = val;
}
});
}
}
假设我有以下 Typescript 模型:
class Person{
public Address: Address;
public FirstName: string;
public LastName: string;
constructor(){
this.Address = new Address();
}
}
我通过 JSON 从服务器获得了这个对象的精确表示。
我将如何着手设置 Person 和 Address 的属性,但保持现有对象不变。
与此非常相似,但一般来说:
public SetData(json:any){
this.Address.City = json.Address.City;
this.Address.Province = json.Address.Province;
this.FirstName = json.FirstName;
}
问题是原始对象必须保留并调用 setter,因为它们是 Mobx 可观察对象。这排除了 Object.assign 和我发现的任何 'extend' 方法。
谢谢。
在稍微简化的情况下,您可以手动完成而不需要太多 effort:
class Address
{
public City: string;
public Province: string;
}
class Person{
public Address: Address;
public FirstName: string;
public LastName: string;
constructor() {
this.Address = new Address();
}
private SetDataInternal(target: any, json: any)
{
if (typeof json === "undefined" || json === null)
{
return;
}
for (let propName of Object.keys(json))
{
const val = target[propName];
if (typeof val === "object")
{
this.SetDataInternal(val, json[propName]);
}
else
{
target[propName] = json[propName];
}
}
}
public SetData(json: any)
{
this.SetDataInternal(this, json);
}
}
const json = {
Address: {
City: "AAA",
Province: "BBB"
},
FirstName: "CCC"
}
const p = new Person();
p.SetData(json);
console.log(p);
它肯定会遗漏一些检查和特殊情况验证,但除此之外它会满足您的要求。
我基于 Amids 的最终实现:
从 "underscore" 导入 * 作为 _;
export class ObjectMapper
{
public static MapObject(source: any, destination: any) {
_.mapObject(source, (val, key) => {
if(_.isObject(val))
{
this.MapObject(val, destination[key]);
}
else if(_.isArray(val))
{
const array = destination[key];
for(var i in val)
{
const newObject = {};
_.extend(newObject, val[i]);
array.push(newObject);
}
}
else
{
destination[key] = val;
}
});
}
}