Parsing JSON with PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nhbach
    New Member
    • Dec 2009
    • 7

    Parsing JSON with PHP

    Hi all,
    I have a JSON generated from API, but when I try to save, it shows that txt file. https://eutils.ncbi.nlm.nih.gov/entr...ed-univ.edu.vn

    I write a short code to get the abstract in this link.
    Code:
    $api_json_url2 = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=3321921&tool=my_tool&email=nhbach@huemed-univ.edu.vn";							
    $json2 = file_get_contents($api_json_url2);
    $data2 = json_decode($json2, TRUE);
    	
    $abstract = 	$data['medent']['abstract'];
    But the result is nothing appear.

    Please help to get the "abstract" part.

    Thank you very much
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    if i see it right - the response you get from that request is no valid json, thus it cant simply be parsed that way. your best bet with that response and your requirement would be to match everything after the text abstract which is enclosed in double quotes. I would suggest to use regular expressions for that - but there are different ways to extract that part from plain text. Its much easier then trying to fix the weird structure to a valid JSON to parse it as that.

    Comment

    Working...