2016-11-17 19 views
-1

私はこのデータをbooks.jsonという名前のファイルに保存しています。jsonを配列に入れることができません

{ 
 
\t "date_reviewed":"08/Jan/16", 
 
\t "fname":"John", 
 
\t "mname":"", 
 
\t "lname":"Locke", 
 
\t "email":"", 
 
\t "title":"Boxed In!", 
 
\t "imagelocation":"img\book_covers\BoxedIn.jpg", 
 
\t "series_name":"Dr. Gideon Box", 
 
\t "sellerssite":"https://www.amazon.com/Boxed-Gideon-Donovan-Creed-Novel-ebook/dp/B019HANKW8", 
 
\t "genre":"Thriller,Suspence", 
 
\t "authorsite":"https://johnlockeauthor.wordpress.com/", 
 
\t "facebook":"https://www.facebook.com/john.locke.395669?fref=ts", 
 
\t "fbid":"John Locke", 
 
\t "twitter":"https://twitter.com/DonovanCreed", 
 
\t "twitterid":"@DonovanCreed", 
 
\t "review":"A series you have to be sick to love. I guess that's telling as I do love the series. 
 
           The latest in a series from an author that also has a number of other series that I have been devouring for the last couple of years. What makes these series unique is that they are all tied together by the characters that star in each series. So, if you're a fan of one and have not branched out into the others then it is time to go to his author page and start the reading adventure that will take you into the minds of a lethal assassin, a gunfighter and reluctant sheriff, and a private detective made famous by escaping from a deranged kidnapper. 
 
           Dr. Box may be brilliant when it comes to saving the life of children written off as lost causes but his activities outside the operating room have left any that know him with a strong desire to see him thrown into the bowels of hell as he surely is the very epitome of demon walking the earth. Good thing that he's the only one in the world that can save a life that others deem impossible. Just pray that if you've ever slighted him that you never show up in his hospital under his care. It will be an encounter you never live to regret. 
 
           In this tale Dr. Box is visited by the only man that causes true fear in a doctor who's moral compass was broken at birth. Donovan Creed. An assassin that you do not want to owe a favor to. 
 
           Donovan comes to collect. He has the plan to kill a terrorist and in the process save thousands of lives. The only problem, what he wants is the one thing Dr. Box cannot do. 
 
           Painted into a box (pun intended) he has no choice but to call on the one person that even Donovan is afraid of. Rose. 
 
           Together they come up with a full proof plan. Too bad that Donovan is already two steps ahead and the consequences for Dr. Box are steep and his life may never be the same. 
 
           As a fan, I say you have to add this to your library.", 
 
\t "rating":"5" 
 
}

私は、データ内の特殊文字をエスケープするためにデータを符号化し、その後取り出します。

<?php 
 

 
    
 
    //Get data from existing json file 
 
\t $jsondata = file_get_contents("books.json");; 
 
      
 
      $arr_data = json_encode($jsondata, true); 
 
?>

私は、配列であることを配置しようとしたが、それはので、私は多分、私はそれをデコードして、私はこのコードを試してみましたので、配列にそれを置くためにと思っていたが失敗しました。

$Adata = json_decode($arr_data, true); 
 
      
 
echo $Adata['fname'];

ので、置くこと、すべて一緒にあなたが生成し、エラー、次のしている "警告:不正な文字列がオフセット 'fnameに'"

<?php 
 

 
    
 
    //Get data from existing json file 
 
\t $jsondata = file_get_contents("books.json");; 
 
      
 
      $arr_data = json_encode($jsondata, true); 
 
          
 
      $Adata = json_decode($arr_data, true); 
 
      
 
      echo $Adata['fname']; 
 

 
?>

I助けていただければ幸いです。私の究極の目標は、データを配列に取得して、コードにフィードしてDBに格納できるようにすることです。

+0

はあなただけで、 '$ Adata'エコーの代わりに、その配列のキーを参照しようとするとどうなるか、教えて祈りますか?全体を印刷してください。 'fname'が存在するかどうかを調べることができる文字列表現を持っていますか? –

+0

これがPHPの質問であれば、そのようにタグ付けしてください。 –

+0

それは主にjsonの質問torazaburoです。 空白のページが表示されますが、エラーも結果もありません。Matias elgart – tylerbarton

答えて

0

これにあなたのコードの編集:

$jsondata = file_get_contents("books.json"); 
$arr_data = json_decode($jsondata, true); 
echo $arr_data['fname']; //shoud print out: John 
関連する問題