converting fgetcsv response into specific json

馋奶兔 提交于 2019-12-01 15:31:15

I found the answer to this, 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; 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!