3 Replies - 724 Views - Last Post: 31 January 2012 - 10:12 AM Rate Topic: -----

Topic Sponsor:

#1 clarkeash  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 55
  • Joined: 04-February 11

Parse JSON with PHP and MySQL Insert issue

Posted 31 January 2012 - 09:44 AM

Hi, I am trying to insert data from a JSON file into a database, I have no issue with database insertion.
However I am new to JSON, I have been trying to do this for a few days and the tutorials i have found all seem simpilar than what I have, so your advice is appreciated.

list.json
[
    {
        "rating": "9.2",
        "link": "http://www.imdb.com/title/tt0111161/",
        "name": "The Shawshank Redemption",
        "rank": "1",
        "year": "1994"
    },
    {
        "rating": "9.2",
        "link": "http://www.imdb.com/title/tt0068646/",
        "name": "The Godfather",
        "rank": "2",
        "year": "1972"
    },
    {
        "rating": "9.0",
        "link": "http://www.imdb.com/title/tt0071562/",
        "name": "The Godfather: Part II",
        "rank": "3",
        "year": "1974"
    }
]



I have then got as far as the following:
<?php
$list = file_get_contents('list.json');
$json = json_decode($list);

?>



I have tried a few ways to use this data but had no luck, i know that i need to put the data into a php array.
$films = array(
	'rating' => $json->rating,
	'link' => $json->link,
	'name' => $json->name,
	'rank' => $json->rank,
	'year' => $json->year
	);


I tried to use the above and attempted to print
$films['name'];
in a foreach loop, but this also did not work.

Advice required, Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Parse JSON with PHP and MySQL Insert issue

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1941
  • View blog
  • Posts: 7,296
  • Joined: 08-August 08

Re: Parse JSON with PHP and MySQL Insert issue

Posted 31 January 2012 - 09:52 AM

After decode, try:
print_r($json);

Was This Post Helpful? 1
  • +
  • -

#3 KingCuddles  Icon User is offline

  • D.I.C Regular

Reputation: 118
  • View blog
  • Posts: 443
  • Joined: 20-December 08

Re: Parse JSON with PHP and MySQL Insert issue

Posted 31 January 2012 - 09:57 AM

Try passing true as a second parameter to json_decode().

$json = json_decode($list, true);



This will return an associative array, which you can then work with.

In your case it would return:

Array
(
    [0] => Array
        (
            [rating] => 9.2
            [link] => http://www.imdb.com/title/tt0111161/
            [name] => The Shawshank Redemption
            [rank] => 1
            [year] => 1994
        )

    [1] => Array
        (
            [rating] => 9.2
            [link] => http://www.imdb.com/title/tt0068646/
            [name] => The Godfather
            [rank] => 2
            [year] => 1972
        )

    [2] => Array
        (
            [rating] => 9.0
            [link] => http://www.imdb.com/title/tt0071562/
            [name] => The Godfather: Part II
            [rank] => 3
            [year] => 1974
        )

)


This post has been edited by KingCuddles: 31 January 2012 - 10:08 AM

Was This Post Helpful? 3
  • +
  • -

#4 clarkeash  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 55
  • Joined: 04-February 11

Re: Parse JSON with PHP and MySQL Insert issue

Posted 31 January 2012 - 10:12 AM

$json = json_decode($list, true);

this worked brilliantly, thank you.

This post has been edited by clarkeash: 31 January 2012 - 10:12 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1