2017-04-14 10 views
0

JSONファイルを解析してMySQLデータベースに問題があります。これは一部のFacebookstatsの輸出です。複数のレベルを持つJSONファイルをMySQLデータベースで解析する

複数のページを複数回エクスポートしたため、データベースに対応するIDがあることが重要です。

(フェイスブックからまたはcURLの)JSONfileは次のようになります。

{ 
"data": [ 
    { 
     "name": "impressions", 
     "period": "week", 
     "values": [ 
      { 
       "value": 123456789, 
       "end_time": "2016-01-01T08:00:00+0000" 
      }, 
      { 
       "value": 12345678, 
       "end_time": "2016-01-02T08:00:00+0000" 
      }, 
      { 
       "value": 1234567, 
       "end_time": "2016-01-03T08:00:00+0000" 
      }, 
      { 
       "value": 123456, 
       "end_time": "2016-01-04T08:00:00+0000" 
      }, 
      { 
       "value": 12345, 
       "end_time": "2016-01-05T08:00:00+0000" 
      } 
     ], 
     "title": "Weekly Impressions", 
     "description": "The number of impressions seen of any content associated with your Page. (Total Count)", 
     "id": "101010101010\/insights\/page_impressions\/week" 
    } 
], 
"paging": { 
    "previous": "1", 
    "next": "2" 
    } 
} 

私は、理想的には、次のようになりますMySQLデータベースにこのデータを解析します:

id      value    end_time 
101010101010   123456789   2016-01-01T08:00:00+0000 
101010101010   12345678   2016-01-02T08:00:00+0000 
101010101010   1234567    2016-01-03T08:00:00+0000 
101010101010   123456    2016-01-04T08:00:00+0000 
101010101010   12345    2016-01-05T08:00:00+0000 

私が誰かを願っていますいくつかのアイデアがありました:-)

+1

あなたは何を試してみましたか? PHPはJSONを簡単に配列にデコードできます。いくつかのネストされたforeachはデータを簡単に取得して、テーブルに挿入することができます。 –

答えて

1

json_decode()を使用してください。例:

$jsonString = '{ 
"data": [ 
{ 
    "name": "impressions", 
    "period": "week", 
    "values": [ 
     { 
      "value": 123456789, 
      "end_time": "2016-01-01T08:00:00+0000" 
     }, 
     { 
      "value": 12345678, 
      "end_time": "2016-01-02T08:00:00+0000" 
     }, 
     { 
      "value": 1234567, 
      "end_time": "2016-01-03T08:00:00+0000" 
     }, 
     { 
      "value": 123456, 
      "end_time": "2016-01-04T08:00:00+0000" 
     }, 
     { 
      "value": 12345, 
      "end_time": "2016-01-05T08:00:00+0000" 
     } 
    ], 
    "title": "Weekly Impressions", 
    "description": "The number of impressions seen of any content associated with your Page. (Total Count)", 
    "id": "101010101010\/insights\/page_impressions\/week" 
} 
], 
"paging": { 
    "previous": "1", 
    "next": "2" 
    } 
}'; 

そして、連想配列にそれをデコード:

$assocData = json_decode($jsonString, true); //Setting second optional parameter to true makes it return an associative array. 

次に、あなたがしたい、しかし、それへのアクセス:

$data = $assocData['data']; 
関連する問題