简体   繁体   English

访问Json对象PHP(字符串不是数组)

[英]Access Json Object PHP (String Not Array)

Yes, I have read previous questions and I know how to access a JSON object and how to convert it into an array. 是的,我已经阅读了先前的问题,并且知道如何访问JSON对象以及如何将其转换为数组。 I know about json_encode/decode. 我知道json_encode / decode。 My problem is that my JSON response has a string, arrays and all and it will always return NULL when I access the data directly. 我的问题是我的JSON响应包含一个字符串,数组和所有元素,当我直接访问数据时,它将始终返回NULL。

  object(Unirest\Response)#8 (4) {
    ["code"]=>
      int(200)
    ["body"]=>
      string(666) "{ "ticker": "AAPL:US", ".."
    ["headers"]=>
    array(9) {
    [0]=>
    string(15) "HTTP/1.1 200 OK"

Normally you would be able to directly access the object like this and this worked just fine when I last accessed the script a few days ago: 通常,您可以像这样直接访问对象,而当我几天前最后一次访问脚本时,此方法就可以正常工作:

$response->body->ticker

Or you could use json_decode with true to turn it into an array. 或者,您可以将json_decode与true一起使用,以将其转换为数组。

$array = json_decode($response->body, true);

However, all of this no longer works. 但是,所有这些不再起作用。 I believe they changed something with the output because it was working just a while ago but I have no clue. 我相信他们对输出进行了一些更改,因为它在前一段时间才起作用,但是我不知道。 Any idea how to access the ticker data? 知道如何访问股票行情数据吗? I tested it with a different API and the same commands are working just fine to retrieve data from a different API, but the output seems to be different. 我使用不同的API对其进行了测试,并且相同的命令可以很好地从不同的API检索数据,但是输出似乎有所不同。

$response->body is a json string assuming you didnt shorten it so much as to loose someting important and therefore needs to be seperately converted to a PHP data item. $response->body是一个json字符串, 假设您没有将其缩短得太多以至于失去一些重要的东西 ,因此需要将其单独转换为PHP数据项。

As its a json string representing an object why not convert it to a PHP object like so 作为代表对象的json字符串,为什么不像这样将其转换为PHP对象

$body = json_decode($response->body);

Then you can address its properties like 然后,您可以处理其属性,例如

$body->ticker

Alternatively 另外

$response->body = json_decode($response->body);

Now you can address it as you expected ie 现在您可以按预期的方式处理它,即

$response->body->ticker

Ok, finally solved it after reading this answer: PHP json_decode() returns NULL with valid JSON? 好的,在阅读以下答案后终于解决了: PHP json_decode()返回带有有效JSON的NULL?

Apparently, as I assumed earlier it was a formatting issue. 显然,正如我之前所假定的,这是一个格式问题。 I did not know that JSON would return NULL if the object includes non-UTF8 code and/or BOM codes. 我不知道如果对象包含非UTF8代码和/或BOM代码,JSON将返回NULL。

I couldn't find any BOM codes but I suppose there was some non-UTF8 that was breaking it. 我找不到任何BOM表代码,但我想是有一些非UTF8破坏了它。

Long story short this works: 长话短说,这有效:

$dec =  json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $response->body), true );         
 echo $dec['ticker'];

Well at least I now know a lot more about JSON which will come in handy some day ;) 好吧,至少我现在对JSON有更多了解,这将在一天之内派上用场;)

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

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