SharePoint API 调用多个列表以返回一个数组

问题描述 投票:0回答:1

我有四个 SharePoint 文档列表,我需要它们来查找任何专门标记的文档。我想将结果合并到一个数组中,并将 JSON 返回到 javascript 文件以格式化数据以添加到网页。我知道此时其中三个列表没有标记任何文档,因此它们的结果将为空。当我调用该函数时,我得到“ArrayArrayArray{来自包含项目的一个列表的会议文档列表结果}。Javascript 无法将其识别为 JSON。看来我在开始时创建的完整数组并未合并最后的数组但试图发送四个。

public function getSpecalMarkedDocuments() {
    $fullDocumentResults = [];
    $documentLists = ['List1', 'List2', 'List3', 'List4'];

    foreach ($documentLists as $list) {
        Code here that formats the Sharepoint API $url call which works fine
        $results = publicCallAPI($url);

        if (is_null($results) {
        // I have added echo $documentLists here and it will return the three empty list names.
        } else {
        foreach ($results as $items) {
           $meetingDate = $items['Meeting'];
           $filename = $items['File']['Name'];
           $filepath = $items['PubLink'];

           $fullDocumentResults[$meetingDate] = [$filename, $filepath];
        }
    }
    return new JsonResponse($fullDocumentResults);
}

我得到的不是单个 JSON 列表,而是:

ArrayArrayArray{"会议日期1":["文件名1","文件路径1"],"会议日期2":["文件名2","文件路径2"]}

console.log 说: SyntaxError:意外的标记“A”,“ArrayArray”...不是有效的 JSON

var_dump($results) 是:

    array (size=4)
        0 => 
            array (size=4)
              'File' => 
                array (size=2)
                  '__metadata' => 
                    array (size=3)
                      ...
                      'Name' => string 'Name of document here' (length=51)
                      'Meeting' => string '2024-04-04T07:00:00Z' (length=20)
                      'PubLink' => string 'link is here' (length=121)

      1 => 
          array (size=4)
              'File' => 
                array (size=2)
                  '__metadata' => 
                    array (size=3)
                      ...
                      'Name' => string 'document name here' (length=52)
                      'Meeting' => string '2024-04-11T07:00:00Z' (length=20)
         
                      'PubLink' => string 'Link is here' (length=121)
 
      2 => 
            array (size=4)
              'File' => 
                array (size=2)
              '__metadata' => 
                array (size=3)
                  ...
                  'Name' => string 'Document name here' (length=36)
                  'Meeting' => string '2024-04-19T07:00:00Z' (length=20)
                  'PubLink' => string 'Link to document' (length=121)
 
      3 => 
        array (size=4)
              'File' => 
                array (size=2)
              '__metadata' => 
                array (size=3)
                  ...
                  'Name' => string 'Document name here' (length=52)
                  'Meeting' => string '2024-04-11T07:00:00Z' (length=20)
                  'PubLink' => string 'link to document' (length=121)

    Array
        /Controller/SharepointGetDocs.php:799:null
    Array
        /Controller/SharepointGetDocs.php:799:null
    Array
        /Controller/SharepointGetDocs.php:799:null

var_dump($fullDocumentResults) 是:

 ArrayArrayArray<pre class='xdebug-var-dump' dir='ltr'>
<small>/Controller/SharepointGetDocs.php:779:</small>
<b>array</b> <i>(size=3)</i>
    '2024-04-04T07:00:00Z': 
        <b>array</b> <i>(size=2)</i>
            0 <'document name'>
            1 <'link to document'>
    '2024-04-11T07:00:00Z': 
        <b>array</b> <i>(size=2)</i>
            0 <'document name'
            1 <'link to document'>
    '2024-04-19T07:00:00Z':
        <b>array</b> <i>(size=2)</i>
            0 <'document name'>
            1 <'link to document'>
php arrays json sharepoint
1个回答
0
投票
public function getSpecalMarkedDocuments() {
$fullDocumentResults = []; // Initialize an empty array to hold combined results

$documentLists = ['List1', 'List2', 'List3', 'List4'];

foreach ($documentLists as $list) {
    // Code here that formats the Sharepoint API $url call which works fine
    $results = publicCallAPI($url);

    if (!is_null($results)) {
        foreach ($results as $items) {
           $meetingDate = $items['Meeting'];
           $filename = $items['File']['Name'];
           $filepath = $items['PubLink'];

           // Add each document to the combined results array
           $fullDocumentResults[] = ['meetingDate' => $meetingDate, 'filename' => $filename, 'filepath' => $filepath];
        }
    }
}

// Return combined results as JSON response
return new JsonResponse($fullDocumentResults);

}

© www.soinside.com 2019 - 2024. All rights reserved.