【问题标题】:Parsing Duplicatable Form's JSON data to PHP for Mail将 Duplicatable Form 的 JSON 数据解析为 PHP for Mail
【发布时间】:2013-01-21 10:22:30
【问题描述】:

我有一个包含可重复字段的表单,它会生成以下 JSON 数据:

{
    "mainmember": [
        {
            "name": "testtest",
            "surname": "test",
            "id": "8509295266086",
            "age": "27",
            "gender": "Male",
            "townofbirth": "george",
            "email": "test@test.com",
            "contact": "0112121211",
            "passport": "1111111111111",
            "postal": "grjiubrwg",
            "postal_code": "0010",
            "residential": "tytyytyttyytyt",
            "residential_code": "4422"
        }
    ],
    "dependant1": [
        {
            "name": "testDUP",
            "surname": "testDUP",
            "id": "4802235040081",
            "age": "64",
            "gender": "Male",
            "townofbirth": "tehjte",
            "cell": "4774811845",
            "email": "testDUP",
            "passport": "8202255133088",
            "relationship": "spouse"
        }
    ],
    "dependant2": [
        {
            "name": "testDUP2",
            "surname": "testDUP2",
            "id": "1111111111111",
            "age": "45",
            "gender": "F",
            "townofbirth": "knysnasw",
            "cell": "0000000000",
            "email": "testDUP2",
            "passport": "2222222222222",
            "relationship": "inlaw"
        }
    ]
}

现在可复制字段是依赖项,在这个 JSON 中有 2 个依赖项,但在重复时它增加到“dependant3、dependant4、dependant5”。

目前,当我提交并发送到 PHP 时,我可以计算依赖项:

    <?php
$json = $_POST['parameters'];
$json_string = stripslashes($json);
$data = json_decode($json_string, true);

$datasetCount = count($data); // returns count of array items of any array
echo "<h1>There are $datasetCount Dependants</h1>";

$i = 0;
foreach ($data as $each_dep) {
    $i++;
    echo "<h2>Dependant $i</h2>";
    while (list($key, $value) = each ($each_dep)) {

        echo "$key: $value<br />";

    }

}

?>

以及我正在使用的 jQuery 发送函数:

jQuery('#submit').click(function(){
    jQuery('div[class*="mainmember"]').each(function(k, v){
        mainmember = {};
        mainmember['id'] = ''; 
        $(v).find('.id').each(function(){
          mainmember['id'] += $(this).val(); 
        });
        mainmember['age'] = ''; 
        $(v).find('.age').each(function(){
          mainmember['age'] += $(this).val(); 
        });
        mainmember['gender'] = $(v).find('.gender').val();
        result['mainmember'] = [mainmember];

    });
    jQuery('div[class*="dependant"]').each(function(k, v){
        dep_counter++
        dependants = {};
        result['dependant'+dep_counter] = [dependants];
        dependants['id'] = ''; 
        $(v).find('.id').each(function(){
          dependants['id'] += $(this).val(); 
        });
        dependants['age'] = ''; 
        $(v).find('.age').each(function(){
          dependants['age'] += $(this).val(); 
        });
        dependants['gender'] = $(v).find('.gender').val();
    });
    var jsonData = JSON.stringify(result);

    console.log(jsonData);
});

我只是无法获取其他数据,例如姓名、姓氏等。 我需要像这样将其解析为电子邮件:

主要成员: 详情

从属 2: 详情

从属 3: 详情

非常感谢任何帮助。

【问题讨论】:

  • 你想做什么?你能举一个包含重复的例子吗?
  • 试图清理它更多..希望它有所帮助
  • 在您的表格中,mainmember 有多少个?一个还是多个?
  • 为什么不将依赖项存储在数组中?为什么要将 mainmember 存储在数组中?它不总是包含一个元素吗?我建议像"mainmember": { /* main member data */ }, "dependants": [{ /* dep1 data */ }, { /* dep2 data */ }] 等

标签: php jquery json


【解决方案1】:

您的 json 结构非常痛苦,您似乎正在创建单个对象的数组。 相反,您应该有对象数组,或键引用的单个对象。

对于你的结构来说,我认为你应该有一个对象数组作为依赖对象,一个对象作为主要成员,如下所示:

{
    "mainmember": {
        "name": "test2",
        "id": "1111111111111",
        "age": "45",
        "gender": "M",
        "townofbirth": "knysna",
        "email": "tjyrrtjhe",
        "contact": "1111111111",
        "passport": "5555555555555",
        "postal": "yrtjyt",
        "postal_code": "1101",
        "residential": "tkyutk",
        "residential_code": "5555"
    },
    "dependants": [
        {
            "name": "dtjtet",
            "surname": "grwwr",
            "id": "1111222222222",
            "age": "48",
            "gender": "F",
            "townofbirth": "knmysn",
            "cell": "0045128588",
            "email": "fae@gmail.COM",
            "passport": "1212112111111",
            "relationship": "spouse"
        },
        {
            "name": "dtjtet2",
            "surname": "grwwr2",
            "id": "11112222222222",
            "age": "44",
            "gender": "M",
            "townofbirth": "knmysn",
            "cell": "0045128588",
            "email": "fae@gmail.COM",
            "passport": "1212112111111",
            "relationship": "spouse"
        }
    ]
}

这样你就有一个“主成员”对象和一个依赖数组。

然后使用下面的 php 代码,您可以打印它的详细信息:

<?php
function printMember($member) {
    foreach($member as $key=>$value) {
        echo "$key : $value <br />";
    }
}


$json = $_POST['parameters'];
$json_string = stripslashes($json);
$data = json_decode($json_string, true);

$depCount = count($data["dependants"]);
echo "<h1>There are $depCount Dependants</h1>";

echo "<h2>Main member data:</h2>";
printMember($data["mainmember"]);

foreach ($data["dependants"] as $index => $dependant) {

    echo "<h2>Dependant $index</h2>";
    printMember($dependant);

}

要在 jquery 代码中创建正确的 json 结构,如下所示:

jQuery('#submit').click(function(){
    jQuery('div[class*="mainmember"]').each(function(k, v){
        mainmember = {};
        mainmember['id'] = ''; 
        $(v).find('.id').each(function(){
          mainmember['id'] += $(this).val(); 
        });
        mainmember['age'] = ''; 
        $(v).find('.age').each(function(){
          mainmember['age'] += $(this).val(); 
        });
        mainmember['gender'] = $(v).find('.gender').val();
        //This is changed: Remove the [] from around the mainmember, 
        //should be a single object not an array
        result['mainmember'] = mainmember;

    });

    //create the array of dependants
    result['dependants'] = [];
    jQuery('div[class*="dependant"]').each(function(k, v){
        dependant = {};
        dependant['id'] = ''; 
        $(v).find('.id').each(function(){
          dependant['id'] += $(this).val(); 
        });
        dependant['age'] = ''; 
        $(v).find('.age').each(function(){
          dependant['age'] += $(this).val(); 
        });
        dependant['gender'] = $(v).find('.gender').val();

        //add the dependant to the array
        result['dependants'].push(dependant);
    });
    var jsonData = JSON.stringify(result);

    console.log(jsonData);
});

这是未经测试的,但应该可以帮助您继续。您的问题是因为您使用的是单个对象的数组而不是对象本身。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 2014-05-29
    • 2014-05-19
    • 2015-06-30
    相关资源
    最近更新 更多