使用表单创建JSON [英] Using a form to create JSON

查看:117
本文介绍了使用表单创建JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个API,期望使用以下格式的JSON字符串:

I have an API which expects a JSON string in the following format:

{
  "Title": "Mr",
  "Forenames": "Steve",
  "Surname": "Williams",
  "CountryOfBirth": 1,
  "EmailAddress": "john.doe@email.com",
  "EmailType": "Personal",
  "BirthDate": "\/Date(632880000000)\/",
  "Suffix": null,
  "NationalInsuranceNumber": null,
  "PrimaryAddress": {
    "Address1": "Flat 1",
    "Address2": "Oxford Street",
    "City": "London",
    "County": "London",
    "Postcode": "L12456",
    "Country": 1
  },
  "AdditionalAddresses": [
    {
      "Address1": null,
      "Address2": null,
      "City": null,
      "County": null,
      "Postcode": null,
      "Country": 0,
      "AddressType": 0
    }
  ],
  "PrimaryTelephone": {
    "Number": "123456789",
    "DialingCode": 1,
    "TelephoneType": 1
  },
  "AdditionalTelephone": [
    {
      "Number": null,
      "DialingCode": 0,
      "TelephoneType": 0
    }
  ],
  "BankAccount": {
    "AccountName": "John Doe Account",
    "AccountNumber": "123456789",
    "SortCode": "123456"
  },
  "PrimaryCitizenship": {
    "CountryOfResidency": 1,
    "TaxIdentificationNumber": "AB12CD34EF56"
  },
  "AdditionalCitizenship": [
    {
      "CountryOfResidency": 0,
      "TaxIdentificationNumber": null
    }
  ],
  "ExternalCustomerId": "151",
  "ExternalPlanId": "151",
  "PlanType": 10
}

如您所见,有一些内部嵌套元素,其中每个值本身可以是一个数组,例如AdditionalTelephone

As you can see there are some inner nested elements where each value can itself be an array, such as AdditionalTelephone

我使用以下方法在PHP中重新创建了该JSON字符串:

I re-created this JSON string in PHP with the following:

<?php

$dataArray = array(
    "Title" => "Mr",
    "Forename" => "Jesse",
    "Surname" => "Orange",
    "CountryOfBirth" => 1,
    "EmailAddress" => "email@gmail.com",
    "EmailType" => "Personal",
    "BirthDate" => "\/Date(632880000000)\/",
    "Suffix" => null,

    "PrimaryAddress" => array(
        "Address1" => "Flat 1",
        "Address2" => "Oxford Street",
        "City" => "London",
        "County" => "London",
        "Postcode" => "L12456",
        "Country" => 1
    ),

    "AdditionalAddresses" => array(

        array(
            "Address1" => null,
            "Address2" => null,
            "City" => null,
            "County" => null,
            "Postcode" => null,
            "Country" => 0,
            "AddressType" => 0
        )

    ),

    "PrimaryTelephone" => array(
        "Number" => "123456789",
        "DialingCode" => 1,
        "TelephoneType" => 1
    ),

    "AdditionalTelephone" => array(

        array(
            "Number" => "123456789",
            "DialingCode" => 1,
            "TelephoneType" => 1
        )

    ),

    "BankAccount" => array(
        "AccountName" => "John Doe Account",
        "AccountNumber" => "123456789",
        "SortCode" => "123456"
    ),

    "PrimaryCitizenship" => array(
        "CountryOfResidency" => 1,
        "TaxIdentificationNumber" => "AB12CD34EF56"
    ),

    "AdditionalCitizenship" => array(

        array(
            "CountryOfResidency" => 0,
            "TaxIdentificationNumber" => null
        )
    ),
    "ExternalCustomerId" => "151",
    "ExternalPlanId" => "151",
    "PlanType" => 10
);
    header("Content-type:application/json");
    $jsonDataArray = json_encode($dataArray, JSON_PRETTY_PRINT);
    echo $jsonDataArray;
    die();
?>

这将打印出同样的东西.

This prints the same kind of thing.

现在,我有一个用户填写的表格,数据已过帐,所以我想在我创建的数组中使用这些值.

Now, I have a form that a user fills in and the data is POSTed, so I wanted to use these values in the array I had created.

例如:

$dataArray = array(
  "Title" => POST['Title'],
   etc
)

问题是:当值是内部数组的列表(例如AdditionalCitizenship的情况)时,我可以将输入名称用作数组吗?

The question is: when a value is a list of inner arrays such as the case with AdditionalCitizenship can I use names of inputs as arrays?

<input type="text" name=AdditionalCitizenship[Array1]['CountryOfResidency']>
<input type="text" name=AdditionalCitizenship[Array2]['CountryOfResidency']>

推荐答案

是的,除了您会使用

<input type="text" name="AdditionalCitizenship[0][CountryOfResidency]">
<input type="text" name="AdditionalCitizenship[0][TaxIdentificationNumber]">
<input type="text" name="AdditionalCitizenship[1][CountryOfResidency]">
<input type="text" name="AdditionalCitizenship[1][TaxIdentificationNumber]">

在您的$_POST数组中,它看起来像

In your $_POST array this will look like

"AdditionalCitizenship" => array(
    array(
        "CountryOfResidency" => 0,
        "TaxIdentificationNumber" => null
    ),
    array(
        "CountryOfResidency" => 0,
        "TaxIdentificationNumber" => null
    )
)

这篇关于使用表单创建JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆