PHP / JSON - 如何导入stdClass对象的名称?

时间:2013-10-10 20:53:34

标签: php json

如何将stdClass对象的名称导入数据库?

JSON对象包含:

[items] => stdClass Object
                    ( 
  [bigitems] => stdClass Object
                    (
                        [nameitem1] => stdClass Object
                            (
                                [height] => 100
                                [width] => 137
            )
            [nameitem2] => stdClass Object
                            (
                                [height] => 506
                                [width] => 678
            )
            [nameitem3] => stdClass Object
                            (
                                [height] => 330
                                [width] => 845
            )
            [nameitem3] => stdClass Object
                            (
                                [height] => 793
                                [width] => 788
            )
        )
)

我希望只存储数据库值nameitem1 - nameitem4

Nameitems例如 - box,case - 不同的单词没有数字范围 Bigitems是一个数字

    [items] => stdClass Object
                    ( 
  [4856985254] => stdClass Object
                    (
                        [box] => stdClass Object
                            (
                                [height] => 100
                                [width] => 137
            )
            [case] => stdClass Object
                            (
                                [height] => 506
                                [width] => 678
            )
            [paper] => stdClass Object
                            (
                                [height] => 330
                                [width] => 845
            )
        )

对于此操作,我尝试使用此脚本。脚本工作但缺少商店部分

function wwwResponse($url, $isUrl = true)
{
  if ($isUrl && !ini_get('allow_url_fopen')) {
    throw new Exception('allow_url_fopen = Off');
  }

  $content = file_get_contents($url);
  if ($isUrl) {
    $status_code = substr($http_response_header[0], 9, 3);
    if ($status_code != 200) {
      throw new Exception('Got status code ' . $status_code . ' expected 200');
    }
  }

  $json = json_decode($content);
  if (!$json) {
    throw new Exception('Response contained an invalid JSON response');
  }

  if ($json->status != 'ok') {
    throw new Exception($json->status_code);
  }

  return $json;
}

// --------------------------DB------------------------------- 
 mysql_connect("localhost", "baze", "xxxxxxx");
 mysql_select_db("baze");
//------------------------------------------------------------

    try {
    $json = wwwResponse($url);
        print_r ($json);
    }
  catch (Exception $e) {
  echo $e->getMessage();
}
echo "<pre>\n";
}

感谢您的帮助

3 个答案:

答案 0 :(得分:1)

$items = json_decode(json_encode($obj->items), true);
$names = array_keys((Array) $items['4856985254']);

// will return Array("nameitem1","nameitem2","nameitem3")

答案 1 :(得分:1)

如果它有帮助你可以通过这种方式解码JSON来获取数组而不是STD对象:

$result = json_decode($json, TRUE);

无论如何你可以这样做:

foreach ($data as $d) {
    foreach ($d as $actual) {
        //echo stuff here

    }
}

答案 2 :(得分:0)

$vars = get_object_vars($items->bigitems);
foreach ($vars as $key => $value)
... insert $key ....