简体   繁体   English

Json Decode返回NULL

[英]Json Decode returning NULL

I am trying to decode json into array in PHP.But My json is like 我试图在PHP中将json解码为数组。但我的json就像

string(307) " string(290) "{"id":"1","name":"test name","rowno":"0","note":"test notes","created_date":"2016-05-01","updated_date":"2016-05-12 05:08:05"}" "

string within a string!! 字符串中的字符串!! How to convert it into array. 如何将其转换为数组。

This looks like you're trying to decode ( or trying to output ) the data with var_dump() . 这看起来像是在尝试使用var_dump()解码( 或尝试输出 )数据。 That isn't the function you require; 那不是你需要的功能; what you require is json_decode() : 你需要的是json_decode()

$data = json_decode($json);

If that isn't the issue, and you're actually receiving the data as above then you'll have to strip it out - most likely using a regex like the following: 如果这不是问题,并且您实际上正在接收上述数据,那么您将不得不将其删除 - 最有可能使用如下所示的正则表达式:

$s = 'string(307) " string(290) "{"id":"1","name":"test name","rowno":"0","note":"test notes","created_date":"2016-05-01","updated_date":"2016-05-12 05:08:05"}" "';

preg_match('/\{(.*)\}/', $s, $matches);

print_r($matches);

Which would return your json : 哪个会返回你的json

Array
(
    [0] => {"id":"1","name":"test name","rowno":"0","note":"test notes","created_date":"2016-05-01","updated_date":"2016-05-12 05:08:05"}
    [1] => "id":"1","name":"test name","rowno":"0","note":"test notes","created_date":"2016-05-01","updated_date":"2016-05-12 05:08:05"
)

Thus allowing you to decode it properly within $matches . 因此允许您在$matches正确解码。

Regex is a beast to me so I'll try explain as best as possible what the expression is doing: 正则表达式对我来说是一个野兽所以我会尽可能地解释表达式正在做什么:

  • \\{ matches the first open { \\{匹配第一次打开{
  • (.*) matches any character inbetween (.*)匹配中间的任何字符
  • \\} matches the closing } \\}匹配结束}

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

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