JSON对象作为php字符串

sis*_*sko 1 php string json object

可能重复:
将JSON转换为UTF-8字符串

我正在努力缓存一些推特供长期使用.我的搜索结果产生以下类型的数据结构:

"results": [
{
  "created_at": "Sun, 08 Apr 2012 18:31:04 +0000",
  "entities": {
    "hashtags": [
      {
        "text": "cheeringfor",
        "indices": [
          54,
          66
        ]
      }
    ],
    "urls": [

    ],
    "user_mentions": [
      {
        "screen_name": "BenSpies11",
        "name": "Ben Spies",
        "id": 32124771,
        "id_str": "32124771",
        "indices": [
          0,
          11
        ]
      }
    ]
  },...
Run Code Online (Sandbox Code Playgroud)

我试图在我的数据库表中包含此输出的hashtags部分,但我试图将其保存为纯字符串.

我已经尝试将其作为字符串i:e (string)$result->hashtags但是我得到了无法转换为字符串错误.我也尝试了serialize()函数,当我试图取消序列化并获取我的对象时,它有效但却出现了php错误.

wtb*_*gtr 5

首先,查看php的json_decode函数.它会将您的JSON字符串转换为数据结构,然后您应该能够查找您的主题标签:

$data = json_decode($jsonstring, true);
$hashtags = $data['results'][0]['entities']['hashtags']; // this will be an array
Run Code Online (Sandbox Code Playgroud)

我没有测试过,但我认为这是正确的.如果它不完全正确,你可以在这里阅读json_decode():http://php.net/manual/en/function.json-decode.php