如何手动创建特定的JSON [英] How to manually create specific json

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

问题描述

如何从csv文件获取此json. csv文件具有以下标头:

how to get this json from a csv file. The csv file has the headers:

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

第一行:

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

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

I need that data in the csv to convert to that exact json, but need it to loop for any other rows that may exist.

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

我无法使其正常工作.请帮忙.

I cant make it work. please help.

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

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

I found the answer to this, after trying every possible solution I realised I had to manually create the json as no json or array would give me exactly what I wanted. However I had given up on this API endpoint and went to a simpler one without nesting. Due to the strictness of the json format this API will accept I basically assigned each value from my csv to a variable and manually created the json needed. Although this code below is for the Accounts endpoint, I can use the exact same method for any endpoint because I am kind of manually creating the json. And its the only way it can work I think as some fields are strings like AccountName, some integers like 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; 

推荐答案

脚本

[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"
    }
]

这篇关于如何手动创建特定的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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