【问题标题】:How to manually create specific json [duplicate]如何手动创建特定的json [重复]
【发布时间】:2017-11-30 13:35:31
【问题描述】:

如何从 csv 文件中获取此 json。 csv 文件具有标题:

Description, BusinessSurname, IsCustomer, IsSupplier, AddressType, BusinessAddress, IsInternational

第一行:

Contact1, Contact1, True, True, Business, 123 Fake St, False

我需要将 csv 中的数据转换为确切的 json,但需要它循环以查找可能存在的任何其他行。

{
Description:'Contact1',
SurnameBusinessName:'Contact1',
IsCustomer:True,
IsSupplier:True,
Addresses:
[
{AddressType:'Business',Line1:'123 Fake St',IsInternational:False},
]
}

我不能让它工作。请帮忙。

########################################

我找到了这个问题的答案,在尝试了所有可能的解决方案后,我意识到我必须手动创建 json,因为没有 json 或数组可以完全满足我的需求。然而,我已经放弃了这个 API 端点,转而使用了一个更简单的没有嵌套的端点。由于 json 格式的严格性,这个 API 将接受我基本上将我的 csv 中的每个值分配给一个变量并手动创建所需的 json。虽然下面的代码是针对 Accounts 端点的,但我可以对任何端点使用完全相同的方法,因为我有点手动创建 json。我认为这是它唯一可以工作的方式,因为有些字段是 AccountName 之类的字符串,有些是 AcountType 之类的整数。

$file = 'acc.csv';
    $mode = 'r';
    $handle = fopen($file, $mode);
    while(($csv = fgetcsv($handle)) !==FALSE){
        foreach($csv as $row => $value){
            $data = $row.$value;
            switch ($row){
                case '0':
                    $accounttype = $value;
                    break;
                case '1':
                    $accountname = $value;
                    break;
                case '2':
                    $hint = $value;
                    break;
                case '3':
                    $status = $value;
                    break;
                case '4':
                    $sortorder = $value;
                    break;
                case '5':
                    $accountcode = $value;
                    break;
                case '6':
                    $parentaccountcatid = $value;

                    $json = "
                    {
      AccountType:" . $accounttype . ",
      AccountName:'" . $accountname . "',
      Hint:'" . $hint . "',
      Status:" . $status . ",
      SortOrder:" . $sortorder . ",
      AccountCode:'" . $accountcode . "',
      ParentAccountingCategoryID:'" . $parentaccountcatid . "'
     }"; 
    //echo $json; 

【问题讨论】:

标签: php arrays json csv fgetcsv


【解决方案1】:

脚本

[akshay@localhost tmp]$ cat test.php
<?php

function csv_to_array($filename='', $delimiter=',')
{
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

    $header = NULL;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== FALSE)
    {
        while (($row = fgetcsv($handle, 0, $delimiter)) !== FALSE)
        {

            if(!$header)
            {
               // if you want keys with space char then
               // $header = $row; 
               // I prefer trimming
               $header = array_map('trim',$row);
            }
            else
            {
                if(count($header)!=count($row)){ continue; }

                $data[] = array_combine($header, $row);
            }
        }
        fclose($handle);
    }
    return $data;
}

print_r(json_encode(csv_to_array("/tmp/test.csv"),JSON_PRETTY_PRINT));

?>

示例 csv 文件

[akshay@localhost tmp]$ cat test.csv
Description, BusinessSurname, IsCustomer, IsSupplier, AddressType, BusinessAddress, IsInternational
Contact1, Contact1, True, True, Business, 123 Fake St, False

执行

[akshay@localhost tmp]$ php test.php
[
    {
        "Description": "Contact1",
        "BusinessSurname": " Contact1",
        "IsCustomer": " True",
        "IsSupplier": " True",
        "AddressType": " Business",
        "BusinessAddress": " 123 Fake St",
        "IsInternational": " False"
    }
]

【讨论】:

  • 我已经到了那个地步,但我遇到的问题是嵌套地址,这是我的问题的重点。无论如何感谢您的回复。
猜你喜欢
  • 1970-01-01
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2016-07-25
  • 1970-01-01
相关资源
最近更新 更多