简体   繁体   English

PHP - json_decode 给出 NULL

[英]PHP - json_decode giving NULL

I'm trying to use the advice given in How to search through a JSON Array in PHP to lookup country names based on a country code in a .json file.我正在尝试使用如何在 PHP 中搜索 JSON 数组中给出的建议,以根据 .json 文件中的国家代码查找国家名称。

I have a file, countrycodes.js which is formatted like this:我有一个文件countrycodes.js ,其格式如下:

countries = [
    {code: "GB", name: "United Kingdom"},
    {code: "AF", name: "Afghanistan"},
    // ...
    {code: "ZM", name: "Zambia"},
    {code: "ZW", name: "Zimbabwe"}
];

The variable, countries has to be there because part of the application relies on this.变量, countries必须存在,因为部分应用程序依赖于此。

In PHP I've done the following:在 PHP 中,我做了以下事情:

$str = file_get_contents('countrycodes.js');
var_dump($str);

This outputs a string:这会输出一个字符串:

string(9642) "countries = [
{code: "GB", name: "United Kingdom"},
{code: "AF", name: "Afghanistan"},
// ...

However, when I try and json_decode it, the following gives NULL :但是,当我尝试json_decode它时,以下给出NULL

$str = file_get_contents('countrycodes.js');
$json = json_decode($str);
var_dump($json);

I don't know why this is because json_decode accepts a string, and this is what's given on the link I posted above?我不知道为什么这是因为json_decode接受一个字符串,这就是我在上面发布的链接上给出的内容? I tried removing the JavaScript variable ( countries = ) but this made no difference.我尝试删除 JavaScript 变量( countries = ),但这没有任何区别。

Ultimately what I want to do is be able to give PHP a country code such as 'GB' and get it to return the appropriate name eg 'United Kingdom'.最终,我想要做的是能够给 PHP 一个国家code ,例如“GB”,并让它返回适当的name ,例如“英国”。 My understanding of this is the json_decode part will need to work before this is possible.我对此的理解是json_decode部分需要在这可能之前工作。

For reference, the reason countries = was being used is for populating a <select> element based on advice given here: Populating select using ajax json array作为参考,使用countries =的原因是根据此处给出的建议填充<select>元素: Populating select using ajax json array

If you json_decode returns null , this means the input string is not in a valid format.如果您json_decode返回null ,这意味着输入字符串的格式无效。 Your file contains a javascript variable and not a JSON string.您的文件包含一个 javascript 变量而不是 JSON 字符串。

This is how the string should probably formatted:这就是字符串应该如何格式化:

{
    "countries": [
        {"code": "GB", "name": "United Kingdom"},
        {"code": "AF", "name": "Afghanistan"},
        {"code": "ZM", "name": "Zambia"},
        {"code": "ZW", "name": "Zimbabwe"}
    ]
}

Extending on Jerodev's answer, if you cannot will not change the representation, you could do the following.扩展 Jerodev 的答案,如果您 不能 更改表示,则可以执行以下操作。

Modify your file so that you wrap the keys with quotes.修改您的文件,以便用引号将键括起来。 Something like,就像是,

countries = [
    {"code": "GB", "name": "United Kingdom"},
    {"code": "AF", "name": "Afghanistan"},
    // ...
    {"code": "ZM", "name": "Zambia"},
    {"code": "ZW", "name": "Zimbabwe"}
];

And then just decode the json part.然后只需解码 json 部分。

$str = file_get_contents('countrycodes.js');
$str = substr($str,12, strlen($str)-13); // do not include the semicolon
var_dump(json_decode($str));

However this is not a good approach, so I'd still suggest to look for other ways to populate the data in your element.然而这不是一个好方法,所以我仍然建议寻找其他方法来填充元素中的数据。

Note that 12 is the position where [ starts, if you have made any changes to variable names or spaces, that (and 13 ) needs to change.请注意, 12[开始的位置,如果您对变量名称或空格进行了任何更改,则(和13 )需要更改。

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

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