0

我目前正在使用 Adob​​e Echosign API 来格式化表格中的一些数据。输出如下所示:

{
  "agreementId": "",
  "events": [
    {
      "actingUserEmail": "",
      "actingUserIpAddress": "",
      "date": "date",
      "description": "",
      "participantEmail": "",
      "type": "",
      "versionId": ""
    }
  ],
  "locale": "",
  "modifiable": false,
  "name": "",
  "nextParticipantSetInfos": [
    {
      "nextParticipantSetMemberInfos": [
        {
          "email": "",
          "waitingSince": "date"
        }
      ]
    }
  ],
  "participantSetInfos": [
    {
      "participantSetId": "",
      "participantSetMemberInfos": [
        {
          "email": "",
          "participantId": ""
        }
      ],
      "roles": [
        ""
      ],
      "status": ""
    }
  ],
  "status": "",
  "vaultingEnabled": false
}

我正在遍历多个协议,这会将它们分别输出为一个单独的数组。

这可能是一个非常基本的问题,但是我将如何遍历每个数组并提取“participantEmail”、“name”和“status”值?

谢谢!

4

2 回答 2

0

假设您有一系列与您提供的格式的协议,您可以执行以下操作:

<?php

$json = '[{
  "agreementId": "",
  "events": [
    {
      "actingUserEmail": "",
      "actingUserIpAddress": "",
      "date": "date",
      "description": "",
      "participantEmail": "an email",
      "type": "",
      "versionId": ""
    }
  ],
  "locale": "",
  "modifiable": false,
  "name": "a name",
  "nextParticipantSetInfos": [
    {
      "nextParticipantSetMemberInfos": [
        {
          "email": "",
          "waitingSince": "date"
        }
      ]
    }
  ],
  "participantSetInfos": [
    {
      "participantSetId": "",
      "participantSetMemberInfos": [
        {
          "email": "",
          "participantId": ""
        }
      ],
      "roles": [
        ""
      ],
      "status": "a status"
    }
  ],
  "status": "",
  "vaultingEnabled": false
}]';

$parsed = json_decode($json, true);

$names = [];
foreach($parsed as $agreement) {

    $names[] = $agreement['name'];

    $emails = []; 
    foreach($agreement['events'] as $event) {
        $emails[] = $event['participantEmail'];
    }

    $status = []; 
    foreach($agreement['participantSetInfos'] as $participant) {
        $status[] = $participant['status'];
    }

}

var_dump($names);
var_dump($emails);
var_dump($status);

当然,您应该对空值等进行一些检查,但这只是为了给您一个想法。由于您没有阐明姓名、状态和电子邮件之间的关系,我只是将它们放在单独的数组中,但这并不是通过一些数组处理无法解决的。

希望这可以帮助!

于 2016-10-19T14:58:02.937 回答
0

根据您使用的编程语言,有很多方法可以处理这个问题。在 JS 中,您可以将此 JSON 数组转换为 JS 对象数组,然后您可以使用 JS 处理访问它们。

JS 的示例是:

var input = 'yourJSONstring';
var jsObject = JSON.parse(input);
for(var foo in jsObject){
	var name = foo.name;
    var participantEmail=  foo.events[0].participantEmail;
    var status = foo.participantEmail[0].status;
}

于 2016-10-18T13:38:20.387 回答