多SharePoint列表API调用返回无效JSON问题解决
问题场景
要从4个SharePoint文档列表中筛选带有特殊标记的文档,合并结果为单个数组后返回JSON给JavaScript做网页渲染。但实际调用时,返回结果为ArrayArrayArray{"meeting date1":["filename1", "filepath1"], "meeting date2":["filename2", "filepath2"]},前端无法识别为合法JSON。
报错信息
前端控制台抛出:
SyntaxError: Unexpected token 'A', " ArrayArray"... is not valid JSON
相关代码
主处理函数
public function getSpecalMarkedDocuments() { $fullDocumentResults = []; $documentLists = ['List1', 'List2', 'List3', 'List4']; foreach ($documentLists as $list) { // 此处为SharePoint API请求URL的格式化代码,逻辑正常 $results = publicCallAPI($url); if (is_null($results)) { // 此处echo会输出三个空列表的名称 } else { foreach ($results as $items) { $meetingDate = $items['Meeting']; $filename = $items['File']['Name']; $filepath = $items['PubLink']; $fullDocumentResults[$meetingDate] = [$filename, $filepath]; } } } return new JsonResponse($fullDocumentResults); }
API调用函数
$results = curl_exec($ch); $resultsJSON = json_decode($results, true); curl_close($ch); $data = $resultsJSON['d']['results']; return $data;
排查过程
- 用
var_dump($results)查看返回数据,发现有效结果中混杂多个"Array null"信息 - 尝试将空结果手动设为
[],仍会传入"Array"字符串 - 最终定位问题根源:另外3个列表未添加「特殊标记」字段,导致SharePoint API返回的结构不符合预期,publicCallAPI函数无法正确解析出数组类型的结果,进而在主函数处理时输出了异常字符串。
解决方法
联系SharePoint维护人员为缺失字段的3个列表添加「特殊标记」字段后,函数恢复正常,返回合法的JSON数据。
内容的提问来源于stack exchange,提问作者Pencilneck
相关产品推荐
相关产品推荐

