简体   繁体   English

如何检查json编码的字符串?

[英]How to check json encoded string?

I found that many of you use a function like this... 我发现你们中的许多人都使用这样的功能...

function isJson($string) {
    json_decode($string);
    return (json_last_error() == JSON_ERROR_NONE);
}

(from Fastest way to check if a string is JSON in PHP? ) (从最快的方法检查字符串在PHP中是否为JSON?

Many of json encoded string contain these characters { : , " 许多json编码的字符串都包含以下字符{ : , "

If i check the string that contain these characters it may return true. 如果我检查包含这些字符的字符串,则可能返回true。

example: 例:

$var = '{"1":"a2","a2":{"1":"a2.2","2":"a2.2"}}';
var_dump(isJson($var)) // true;

But when i check the string that is just a number, it return true too. 但是当我检查只是数字的字符串时,它也返回true。

example: 例:

$phone = '021234567';
var_dump(isJson($phone)); // true

I don't think the string '021234567' is json encoded and it should return false. 我不认为字符串'021234567'是json编码的,它应该返回false。


Let see more example for more clear question. 让我们看更多示例以了解更明确的问题。

$var1 = '021234567'; // this is string. not integer. NOT json encoded.
$var2 = '"021234567"'; // this is json encoded.
$var3 = 021234567; // this is integer. NOT json encoded.
$var4 = 'bla bla'; // this is string. NOT json encoded.
$var5 = '{"1":"a2","a2":{"1":"a2.2","2":"a2.2"}}'; // this is json encoded.

var_dump(isJson($var1));
var_dump(isJson($var2));
var_dump(isJson($var3));
var_dump(isJson($var4));
var_dump(isJson($var5));

the results are: 结果是:

true
true
true
false
true

But the expected results are: 但是预期结果是:

false
true
false
false
true

The question is. 问题是。 How to check the string that is valid json encoded? 如何检查有效的json编码的字符串?


More example 2 更多示例2

If 021234567 is an integer. 如果021234567是整数。 when i use json_encode it returns integer and have nothing to do with it again when use json_decode 当我使用json_encode时,它返回整数,并且与使用json_decode时无关

$var = 021234567;
echo $var; // 4536695
echo json_encode($var); // 4536695
echo json_decode(4536695); // 4536695
echo json_decode($var); // 4536695

$var = 21234567;
echo $var; // 21234567
echo json_encode($var); // 21234567
echo json_decode(4536695); // 21234567
echo json_decode($var); // 21234567

so, the integer value should return false when i check json encoded string with isJson function. 因此,当我使用isJson函数检查json编码的字符串时,整数值应返回false。 but it is not. 但事实并非如此。

If 021234567 is string. 如果021234567是字符串。 when i use json_encode i should got "021234567" (with double quote). 当我使用json_encode时,我应该得到“ 021234567”(带双引号)。 if this string has no double quote, it should not be json encoded. 如果此字符串没有双引号,则不应使用json编码。

$var = '021234567';
echo $var; // 021234567
echo json_encode($var); // "021234567"
echo json_decode('"021234567"'); // 021234567
echo json_decode($var); // 21234567

// example if string is text with double quote in it.
$var = 'i"m sam';
echo $var; // i"m sam
echo json_encode($var); // "i\"m sam"
echo json_decode('"i\"m sam"'); // i"m sam
echo json_decode($var); // null

As you see. 正如你看到的。 the simple string with json encoded should contain at least double quote character at open and close of that string. 带有json编码的简单字符串应在该字符串的打开和关闭处至少包含双引号字符。

Please help me how to check that string is valid json encoded . 请帮助我检查字符串是否为有效的json编码

I think the valid json encoded string should at least have double quote " or curly bracket {} at the open and close of that string. 我认为有效的json编码字符串在该字符串的开头和结尾处至少应带有双引号"或大bracket {}

I don't know how to check that or don't know how to preg_match that. 我不知道如何检查或不知道preg_match。

From PHP Documentation : PHP文档

PHP implements a superset of JSON - it will also encode and decode scalar types and NULL. PHP实现了JSON的超集-它还将对标量类型和NULL进行编码和解码。 The JSON standard only supports these values when they are nested inside an array or an object. JSON标准仅在将这些值嵌套在数组或对象中时才支持它们。

So, the reason leading zeros make this check pass is that json_decode just strips them during parsing without giving any errors. 因此,前导零使此检查通过的原因是json_decode只是在解析过程中剥离了它们而没有给出任何错误。 For example: 例如:

echo json_decode('0123');

will just display 123 . 只会显示123

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

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