1

当我使用 graph api 时,我得到了这个结果。它是数组格式

{
  "id": "216805086", 
  "name": "raj sharma", 
  "first_name": "raj ", 
  "last_name": "sharma", 
  "link": "https://www.facebook.com/raj.sharma.5", 
  "username": "raj .sharma.5", 
  "favorite_teams": [
    {
      "id": "198358615428", 
      "name": "Mumbai Indians"
    }, 
    {
      "id": "190313434323691", 
      "name": "Indian Cricket Team"
    }
  ], 
  "favorite_athletes": [
    {
      "id": "100787839962234", 
      "name": "Saina Nehwal"
    }
  ], 
  "gender": "male", 
  "email": "raj.discoverme@gmail.com", 
  "timezone": 5.5, 
  "locale": "en_GB", 
  "verified": true, 
  "updated_time": "2013-08-13T06:01:17+0000"
}

我正在使用 php 语言和 phpmyadmin 数据库。现在我想将数组插入我的数据库。我应该为 id 、 name 、 first_name 、last_name、link、favorite_teams 等创建一个列,还是应该为所有这些创建一个列 ........ 如何将 tha 数组插入数据库

4

2 回答 2

1

Actually this is not an array. This is JSON. In JSON there are two formats,

JSONArray [ ]

JSONObject { }

You are getting the JSONObject as your output. There is a function in PHP callerd JSONDecode. Go through this you will get idea.

于 2013-08-13T06:29:36.223 回答
0

将 Facebook 应用程序数据存储在数据库中违反 Facebook 政策http://developers.facebook.com/policy/

 $data = '{
  "id": "216805086", 
  "name": "raj sharma", 
  "first_name": "raj ", 
  "last_name": "sharma", 
  "link": "https://www.facebook.com/raj.sharma.5", 
  "username": "raj .sharma.5", 
  "favorite_teams": [
    {
      "id": "198358615428", 
      "name": "Mumbai Indians"
    }, 
    {
      "id": "190313434323691", 
      "name": "Indian Cricket Team"
    }
  ], 
  "favorite_athletes": [
    {
      "id": "100787839962234", 
      "name": "Saina Nehwal"
    }
  ], 
  "gender": "male", 
  "email": "raj.discoverme@gmail.com", 
  "timezone": 5.5, 
  "locale": "en_GB", 
  "verified": true, 
  "updated_time": "2013-08-13T06:01:17+0000"
}';

//decode to get as php variable
$values = json_decode($data,true); //true to decode as a array not an object

$sql = "INSERT INTO TableName (id,name,first_name,last_name,link,username)
VALUES ('".$values['id']."','".$values['name']."','".$values['first_name']."','".$values['last_name']."','".$values['link']."','".$values['username']."')";
mysql_query($sql);

Json_decode() 采用 JSON 编码的字符串并将其转换为 PHP 变量。

于 2013-08-13T06:44:23.153 回答