dongqufi82315 2015-07-21 18:28
浏览 36
已采纳

解析不一致的JSON数据:PHP

The application Pipedrive gives me inconsistent json data. For example, in some array elements, it gives me "formatted_value":"$3,500","weighted_value":2100,"formatted_weighted_value":"$2,100","rotten_time":null, while in others, it gives me, "formatted_value":"$2,950","rotten_time":null,"weighted_value":2950,"formatted_weighted_value":"$2,950". I would like the json data to be in the order of formatted_value,weighted_value,formatted_weighted_value,rotten_time in every array element, but that's not the case sadly.

Does anyone know of a way to check that the right data is written the right column based on column name and key name?

Below is my code to parse the json data:

function parseFunction($startPos) {
$url = 'urlToCallJsonData';
$ch = curl_init($url); //initialize connection with a URL


if(is_callable('curl_init'))
{
    echo "Enabled";
}
else
{
    echo "Not enabled";
}

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
$json_response = curl_exec($ch);
$info = curl_getinfo($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $status != 200 )
{
    die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($ch) . ", curl_errno " . curl_errno($ch));
}
curl_close($ch);

$response = json_decode($json_response, true);

$count = Count($response['data']);

for ($x=0; $x<$count; $x++)
{
    $currentRecord = $response['data'][$x];
}

//open writing to file
if($startPos == 0)
{
    $fp = fopen('cacheDeals.csv', 'w');
}
else
{
    $fp = fopen('cacheDeals.csv', 'a');
}

$test_array = $response['data'][0];//test_array = first row of data

// writes the headers to the csv file. 
if($startPos == 0)
{
    $keys = array_keys($test_array);
    fputcsv($fp, $keys);
}

$array_records = $response['data'];//all of incoming data
//write data to csv file
foreach ($array_records as $fields)
{
    fputcsv($fp, $fields);
}

//check to see if more data should be written
$more = $response[additional_data][pagination][more_items_in_collection];
$nextStart = $response[additional_data][pagination][next_start];
if($more =="true")
{
    downloadPipedriveDealsData($nextStart);
}

}//end of function

parseFunction(0);
  • 写回答

1条回答 默认 最新

  • doujiang1993 2015-07-21 19:57
    关注

    Sorry but I cant point this out in comments alone, some of this could be cleaner such as

        //open writing to file
        if($startPos == 0)
        {
            $fp = fopen('cacheDeals.csv', 'w');
        }
        else
        {
            $fp = fopen('cacheDeals.csv', 'a');
        }
    
        $test_array = $response['data'][0];//test_array = first row of data
    
        // writes the headers to the csv file. 
        if($startPos == 0)
        {
            $keys = array_keys($test_array);
            fputcsv($fp, $keys);
        }
    

    Could be simply this

      // writes the headers to the csv file. 
        if($startPos == 0){
            $fp = fopen('cacheDeals.csv', 'w');
            fputcsv($fp, array_keys($response['data'][0]));
        }else{
            $fp = fopen('cacheDeals.csv', 'a');
        }
    

    I don't see a purpose to this whole block at all

    $count = Count($response['data']);
    
    for ($x=0; $x<$count; $x++)
    {
        $currentRecord = $response['data'][$x];
    }
    

    This syntax is invalid

    $more = $response[additional_data][pagination][more_items_in_collection];
    $nextStart = $response[additional_data][pagination][next_start];
    

    And will issue a Notice ( undefined constant assuming '' ) etc. because there are no quotes around the string keys in the arrays. In the unlikly event that one of those keys is a constant, that's a whole other can of worms. Because you will never get your data out then. They should be done this way with ' or " 's around them. see also What does the PHP error message "Notice: Use of undefined constant" mean?

    $more = $response['additional_data']['pagination']['more_items_in_collection'];
    $nextStart = $response['additional_data']['pagination']['next_start'];
    

    Just saying.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来