php
  • json
  • csv
  • 2017-03-14 14 views 1 likes 
    1

    こんにちは私はjsonをcsvに変換することで小さな問題があります。私はこの[{"name":"Wayne","age":28},{"name":"John","age":21},{"name":"Sara","age":24}]とその作業完璧のような別のJSON形式で試してみましたjsonをcsvからPHPを使って変換する

    $jsonString = '{"cod":"200","calctime":0.3107,"cnt":15,"list":[{"id":2208791,"name":"Yafran","coord":{"lon":12.52859,"lat":32.06329},"main":{"temp":9.68,"temp_min":9.681,"temp_max":9.681,"pressure":961.02,"sea_level":1036.82,"grnd_level":961.02,"humidity":85},"dt":1485784982,"wind":{"speed":3.96,"deg":356.5},"rain":{"3h":0.255},"clouds":{"all":88},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}]}]}'; 
    
    //Decode the JSON and convert it into an associative array. 
    $jsonDecoded = json_decode($jsonString, true); 
    
    //Give our CSV file a name. 
    $csvFileName = 'file.csv'; 
    
    //Open file pointer. 
    $fp = fopen($csvFileName, 'w'); 
    
    //Loop through the associative array. 
    foreach($jsonDecoded as $row){ 
        //Write the row to the CSV file. 
        fputcsv($fp, $row); 
    } 
    
    //Finally, close the file pointer. 
    fclose($fp); 
    
    ?> 
    

    : はここに私のコードです。 コードを変更してCSV形式で正しく保存する方法

    画像: 今ではこのようにそれを保存します。pic 1 私はこのようにそれを保存する必要があります:pic 2

    誰かが私を助けることができますか?これは動作します

    +0

    を使用し、このhttps://github.com/danmandle/JSON2CSV –

    +1

    理解しますあなたのデータ構造、すなわち '$ jsonDecoded'配列とそれから間違っていることがわかります – RiggsFolly

    答えて

    1

    ・ホープ..

    <?php 
    
    $jsonString = '{"cod":"200","calctime":0.3107,"cnt":15,"list":[{"id":2208791,"name":"Yafran","coord":{"lon":12.52859,"lat":32.06329},"main":{"temp":9.68,"temp_min":9.681,"temp_max":9.681,"pressure":961.02,"sea_level":1036.82,"grnd_level":961.02,"humidity":85},"dt":1485784982,"wind":{"speed":3.96,"deg":356.5},"rain":{"3h":0.255},"clouds":{"all":88},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}]}]}'; 
    
    $jsonDecoded = json_decode($jsonString, true); 
    $csvHeader=array(); 
    $csvData=array(); 
    jsontocsv($jsonDecoded); 
    print_r($csvHeader); 
    print_r($csvData); 
    
    
    
    $csvFileName = 'file.csv'; 
    $fp = fopen($csvFileName, 'w'); 
    fputcsv($fp, $csvHeader); 
    fputcsv($fp, $csvData); 
    fclose($fp); 
    
    function jsontocsv($data) 
    { 
        global $csvData,$csvHeader; 
        foreach($data as $key => $value) 
        { 
         if(!is_array($value)) 
         { 
          $csvData[]=$value; 
          $csvHeader[]=$key; 
         } 
         else 
         { 
          jsontocsv($value); 
         } 
        } 
    } 
    

    JSON 2

    <?php 
    
    $jsonString =file_get_contents("http://samples.openweathermap.org/data/2.5/box/city?bbox=12,32,15,37,10&appid=b1b15e88fa797225412429c1c50c122a1");; 
    $jsonDecoded = json_decode($jsonString, true); 
    $csvHeader=array(); 
    $csvData=array(); 
    $csvFileName = 'file.csv'; 
    $fp = fopen($csvFileName, 'w'); 
    $counter=0; 
    foreach($jsonDecoded["list"] as $key => $value) 
    { 
        jsontocsv($value); 
        if($counter==0) 
        { 
         fputcsv($fp, $csvHeader); 
         $counter++; 
        } 
        fputcsv($fp, $csvData); 
        $csvData=array(); 
    } 
    fclose($fp); 
    
    function jsontocsv($data) 
    { 
        global $csvData,$csvHeader; 
        foreach($data as $key => $value) 
        { 
         if(!is_array($value)) 
         { 
          $csvData[]=$value; 
          $csvHeader[]=$key; 
         } 
         else 
         { 
          jsontocsv($value); 
         } 
        } 
    } 
    
    +0

    投稿しないでくださいスエーOFF-SITE。彼らは将来の訪問者にどのように利用されるのでしょうか – RiggsFolly

    +0

    これは完璧に動作しています!しかし、私たちがjsonをこのように使用すると、なぜうまくいかないのですか?http://samples.openweathermap.org/data/2.5/box/city?bbox=12,32,15,37,10&appid=b1b15e88fa797225412429c1c50c122a1 –

    +0

    @GeorgiBangeev私はその質問に基づいて答えますが、あなたが参照しているjsonには、このコードでは行えない複数の行とネストされたデータが含まれています。私はその特定のjsonの投稿を更新しています。 –

    0

    はこれを試してみてください。

    $rowNr = 0; 
    $prevKey = ""; 
    $target = []; 
    
    $iterator = new RecursiveArrayIterator($jsonDecoded['list'][$rowNr]); 
    iterator_apply($iterator, 'traverseStructure', array($iterator,$prevKey,&$target)); 
    
    function traverseStructure($iterator, $prevKey, &$target) { 
    
    while ($iterator -> valid()) { 
        if ($iterator -> hasChildren()) {   
         $prevKey = $iterator->key(); 
         traverseStructure($iterator -> getChildren(), $prevKey, $target);    
        } 
        else {      
         if(isset($prevKey) && !is_int($prevKey)){    
          $row1 = $prevKey."/".$iterator->key();    
         }else{ 
          $row1 = $iterator->key(); 
         }   
         $row2 = $iterator->current();   
         $target[$row1] = $row2;   
        } 
        $iterator -> next();   
        } 
    } 
    
    fputcsv($fp, array_keys($target)); 
    fputcsv($fp, array_values($target)); 
    
    関連する問題