将 json 数据转换为 component.ts 中的对象
convert json data to object in component.ts
我有 json 数据,我想将其转换为对象格式以进行创建操作。
json
[
{
"user": {
"id": 83,
"username": "das",
"first_name": "dsafha",
"last_name": "dfksdfk",
"email": "sasda@gmail.com",
"is_active": true,
"is_superuser": false
},
"role": "testBu"
},
{
"user": {
"id": 84,
"username": "sadfds",
"first_name": "dshhgds",
"last_name": "fsdjsl",
"email": "fdjgd@gmail.com",
"is_active": true,
"is_superuser": false
},
"role": "testeditrole"
},
{
"user": {
"id": 86,
"username": "fs",
"first_name": "efhks",
"last_name": "sofdh",
"email": "fdshk@gmail.com",
"is_active": true,
"is_superuser": false
},
"role": "testeditrole"
},
{
"user": {
"id": 87,
"username": "xz",
"first_name": "vj",
"last_name": "vkfd",
"email": "sdsl@gmail.com",
"is_active": true,
"is_superuser": false
},
"role": "testeditrole"
}
]
我试过这样做
componet.ts
let user:any = {};
user["username"] = this.user.user.email
user["first_name"] = this.user.user.first_name
user["last_name"]= this.user.user.last_name
user["email"]= this.user.user.email
this.userData["user"] = user
this.userData["role"] = this.user.role
在输入字段中输入数据后安慰我,我没有得到角色数据。我确实得到了其他数据。角色数据显示为空。我确实认为这是因为我在 componet.ts 中编写代码的方式导致了这个问题。
this.userData["role"] = this.user.role
应该是 this.userData["role"] = this.role
无需转换。它已经是 json 格式,但您应该使用数组索引然后设置用户变量。
例如:
let user:any = {};
user = this.user[arrayIndex].user;
this.userData["user"] = user
this.userData["role"] = this.user[arrayIndex].role;
如果你想从 json 数组中获取数据,你应该使用数组索引。
像这样。
let user:any = {};
user = this.user[0].user;
this.userData["role"] = this.user[0].role;
您可以随时创建模型:
export class User {
id: string,
username: string,
first_name: string,
last_name: string,
email: string,
is_active: boolean,
is_superuser: boolean
}
export class UserDetails{
user:User;
role:string;
}
然后:
// Assume you have received the json in string form in
'resultlist' variable
let dataList = <Array<UserDetails>>JSON.parse(resultlist);
或者:
// Assume you have received the json in object form in 'resultlist' variable
let dataList = <Array<UserDetails>>resultlist;
我认为最好的方法是,创建一个与 JSON 的结构相对应的对象,然后将数据 JSON 分配给对象的数组。
class User{
id:string;
username:string;
firstName:string;
lastName:string;
email:string;
isActive:boolean;
isSuperviser:boolean;
}
class JSONData{
user:User;
role:string
}
data:JSONData[] = yourJson.data;
您将 json 数据中的 属性 名称调整为您 class 中的名称,您可以正确轻松地处理数据,请参考此 link more infos
我有 json 数据,我想将其转换为对象格式以进行创建操作。
json
[
{
"user": {
"id": 83,
"username": "das",
"first_name": "dsafha",
"last_name": "dfksdfk",
"email": "sasda@gmail.com",
"is_active": true,
"is_superuser": false
},
"role": "testBu"
},
{
"user": {
"id": 84,
"username": "sadfds",
"first_name": "dshhgds",
"last_name": "fsdjsl",
"email": "fdjgd@gmail.com",
"is_active": true,
"is_superuser": false
},
"role": "testeditrole"
},
{
"user": {
"id": 86,
"username": "fs",
"first_name": "efhks",
"last_name": "sofdh",
"email": "fdshk@gmail.com",
"is_active": true,
"is_superuser": false
},
"role": "testeditrole"
},
{
"user": {
"id": 87,
"username": "xz",
"first_name": "vj",
"last_name": "vkfd",
"email": "sdsl@gmail.com",
"is_active": true,
"is_superuser": false
},
"role": "testeditrole"
}
]
我试过这样做
componet.ts
let user:any = {};
user["username"] = this.user.user.email
user["first_name"] = this.user.user.first_name
user["last_name"]= this.user.user.last_name
user["email"]= this.user.user.email
this.userData["user"] = user
this.userData["role"] = this.user.role
在输入字段中输入数据后安慰我,我没有得到角色数据。我确实得到了其他数据。角色数据显示为空。我确实认为这是因为我在 componet.ts 中编写代码的方式导致了这个问题。
this.userData["role"] = this.user.role
应该是 this.userData["role"] = this.role
无需转换。它已经是 json 格式,但您应该使用数组索引然后设置用户变量。
例如:
let user:any = {};
user = this.user[arrayIndex].user;
this.userData["user"] = user
this.userData["role"] = this.user[arrayIndex].role;
如果你想从 json 数组中获取数据,你应该使用数组索引。 像这样。
let user:any = {};
user = this.user[0].user;
this.userData["role"] = this.user[0].role;
您可以随时创建模型:
export class User {
id: string,
username: string,
first_name: string,
last_name: string,
email: string,
is_active: boolean,
is_superuser: boolean
}
export class UserDetails{
user:User;
role:string;
}
然后:
// Assume you have received the json in string form in
'resultlist' variable
let dataList = <Array<UserDetails>>JSON.parse(resultlist);
或者:
// Assume you have received the json in object form in 'resultlist' variable
let dataList = <Array<UserDetails>>resultlist;
我认为最好的方法是,创建一个与 JSON 的结构相对应的对象,然后将数据 JSON 分配给对象的数组。
class User{
id:string;
username:string;
firstName:string;
lastName:string;
email:string;
isActive:boolean;
isSuperviser:boolean;
}
class JSONData{
user:User;
role:string
}
data:JSONData[] = yourJson.data;
您将 json 数据中的 属性 名称调整为您 class 中的名称,您可以正确轻松地处理数据,请参考此 link more infos