从json格式获取数据错误

时间:2014-11-29 09:28:46

标签: php mysql json

我有以下JSON格式

 Array (
    [version] => 3 
    [status] => ok 
    [response] => Array (
        [data] => Array (
            [0] => Array (
                [accessible_wheelchair] => 1
                [address] => 200 5th Ave
                [category_labels] => Array (
                    [0] => Array (
                        [0] => Social
                        [1] => Food and Dining
                        [2] => Restaurants
                        [3] => Italian
                    )
                    [1] => Array (
                        [0] => Social
                        [1] => Food and Dining
                        [2] => Restaurants
                        [3] => Steakhouses
                    )
                    [2] => Array (
                        [0] => Social
                        [1] => Entertainment
                        [2] => Music and Show Venues
                    )
               )
               [country] => us
           )
        )
        [included_rows] => 20
    )
) 

我想从data-> [0]数组中获取数据。 我使用以下代码将数据插入数据库

$newurl="/*the api link is here*/";
$json = file_get_contents($newurl); 
$data = json_decode($json,true);
foreach($data as $value){
        $value1=$value;//$value
        foreach($value as $newValue){
                foreach($newValue as $newValue1){
                    $name=$newValue1["name"];
                    $sql="insert into restaurant (name) values(".$name.");";
                    db_query($sql) or die("insert error");
                }
        }
    }

但它无法插入数据。请确认是否有其他方法来获取我想要的数据并将其存储在数据库中。

1 个答案:

答案 0 :(得分:0)

餐馆名称在哪里?

<?php
$newurl="/*the api link is here*/";
$json = file_get_contents($newurl);
if ($json)         
{             
    if (!($response = json_decode(stripslashes($json), TRUE)))             
    {                 
        die("invalid json format");      
    }         
}

if (!empty($response['status']) && $response['status'] == "ok")
{   
    $data = $response['response']['data'];

    for ($i=0; $i < count($data); $i++) 
    { 
        // in this step data 0 wich you want to extract
        /*
        [accessible_wheelchair] => 1
                [address] => 200 5th Ave
                [category_labels] => Array (
                    [0] => Array (
                        [0] => Social
                        [1] => Food and Dining
                        [2] => Restaurants
                        [3] => Italian
                    )
                    [1] => Array (
                        [0] => Social
                        [1] => Food and Dining
                        [2] => Restaurants
                        [3] => Steakhouses
                    )
                    [2] => Array (
                        [0] => Social
                        [1] => Entertainment
                        [2] => Music and Show Venues
                    )
               )
        */
    // but what i confused here is restaurant name? i don't see restaurant name here..
    }
}