简体   繁体   English

在add_post_meta中存储Json值

[英]Storing Json value in add_post_meta

How can I store this Json value in add_post_meta 如何将此Json值存储在add_post_meta中

  $offer_name= $item['data'][$i]['Offer']['name'];

  add_post_meta($post_id, 'offer_name', $offer_name);

The json value of $offer_name doesn't get stored but normal string get stored. $ offer_name的json值未存储,但存储了正常的字符串。

Update 1: this is how I tested . 更新1:这是我测试的方式。

case 1 // With json the value doesn't get stored.. case 1 //使用json,值不会被存储..

 $offer_name= $item['data'][$i]['Offer']['name']; 

echo $offer_name; // the value gets echoed .

 add_post_meta($post_id, 'offer_name', $offer_name); // the value of $offer_name doesn't  get stored 

case 2 // without json the value get stored.. case 2 //没有json值存入..

  $offer_name="My  Name"; 

 add_post_meta($post_id, 'offer_name', $offer_name); // the value of $offer_name does get stored 

Update 2: if i store the json data in "{ }" then the value get stored in WordPress 更新2:如果我将json数据存储在“{}”中,则该值将存储在WordPress中

$offer_name ="{$item['data'][$i]['Offer']['name']}";


add_post_meta($post_id, 'offer_name', $offer_name);

Thanks in Advance 提前致谢

You need to use json_encode() . 你需要使用json_encode() This will return a JSON formatted string which you can use to save in your database. 这将返回一个JSON格式的字符串,您可以使用该字符串保存在数据库中。

Note that with characters such as ë, â, Æ etc you should use addslashes() on your JSON encoded string before you attempt to save it in the database. 请注意,对于诸如ë,â,Æ等字符,您应该在尝试将其保存在数据库中之前对JSON编码的字符串使用addslashes() This way once you decode the JSON string these special characters can be seen properly. 这样,一旦解码了JSON字符串,就可以正确地看到这些特殊字符。

(Yes this is very old but I am searching for something myself and thought why not eh?) (是的,这是非常古老但我自己在寻找一些东西并想到为什么不呢?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM