1

我正在尝试解析 Jotform API 返回的数据。

我成功地回显了数据,但我也得到了不必要的额外文本行。

我的 PHP 代码是:

包括“JotForm.php”;

$jotformAPI = new JotForm("myapikey");

$submissions = $jotformAPI->getFormSubmissions("myformid");
var_dump($submissions );

foreach ($submissions as $data) {
    $detail = $jotformAPI->getSubmission($data['id']);

    foreach ($detail as $d) {
        echo $d[1]['answer']['first'] . '<br>';
    }
}

var_dump($submissions) 的结果;

{
"responseCode": 200,
"message": "success",
"content": [{
    "id": "237955080346633702",
    "form_id": "31751954731962",
    "ip": "123.123.123.123",
    "created_at": "2013-06-25 03:38:00",
    "updated_at": "2013-06-27 04:58:00",
    "status": "ACTIVE",
    "new": "1",
    "answers": {
        "1": {
            "text": "Name",
            "type":"control_fullname",
            "answer": {
                "first": "LeBron",
                "last": "James"
            },
            "prettyFormat": "LeBron James"
        },
        "2": {
            "text": "Your Message",
            "type": "control_textarea",
            "answer":"¡Ay, caramba!"
        }
}],

}

这是我得到的结果:

1
0
0
0
C



0
LeBron
4

1 回答 1

1

我不得不修复你无效的 json 字符串......

代码:(演示

$json = '{
"responseCode": 200,
"message": "success",
"content": [{
    "id": "237955080346633702",
    "form_id": "31751954731962",
    "ip": "123.123.123.123",
    "created_at": "2013-06-25 03:38:00",
    "updated_at": "2013-06-27 04:58:00",
    "status": "ACTIVE",
    "new": "1",
    "answers": {
        "1": {
            "text": "Name",
            "type":"control_fullname",
            "answer": {
                "first": "LeBron",
                "last": "James"
            },
            "prettyFormat": "LeBron James"
        },
        "2": {
            "text": "Your Message",
            "type": "control_textarea",
            "answer":"¡Ay, caramba!"
        }
}}]
}';
foreach (json_decode($json, true)['content'] as $set) {
    echo "{$set['answers'][1]['answer']['first']} {$set['answers'][1]['answer']['last']}\n";
}

输出:

LeBron James

我对 jot 函数的作用有点模糊,但也许它应该是这样的:

foreach ($submissions as $data) {
    $detail = $jotformAPI->getSubmission($data['id']);
    echo $detail['answers'][1]['answer']['first'] . '<br>';
}
于 2018-11-03T00:10:01.653 回答