我想将我的json嵌套数组对象展平为平面数组
根据用户输入数组
,键和值对都应该是动态的我尝试编写自己的代码,但我不熟悉JavaScript函数来执行concat,push或其他功能。
输入:
{
"ReportID": "ProfitAndLoss",
"ReportName": "Profit and Loss",
"ReportType": 7,
"ReportTitles": [
"Profit & Loss",
"Clearlight Saunas Australia Pty Ltd",
"1 May 2017 to 31 May 2017"
],
"ReportDate": "17 May 2017",
"UpdatedDateUTC": "2017-05-17T05:52:23.414Z",
"Attributes": null,
"Fields": [],
"Rows": [
{
"RowType": "Header",
"Cells": [
{
"Value": "",
"Attributes": null
},
{
"Value": "31 May 17",
"Attributes": null
}
],
"Title": null,
"Rows": null
},
{
"RowType": "Section",
"Cells": null,
"Title": "Income",
"Rows": [
{
"RowType": "Row",
"Cells": [
{
"Value": "Curve Dome Sauna",
"Attributes": [
{
"Value": "a76bacdc-a411-4924-9860-7c8b1bf36e5b",
"Id": "account"
}
]
},
{
"Value": "6725.46",
"Attributes": [
{
"Value": "a76bacdc-a411-4924-9860-7c8b1bf36e5b",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "Row",
"Cells": [
{
"Value": "Essential Sauna Sales",
"Attributes": [
{
"Value": "b2544842-1e51-4c08-80bc-7077f01a8657",
"Id": "account"
}
]
},
{
"Value": "21359.09",
"Attributes": [
{
"Value": "b2544842-1e51-4c08-80bc-7077f01a8657",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "Row",
"Cells": [
{
"Value": "Premier Sauna Sales",
"Attributes": [
{
"Value": "7f5bb82f-f488-441e-b578-c3e6d16829f0",
"Id": "account"
}
]
},
{
"Value": "19088.18",
"Attributes": [
{
"Value": "7f5bb82f-f488-441e-b578-c3e6d16829f0",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "Row",
"Cells": [
{
"Value": "Revenue - Installation ",
"Attributes": [
{
"Value": "e4985303-4ce7-4162-9ed3-e9a7799b5584",
"Id": "account"
}
]
},
{
"Value": "636.36",
"Attributes": [
{
"Value": "e4985303-4ce7-4162-9ed3-e9a7799b5584",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "Row",
"Cells": [
{
"Value": "Sanctuary sauna sale",
"Attributes": [
{
"Value": "90bda37d-e484-494c-8ad5-dd012f6acb56",
"Id": "account"
}
]
},
{
"Value": "26244.55",
"Attributes": [
{
"Value": "90bda37d-e484-494c-8ad5-dd012f6acb56",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "Row",
"Cells": [
{
"Value": "Sauna cover sale",
"Attributes": [
{
"Value": "5a1aa34f-9acb-4154-8d4c-bb7e56a16cc8",
"Id": "account"
}
]
},
{
"Value": "727.27",
"Attributes": [
{
"Value": "5a1aa34f-9acb-4154-8d4c-bb7e56a16cc8",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "SummaryRow",
"Cells": [
{
"Value": "Total Income",
"Attributes": null
},
{
"Value": "74780.91",
"Attributes": null
}
],
"Title": null,
"Rows": null
}
]
},
{
"RowType": "Section",
"Cells": null,
"Title": "",
"Rows": [
{
"RowType": "Row",
"Cells": [
{
"Value": "Gross Profit",
"Attributes": null
},
{
"Value": "74780.91",
"Attributes": null
}
],
"Title": null,
"Rows": null
}
]
},
{
"RowType": "Section",
"Cells": null,
"Title": "Plus Other Income",
"Rows": [
{
"RowType": "Row",
"Cells": [
{
"Value": "Sauna Sale to Clearlight NZ Ltd",
"Attributes": [
{
"Value": "12eca2ea-00ff-4f1a-9187-bd3faaf15eae",
"Id": "account"
}
]
},
{
"Value": "1309.09",
"Attributes": [
{
"Value": "12eca2ea-00ff-4f1a-9187-bd3faaf15eae",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "SummaryRow",
"Cells": [
{
"Value": "Total Other Income",
"Attributes": null
},
{
"Value": "1309.09",
"Attributes": null
}
],
"Title": null,
"Rows": null
}
]
},
{
"RowType": "Section",
"Cells": null,
"Title": "Less Operating Expenses",
"Rows": [
{
"RowType": "Row",
"Cells": [
{
"Value": "Bank Fees",
"Attributes": [
{
"Value": "1ee9c97a-5707-4289-9d40-fa25d2f087d3",
"Id": "account"
}
]
},
{
"Value": "503.54",
"Attributes": [
{
"Value": "1ee9c97a-5707-4289-9d40-fa25d2f087d3",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "Row",
"Cells": [
{
"Value": "Sauna Storage",
"Attributes": [
{
"Value": "f3813ca9-9a0f-4c31-8afb-a1f9af61ca9a",
"Id": "account"
}
]
},
{
"Value": "670.00",
"Attributes": [
{
"Value": "f3813ca9-9a0f-4c31-8afb-a1f9af61ca9a",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "SummaryRow",
"Cells": [
{
"Value": "Total Operating Expenses",
"Attributes": null
},
{
"Value": "1173.54",
"Attributes": null
}
],
"Title": null,
"Rows": null
}
]
},
{
"RowType": "Section",
"Cells": null,
"Title": "",
"Rows": [
{
"RowType": "Row",
"Cells": [
{
"Value": "Net Profit",
"Attributes": null
},
{
"Value": "74916.46",
"Attributes": null
}
],
"Title": null,
"Rows": null
}
]
}
]
}
这是我想要的输出:
[
{
"ReportID": "ProfitAndLoss",
"ReportName": "Profit and Loss",
"ReportType": "7",
"ReportTitles": "Profit & Loss",
"ReportDate": "17 May 2017",
"UpdatedDateUTC": "2017-05-17T05:52:23.414Z",
"Attributes": null,
"Rows__RowType": "Header",
"Rows__Cells": "",
"Rows__Cells____Value": "",
"Rows__Cells____Attributes": null,
"Rows__Title": null,
"Rows__Rows____RowType": "",
"Rows__Rows____Cells__Value": "",
"Rows__Rows____Cells__Attributes": "",
"Rows__Rows____Cells__Attributes____Value": "",
"Rows__Rows____Cells__Attributes____Id": "",
"Rows__Rows____Title": "",
"Rows__Rows____Rows": "",
"Rows__Rows": null
},
{
"ReportID": "",
"ReportName": "",
"ReportType": "",
"ReportTitles": "Clearlight Saunas Australia Pty Ltd",
"ReportDate": "",
"UpdatedDateUTC": "",
"Attributes": "",
"Rows__RowType": "",
"Rows__Cells": "",
"Rows__Cells____Value": "31 May 17",
"Rows__Cells____Attributes": null,
"Rows__Title": "",
"Rows__Rows____RowType": "",
"Rows__Rows____Cells__Value": "",
"Rows__Rows____Cells__Attributes": "",
"Rows__Rows____Cells__Attributes____Value": "",
"Rows__Rows____Cells__Attributes____Id": "",
"Rows__Rows____Title": "",
"Rows__Rows____Rows": "",
"Rows__Rows": ""
},
{
"ReportID": "",
"ReportName": "",
"ReportType": "",
"ReportTitles": "1 May 2017 to 31 May 2017",
"ReportDate": "",
"UpdatedDateUTC": "",
"Attributes": "",
"Rows__RowType": "Section",
"Rows__Cells": null,
"Rows__Cells____Value": "",
"Rows__Cells____Attributes": "",
"Rows__Title": "Income",
"Rows__Rows____RowType": "Row",
"Rows__Rows____Cells__Value": "Curve Dome Sauna",
"Rows__Rows____Cells__Attributes": "",
"Rows__Rows____Cells__Attributes____Value": "a76bacdc-a411-4924-9860-7c8b1bf36e5b",
"Rows__Rows____Cells__Attributes____Id": "account",
"Rows__Rows____Title": null,
"Rows__Rows____Rows": null,
"Rows__Rows": ""
},
{
"ReportID": "",
"ReportName": "",
"ReportType": "",
"ReportTitles": "",
"ReportDate": "",
"UpdatedDateUTC": "",
"Attributes": "",
"Rows__RowType": "",
"Rows__Cells": "",
"Rows__Cells____Value": "",
"Rows__Cells____Attributes": "",
"Rows__Title": "",
"Rows__Rows____RowType": "",
"Rows__Rows____Cells__Value": "6725.46",
"Rows__Rows____Cells__Attributes": "",
"Rows__Rows____Cells__Attributes____Value": "a76bacdc-a411-4924-9860-7c8b1bf36e5b",
"Rows__Rows____Cells__Attributes____Id": "account",
"Rows__Rows____Title": "",
"Rows__Rows____Rows": "",
"Rows__Rows": ""
},
{
"ReportID": "",
"ReportName": "",
"ReportType": "",
"ReportTitles": "",
"ReportDate": "",
"UpdatedDateUTC": "",
"Attributes": "",
"Rows__RowType": "",
"Rows__Cells": "",
"Rows__Cells____Value": "",
"Rows__Cells____Attributes": "",
"Rows__Title": "",
"Rows__Rows____RowType": "Row",
"Rows__Rows____Cells__Value": "Essential Sauna Sales",
"Rows__Rows____Cells__Attributes": "",
"Rows__Rows____Cells__Attributes____Value": "b2544842-1e51-4c08-80bc-7077f01a8657",
"Rows__Rows____Cells__Attributes____Id": "account",
"Rows__Rows____Title": null,
"Rows__Rows____Rows": null,
"Rows__Rows": ""
},
{
"ReportID": "",
"ReportName": "",
"ReportType": "",
"ReportTitles": "",
"ReportDate": "",
"UpdatedDateUTC": "",
"Attributes": "",
"Rows__RowType": "",
"Rows__Cells": "",
"Rows__Cells____Value": "",
"Rows__Cells____Attributes": "",
"Rows__Title": "",
"Rows__Rows____RowType": "",
"Rows__Rows____Cells__Value": "21359.09",
"Rows__Rows____Cells__Attributes": "",
"Rows__Rows____Cells__Attributes____Value": "b2544842-1e51-4c08-80bc-7077f01a8657",
"Rows__Rows____Cells__Attributes____Id": "account",
"Rows__Rows____Title": "",
"Rows__Rows____Rows": "",
"Rows__Rows": ""
}]
答案 0 :(得分:0)
您可以将此json存储在变量中并使用类似
的内容JSON.parse(variable_name);
在此之后,它以数组的形式出现,您可以轻松地遍历它
希望这有帮助!