duandaotuo5542
2014-02-04 09:52
浏览 1.2k
已采纳

如何将此json数组保存到MYSQL数据库

I want to save this json array that my fb-login returns to a database. However I don't really know how to convert that into a string using php.

[{"id":"108124505876479","name":"Wakeboarding"},{"id":"112003352149145","name":"Bouldering"},{"id":"110008522357035","name":"Handball"}]

I have tried this code below but I am even not quite sure anymore whether what the fb login return is a json array or not.

$json = '{"id":"108124505876479","name":"Wakeboarding"},{"id":"112003352149145","name":"Bouldering"},{"id":"110008522357035","name":"Handball"}';

var_dump(json_decode($json));

How would I be able to save the different sports to my database in a string?

Any help would be much appreciated.

Thanks.

EDIT: The upper part returns NULL.


SECOND EDIT:

if (array_key_exists('sports', $me)){


                    $json = '$me['sports']'; PROBLEM IS HERE
                    $data = json_decode($json, true);
                    $sports = array();
                    foreach ($data as $item) {
                        $sports[] = $item['name'];
                    }

                    $user->fb_sports = $sports;
                }

图片转代码服务由CSDN问答提供 功能建议

我想保存这个我的fb-login返回数据库的json数组。 但是我真的不知道如何使用php将其转换为字符串。

  [{“id”:“108124505876479”,“name”:“Wakeboarding”},  { “ID”: “112003352149145”, “名称”: “抱石”},{ “ID”: “110008522357035”, “名称”: “手球”}] 
上  
 
  

我已尝试过以下代码,但我甚至不太确定fb登录返回是否为json数组。

  $ json ='{“  ID “:” 108124505876479" , “名”: “水上滑板”},{ “ID”: “112003352149145”, “名”: “抱石”},{ “ID”: “110008522357035”, “名”: “手球”  }'; 
 
var_dump(json_decode($ json)); 
   
 
 

我如何能够在字符串中将不同的运动保存到我的数据库?< / p>

非常感谢任何帮助。

谢谢。

编辑:上半部分返回NULL。< / p>


第二次编辑:

  if(array_key_exists('sports',$ me)){
 \  n 
 $ json ='$ me ['sports']'; 问题在这里
 $ data = json_decode($ json,true); 
 $ sports = array(); 
 foreach($ data as $ item){
 $ sports [] = $ item ['name']  ; 
} 
 
 $ user-&gt; fb_sports = $ sports; 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dongxin8709 2014-02-04 10:06
    已采纳
    $json = '[{"id":"108124505876479","name":"Wakeboarding"},{"id":"112003352149145","name":"Bouldering"},{"id":"110008522357035","name":"Handball"}]';
    $data = json_decode($json, true);
    $sports = array();
    foreach ($data as $item) {
        $sports[] = $item['name'];
    }
    

    if you want commas separating them:

    $output = implode(', ', $sports);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanjuelian4640 2014-02-04 09:57

    If var_dump(json_decode($json)) returns what you'd expect just save $json directly into the database.

    评论
    解决 无用
    打赏 举报
  • dongnuoyi8833 2014-02-04 10:01

    use :

     $json = serialize($json);
    
    评论
    解决 无用
    打赏 举报
  • dsdf64562672 2014-02-04 10:02
    $json1 = '{"id":"108124505876479","name":"Wakeboarding"},{"id":"112003352149145","name":"Bouldering"},{"id":"110008522357035","name":"Handball"}';
    $json2 = '[{"id":"108124505876479","name":"Wakeboarding"},{"id":"112003352149145","name":"Bouldering"},{"id":"110008522357035","name":"Handball"}]';
    
    var_dump(json_decode($json1,true));
    var_dump(json_decode($json2,true));
    

    watch the difference

    评论
    解决 无用
    打赏 举报
  • dooso0594 2015-02-03 06:27
    $string=mysql_real_escape_string($json);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题