解析不一致的JSON数据:PHP

时间:2015-07-21 18:28:45

标签: php json

Pipedrive应用程序为我提供了不一致的json数据。例如,在某些数组元素中,它会给我"formatted_value":"$3,500","weighted_value":2100,"formatted_weighted_value":"$2,100","rotten_time":null,而在其他数组元素中,它会给我"formatted_value":"$2,950","rotten_time":null,"weighted_value":2950,"formatted_weighted_value":"$2,950"。我希望json数据在每个数组元素中都是formatted_value,weighted_value,formatted_weighted_value,rotten_time的顺序,但遗憾的是并非如此。

是否有人知道如何根据列名和密钥名检查正确的数据是否写入正确的列?

下面是我解析json数据的代码:

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 个答案:

答案 0 :(得分:0)

很抱歉,但我不能仅仅在评论中指出这一点,其中一些可能更清晰,例如

    //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);
    }

可能只是这个

  // 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');
    }

我根本没有看到整个街区的目的

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

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

此语法无效

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

并且会发出一个通知(未定义的常量,假设'')等,因为数组中的字符串键周围没有引号。在不可思议的事件中,其中一个键是常量,这是另一种蠕虫。因为你永远不会得到你的数据。它们应该以{{1​​}}或'周围的方式完成。另见What does the PHP error message "Notice: Use of undefined constant" mean?

"

只是说。