如何显示JSON格式的数据?
How to display data from JSON format?
我需要可以在以下层次结构中打印数据的循环 solutiondetail -> groups -> request details
以下功能为我提供了 solutiondetail -> request detail 的详细信息。我已经给出了新旧 json 格式以及旧 json 的工作函数。现在,在添加组详细信息后,我遇到了循环问题。
上一个 Json:解决方案详情 -> 请求
{
"SolutionsDetail": [
{
"SolutionId": 658,
"name": "dk",
"id": 1568377327000,
"requestDetails": [
{
"ReqId": 2331,
},
]
}
]
}
GeneartingLandingPadData(nxsolId) {
for (let index = 0; index < this.json.length; index++) {
if (this.array[index].SolutionId == nxsolId) {
this.priceDetails = this.array[index].requestdetails;
for (let k = 0; k < this.priceDetails.length; k++) {
console.log("some",this.priceDetails);
}
}
}
return false;
}
新Json:解决方案详情->群组->请求详情
{
"SolutionsDetail": [
{
"SolutionId": 658,
"name": "dk",
"id": 1568377327000,
"groups": [
{
"GroupId": 1,
"requestDetails": [
{
"ReqId": 2331,
},
]
}
]
}
]
}
由于您提供的信息量有限,我无法重现问题来进行测试。但是据我了解,我认为像下面这样更改您的功能可能会解决您的问题。使用新的 JSON,您必须首先访问组数组。然后继续requestId。即使下面的代码不能完全工作,希望您能够通过首先访问组数组然后尝试请求 ID 来解决它。
GeneartingLandingPadData(nxsolId) {
for (let index = 0; index < this.json.length; index++) {
if (this.array[index].SolutionId == nxsolId) {
var groupDetails = this.array[index].groups;
for (let k = 0; k < groupDetails.length; k++) {
var priceDetails = groupDetails[k].requestDetails;
for (let j = 0; j < priceDetails.length; j++){
console.log("some", priceDetails);
}
}
}
}
return false;
}
由于您希望在控制台中打印数据,我假设您需要为每个请求详细信息显示整行
solutiondetail -> groups - > request details
相关TS:
GeneartingLandingPadData(nxsolId) {
for (let index = 0; index < this.newJson.SolutionsDetail.length; index++) {
if (this.newJson.SolutionsDetail[index].SolutionId == nxsolId) {
for (let j = 0; j < this.newJson.SolutionsDetail[index].groups.length; j++) {
for (let k = 0; k < this.newJson.SolutionsDetail[index].groups[j].requestDetails.length; k++) {
console.log("solutiondetail :", this.newJson.SolutionsDetail[index].name, " -> Groups:", this.newJson.SolutionsDetail[index].groups[j].GroupId, " -> Request detail:", this.newJson.SolutionsDetail[index].groups[j].requestDetails[k].ReqId);
}
}
}
}
}
如果我对你的理解是正确的:你想把旧的 JSON 改造成新的。因此,map() 方法是转换数组的最佳方式,通过这种方式 - 转换您的 JSON。这是另一个使用 TypeScript 制作的版本,它允许您逃避 for 循环 的深层嵌套,
我们取一个旧的 JSON 并转换它的 'SolutionsDetail' 数组。也许对你也有帮助。
工作样本:https://stackblitz.com/edit/angular-8ryrvw
下面的方法returns一个新的JSON:
GeneartingLandingPadData(oldJson: any): any {
return oldJson['SolutionsDetail'].map((sDetail: any) => {
let groups: Array<any> = [];
sDetail['requestDetails'].forEach((details: any, index: number) => {
groups.push({ GroupId: index + 1, requestDetails: [details] });
});
delete sDetail['requestDetails'];
sDetail['groups'] = groups;
return sDetail;
});
}
旧JSON
{
"SolutionsDetail": [
{
"SolutionId": 658,
"name": "dk",
"id": 1568377327000,
"requestDetails": [
{
"ReqId": 2331,
},
]
}
]
}
新建JSON
{
"SolutionsDetail": [
{
"SolutionId": 658,
"name": "dk",
"id": 1568377327000,
"groups": [
{
"GroupId": 1,
"requestDetails": [
{
"ReqId": 2331,
},
]
}
]
}
]
}
对我有用)祝你好运。
我需要可以在以下层次结构中打印数据的循环 solutiondetail -> groups -> request details 以下功能为我提供了 solutiondetail -> request detail 的详细信息。我已经给出了新旧 json 格式以及旧 json 的工作函数。现在,在添加组详细信息后,我遇到了循环问题。
上一个 Json:解决方案详情 -> 请求
{
"SolutionsDetail": [
{
"SolutionId": 658,
"name": "dk",
"id": 1568377327000,
"requestDetails": [
{
"ReqId": 2331,
},
]
}
]
}
GeneartingLandingPadData(nxsolId) {
for (let index = 0; index < this.json.length; index++) {
if (this.array[index].SolutionId == nxsolId) {
this.priceDetails = this.array[index].requestdetails;
for (let k = 0; k < this.priceDetails.length; k++) {
console.log("some",this.priceDetails);
}
}
}
return false;
}
新Json:解决方案详情->群组->请求详情
{
"SolutionsDetail": [
{
"SolutionId": 658,
"name": "dk",
"id": 1568377327000,
"groups": [
{
"GroupId": 1,
"requestDetails": [
{
"ReqId": 2331,
},
]
}
]
}
]
}
由于您提供的信息量有限,我无法重现问题来进行测试。但是据我了解,我认为像下面这样更改您的功能可能会解决您的问题。使用新的 JSON,您必须首先访问组数组。然后继续requestId。即使下面的代码不能完全工作,希望您能够通过首先访问组数组然后尝试请求 ID 来解决它。
GeneartingLandingPadData(nxsolId) {
for (let index = 0; index < this.json.length; index++) {
if (this.array[index].SolutionId == nxsolId) {
var groupDetails = this.array[index].groups;
for (let k = 0; k < groupDetails.length; k++) {
var priceDetails = groupDetails[k].requestDetails;
for (let j = 0; j < priceDetails.length; j++){
console.log("some", priceDetails);
}
}
}
}
return false;
}
由于您希望在控制台中打印数据,我假设您需要为每个请求详细信息显示整行
solutiondetail -> groups - > request details
相关TS:
GeneartingLandingPadData(nxsolId) {
for (let index = 0; index < this.newJson.SolutionsDetail.length; index++) {
if (this.newJson.SolutionsDetail[index].SolutionId == nxsolId) {
for (let j = 0; j < this.newJson.SolutionsDetail[index].groups.length; j++) {
for (let k = 0; k < this.newJson.SolutionsDetail[index].groups[j].requestDetails.length; k++) {
console.log("solutiondetail :", this.newJson.SolutionsDetail[index].name, " -> Groups:", this.newJson.SolutionsDetail[index].groups[j].GroupId, " -> Request detail:", this.newJson.SolutionsDetail[index].groups[j].requestDetails[k].ReqId);
}
}
}
}
}
如果我对你的理解是正确的:你想把旧的 JSON 改造成新的。因此,map() 方法是转换数组的最佳方式,通过这种方式 - 转换您的 JSON。这是另一个使用 TypeScript 制作的版本,它允许您逃避 for 循环 的深层嵌套, 我们取一个旧的 JSON 并转换它的 'SolutionsDetail' 数组。也许对你也有帮助。
工作样本:https://stackblitz.com/edit/angular-8ryrvw
下面的方法returns一个新的JSON:
GeneartingLandingPadData(oldJson: any): any {
return oldJson['SolutionsDetail'].map((sDetail: any) => {
let groups: Array<any> = [];
sDetail['requestDetails'].forEach((details: any, index: number) => {
groups.push({ GroupId: index + 1, requestDetails: [details] });
});
delete sDetail['requestDetails'];
sDetail['groups'] = groups;
return sDetail;
});
}
旧JSON
{
"SolutionsDetail": [
{
"SolutionId": 658,
"name": "dk",
"id": 1568377327000,
"requestDetails": [
{
"ReqId": 2331,
},
]
}
]
}
新建JSON
{
"SolutionsDetail": [
{
"SolutionId": 658,
"name": "dk",
"id": 1568377327000,
"groups": [
{
"GroupId": 1,
"requestDetails": [
{
"ReqId": 2331,
},
]
}
]
}
]
}
对我有用)祝你好运。