【问题标题】:Illegal string offset 'properties', how to pass json encoded values非法字符串偏移“属性”,如何传递 json 编码值
【发布时间】:2014-03-07 17:36:58
【问题描述】:

如何将 json_encoded 结果的字段分配给变量。我有以下内容:

$jsonres = json_encode($result); //where result is an array holding fields including name (string), properties (object type or array)

我尝试了以下方法:

echo $jsonres['properties']; // failed with "Illegal string offset 'properties'"
var_dump ($jsonres->properties); //"Notice: Trying to get property of non-object in..." 

我需要能够在我的表单中使用“属性”的值。

谢谢

【问题讨论】:

  • 在你的例子中 $jsonres 是一个对象。而$jsonres['properties'] 访问数组的元素。您必须使用对象表示法(o->prop)。
  • @hindmost,对不起,我没有得到 (o->prop)。似乎类似于我放在 var_dump ($jsonres->properties) 上的内容;对吗?
  • 你使用的Response类的定义在哪里?
  • @Kingsley 你很难理解。所以我猜你必须: 1)将 $result 解码为 php 数组(或对象); 2) 检索需要的属性; 3) 将其编码为 JSON。
  • @NobleUplift 我无法提出解决方案,因为我仍然不明白这个问题。我已经要求提供详细信息,但 OP 没有提供。

标签: php json mongodb echo var-dump


【解决方案1】:

只对 properties 属性进行编码,而不是对整个对象进行编码:

$jsonres = json_encode($result['properties']);
echo $jsonres;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 2020-11-29
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多