2016-11-17 1 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

기도 말은 어떻게됩니까? 그냥 전부 인쇄 해. 'fname'이 있는지 확인할 수있는 문자열 표현이 있습니까? –

+0

PHP 질문 인 경우 태그를 붙이십시오. –

+0

그것은 주로 json 질문 torazaburo입니다. 빈 페이지가 표시됩니다. 오류도없고 결과도 없습니다. Matias elgart. – tylerbarton

답변

0

당신 코드 편집 : 당신은 그냥 배열의 키를 참조하려고 대신`$ Adata` 에코 경우

$jsondata = file_get_contents("books.json"); 
$arr_data = json_decode($jsondata, true); 
echo $arr_data['fname']; //shoud print out: John 
관련 문제