将传入的 JSON 字符串转换为具有 GroupBy 能力的 Javascript 数组
Convert incoming JSON string to Javascript array with ability to GroupBy
我有一个 API 其中 returns 一个 JSON,例如:
{
"CaseAssgnedDt": "2011-07-22T17:12:19",
"InquiryId": "08-CA-036441",
"DisputeUnitCity": "Cleveland",
"CaseClasification": "Unclassified",
"CaseClosedDt": null,
"Created": "2010-10-07T15:32:29",
"CaseDescription": null,
"LastUpd": "2019-10-20T17:17:14",
"LastUpdBy": "1-CGA",
"CaseName": "Sample test case name",
"CaseNumber": "08-CA-036441",
"CaseSource": "Visit",
"DisputeUnitState": "OH",
"CaseStatus": "Open",
"CaseSubType": "CA",
"CaseSubTypeCd": null,
"CaseType": "C",
"CaseLongName": "Long sample test case name",
"Num8a3Discriminatees": null,
"Num8b2Discriminatees": null,
"NumOfEmployees": 146,
"PostElectionSelfCertification": null,
"Potential10j": "N",
"RegionRecommendsPursuing10j": "N",
}
我正在使用 Fetch API.
从 Javascript 中的 API 检索它
一旦我检索到它,我希望将它转换成一个 Javascript 数组 Name/Value 对,匹配具体格式如下:
[
{fieldName:"CaseAssgnedDt", fieldValue: "2011-07-22T17:12:19"},
{fieldName: "InquiryId", fieldValue: "08-CA-036441"},
{fieldName: "DisputeUnitCity", fieldValue: "Cleveland"},
{fieldName: "CaseClasification", fieldValue: "Unclassified"},
{fieldName: "CaseClosedDt", fieldValue: null},
{fieldName: "Created", fieldValue: "2010-10-07T15:32:29"},
{fieldName: "CaseDescription", fieldValue: null},
{fieldName: "LastUpd", fieldValue: "2019-10-20T17:17:14"},
{fieldName: "LastUpdBy", fieldValue: "1-CGA"},
{fieldName: "CaseName", fieldValue: "Sample test case name"},
{fieldName: "CaseNumber", fieldValue: "08-CA-036441"},
{fieldName: "CaseSource", fieldValue: "Visit"},
{fieldName: "DisputeUnitState", fieldValue: "OH"},
{fieldName: "CaseStatus", fieldValue: "Open"},
{fieldName: "CaseSubType", fieldValue: "CA"},
{fieldName: "CaseSubTypeCd", fieldValue: null},
{fieldName: "CaseType", fieldValue: "C"},
{fieldName: "CaseLongName", fieldValue: "Long sample test case name"},
{fieldName: "Num8a3Discriminatees", fieldValue: null},
{fieldName: "Num8b2Discriminatees", fieldValue: null},
{fieldName: "NumOfEmployees", fieldValue: 146},
{fieldName: "PostElectionSelfCertification", fieldValue: null},
{fieldName: "Potential10j", fieldValue: "N"},
{fieldName: "RegionRecommendsPursuing10j", fieldValue: "N"}
]
所以基本上,我想要相当于:
const caseData = [
{fieldName:"CaseAssgnedDt", fieldValue: "2011-07-22T17:12:19"},
{fieldName: "InquiryId", fieldValue: "08-CA-036441"},
{fieldName: "DisputeUnitCity", fieldValue: "Cleveland"},
{fieldName: "CaseClasification", fieldValue: "Unclassified"},
{fieldName: "CaseClosedDt", fieldValue: null},
{fieldName: "Created", fieldValue: "2010-10-07T15:32:29"},
{fieldName: "CaseDescription", fieldValue: null},
{fieldName: "LastUpd", fieldValue: "2019-10-20T17:17:14"},
{fieldName: "LastUpdBy", fieldValue: "1-CGA"},
{fieldName: "CaseName", fieldValue: "Sample test case name"},
{fieldName: "CaseNumber", fieldValue: "08-CA-036441"},
{fieldName: "CaseSource", fieldValue: "Visit"},
{fieldName: "DisputeUnitState", fieldValue: "OH"},
{fieldName: "CaseStatus", fieldValue: "Open"},
{fieldName: "CaseSubType", fieldValue: "CA"},
{fieldName: "CaseSubTypeCd", fieldValue: null},
{fieldName: "CaseType", fieldValue: "C"},
{fieldName: "CaseLongName", fieldValue: "Long sample test case name"},
{fieldName: "Num8a3Discriminatees", fieldValue: null},
{fieldName: "Num8b2Discriminatees", fieldValue: null},
{fieldName: "NumOfEmployees", fieldValue: 146},
{fieldName: "PostElectionSelfCertification", fieldValue: null},
{fieldName: "Potential10j", fieldValue: "N"},
{fieldName: "RegionRecommendsPursuing10j", fieldValue: "N"}
]
除了不对该数组进行编码,我想从检索到的 JSON 中动态构建它。
最终,这就是以一种格式获取我的数据,以便我可以实现类似于 https://docs.microsoft.com/en-us/office/dev/add-ins/concepts/correlated-objects-pattern 中引用的代码,在“使用相关对象模式处理文档中的对象”部分下.
请提供代码来说明或演示您的解决方案。
谢谢。
您可以在 Object.entries
上使用 map
let x = {
"CaseAssgnedDt": "2011-07-22T17:12:19",
"InquiryId": "08-CA-036441",
"DisputeUnitCity": "Cleveland",
"CaseClasification": "Unclassified",
"CaseClosedDt": null,
"Created": "2010-10-07T15:32:29",
"CaseDescription": null,
"LastUpd": "2019-10-20T17:17:14",
"LastUpdBy": "1-CGA",
"CaseName": "Sample test case name",
"CaseNumber": "08-CA-036441",
"CaseSource": "Visit",
"DisputeUnitState": "OH",
"CaseStatus": "Open",
"CaseSubType": "CA",
"CaseSubTypeCd": null,
"CaseType": "C",
"CaseLongName": "Long sample test case name",
"Num8a3Discriminatees": null,
"Num8b2Discriminatees": null,
"NumOfEmployees": 146,
"PostElectionSelfCertification": null,
"Potential10j": "N",
"RegionRecommendsPursuing10j": "N",
}
let res = Object.entries(x).map(([k,v])=>{
return {fieldName:k, value: v}
})
console.log(res)
使用@cmgchess 提供的信息,我能够实现如下解决方案:
async function loadCaseData(caseNum) {
Word.run(async function (context) {
const url = `[URL to my API function]`;
fetch(url)
.then(resp => resp.json())
.then(resp => {
console.log('Server Response : ' + JSON.stringify(resp));
let fieldList = Object.entries(resp).map(([k, v]) => {
return { fieldName: k, fieldValue: v }
})
return fieldList;
})
return context.sync();
});
}
这将从我的 API 函数中检索数据,并根据需要将其存储在数组中。
我有一个 API 其中 returns 一个 JSON,例如:
{
"CaseAssgnedDt": "2011-07-22T17:12:19",
"InquiryId": "08-CA-036441",
"DisputeUnitCity": "Cleveland",
"CaseClasification": "Unclassified",
"CaseClosedDt": null,
"Created": "2010-10-07T15:32:29",
"CaseDescription": null,
"LastUpd": "2019-10-20T17:17:14",
"LastUpdBy": "1-CGA",
"CaseName": "Sample test case name",
"CaseNumber": "08-CA-036441",
"CaseSource": "Visit",
"DisputeUnitState": "OH",
"CaseStatus": "Open",
"CaseSubType": "CA",
"CaseSubTypeCd": null,
"CaseType": "C",
"CaseLongName": "Long sample test case name",
"Num8a3Discriminatees": null,
"Num8b2Discriminatees": null,
"NumOfEmployees": 146,
"PostElectionSelfCertification": null,
"Potential10j": "N",
"RegionRecommendsPursuing10j": "N",
}
我正在使用 Fetch API.
从 Javascript 中的 API 检索它一旦我检索到它,我希望将它转换成一个 Javascript 数组 Name/Value 对,匹配具体格式如下:
[
{fieldName:"CaseAssgnedDt", fieldValue: "2011-07-22T17:12:19"},
{fieldName: "InquiryId", fieldValue: "08-CA-036441"},
{fieldName: "DisputeUnitCity", fieldValue: "Cleveland"},
{fieldName: "CaseClasification", fieldValue: "Unclassified"},
{fieldName: "CaseClosedDt", fieldValue: null},
{fieldName: "Created", fieldValue: "2010-10-07T15:32:29"},
{fieldName: "CaseDescription", fieldValue: null},
{fieldName: "LastUpd", fieldValue: "2019-10-20T17:17:14"},
{fieldName: "LastUpdBy", fieldValue: "1-CGA"},
{fieldName: "CaseName", fieldValue: "Sample test case name"},
{fieldName: "CaseNumber", fieldValue: "08-CA-036441"},
{fieldName: "CaseSource", fieldValue: "Visit"},
{fieldName: "DisputeUnitState", fieldValue: "OH"},
{fieldName: "CaseStatus", fieldValue: "Open"},
{fieldName: "CaseSubType", fieldValue: "CA"},
{fieldName: "CaseSubTypeCd", fieldValue: null},
{fieldName: "CaseType", fieldValue: "C"},
{fieldName: "CaseLongName", fieldValue: "Long sample test case name"},
{fieldName: "Num8a3Discriminatees", fieldValue: null},
{fieldName: "Num8b2Discriminatees", fieldValue: null},
{fieldName: "NumOfEmployees", fieldValue: 146},
{fieldName: "PostElectionSelfCertification", fieldValue: null},
{fieldName: "Potential10j", fieldValue: "N"},
{fieldName: "RegionRecommendsPursuing10j", fieldValue: "N"}
]
所以基本上,我想要相当于:
const caseData = [
{fieldName:"CaseAssgnedDt", fieldValue: "2011-07-22T17:12:19"},
{fieldName: "InquiryId", fieldValue: "08-CA-036441"},
{fieldName: "DisputeUnitCity", fieldValue: "Cleveland"},
{fieldName: "CaseClasification", fieldValue: "Unclassified"},
{fieldName: "CaseClosedDt", fieldValue: null},
{fieldName: "Created", fieldValue: "2010-10-07T15:32:29"},
{fieldName: "CaseDescription", fieldValue: null},
{fieldName: "LastUpd", fieldValue: "2019-10-20T17:17:14"},
{fieldName: "LastUpdBy", fieldValue: "1-CGA"},
{fieldName: "CaseName", fieldValue: "Sample test case name"},
{fieldName: "CaseNumber", fieldValue: "08-CA-036441"},
{fieldName: "CaseSource", fieldValue: "Visit"},
{fieldName: "DisputeUnitState", fieldValue: "OH"},
{fieldName: "CaseStatus", fieldValue: "Open"},
{fieldName: "CaseSubType", fieldValue: "CA"},
{fieldName: "CaseSubTypeCd", fieldValue: null},
{fieldName: "CaseType", fieldValue: "C"},
{fieldName: "CaseLongName", fieldValue: "Long sample test case name"},
{fieldName: "Num8a3Discriminatees", fieldValue: null},
{fieldName: "Num8b2Discriminatees", fieldValue: null},
{fieldName: "NumOfEmployees", fieldValue: 146},
{fieldName: "PostElectionSelfCertification", fieldValue: null},
{fieldName: "Potential10j", fieldValue: "N"},
{fieldName: "RegionRecommendsPursuing10j", fieldValue: "N"}
]
除了不对该数组进行编码,我想从检索到的 JSON 中动态构建它。
最终,这就是以一种格式获取我的数据,以便我可以实现类似于 https://docs.microsoft.com/en-us/office/dev/add-ins/concepts/correlated-objects-pattern 中引用的代码,在“使用相关对象模式处理文档中的对象”部分下.
请提供代码来说明或演示您的解决方案。
谢谢。
您可以在 Object.entries
map
let x = {
"CaseAssgnedDt": "2011-07-22T17:12:19",
"InquiryId": "08-CA-036441",
"DisputeUnitCity": "Cleveland",
"CaseClasification": "Unclassified",
"CaseClosedDt": null,
"Created": "2010-10-07T15:32:29",
"CaseDescription": null,
"LastUpd": "2019-10-20T17:17:14",
"LastUpdBy": "1-CGA",
"CaseName": "Sample test case name",
"CaseNumber": "08-CA-036441",
"CaseSource": "Visit",
"DisputeUnitState": "OH",
"CaseStatus": "Open",
"CaseSubType": "CA",
"CaseSubTypeCd": null,
"CaseType": "C",
"CaseLongName": "Long sample test case name",
"Num8a3Discriminatees": null,
"Num8b2Discriminatees": null,
"NumOfEmployees": 146,
"PostElectionSelfCertification": null,
"Potential10j": "N",
"RegionRecommendsPursuing10j": "N",
}
let res = Object.entries(x).map(([k,v])=>{
return {fieldName:k, value: v}
})
console.log(res)
使用@cmgchess 提供的信息,我能够实现如下解决方案:
async function loadCaseData(caseNum) {
Word.run(async function (context) {
const url = `[URL to my API function]`;
fetch(url)
.then(resp => resp.json())
.then(resp => {
console.log('Server Response : ' + JSON.stringify(resp));
let fieldList = Object.entries(resp).map(([k, v]) => {
return { fieldName: k, fieldValue: v }
})
return fieldList;
})
return context.sync();
});
}
这将从我的 API 函数中检索数据,并根据需要将其存储在数组中。