JSON 函数 业 ,精于勤 荒于嬉.

JSON 函数 json_decode 对 JSON 格式的字符串进行解码

发表日期:2021-07-01 08:56:20 | 来源: | 分类:JSON 函数

      示例1
1<?php 
2$json '{"a":1,"b":2,"c":3,"d":4,"e":5}';
3var_dump(json_decode($json));
4var_dump(json_decode($json, true));
5?>
      示例2
1<?php 
2$json '{"foo-bar": 12345}';
3$obj = json_decode($json);
4print $obj->{'foo-bar'};
5// 12345
6?>
      示例3
01<?php 
02// the following strings are valid JavaScript but not valid JSON
03// the name and value must be enclosed in double quotes
04// single quotes are not valid 
05$bad_json "{ 'bar': 'baz' }";
06json_decode($bad_json);
07 // null
08 // the name must be enclosed in double quotes
09 $bad_json '{bar: "baz" }';
10json_decode($bad_json);
11 // null// trailing commas are not allowed
12 $bad_json '{bar: "baz", }';
13json_decode($bad_json);
14 // null
15 ?>
      示例4
01<?php 
02// Encode the data.
03$json = json_encode(array(1 => array('English' => array('One','January'),'French' => array('Une','Janvier'))));
04// Define the errors.
05$constants = get_defined_constants(true);
06$json_errors array();
07foreach ($constants["json"as $name => $value) {
08    if (!strncmp($name"JSON_ERROR_", 11)) {
09        $json_errors[$value] = $name;
10    }
11}
12// Show the errors for different depths.
13foreach (range(4, 3, -1) as $depth) {
14    var_dump(json_decode($json, true, $depth));
15    echo 'Last error: '$json_errors[json_last_error()], PHP_EOL, PHP_EOL;
16}
17?>
      示例5
1<?php 
2$json = '{
3"number": 12345678901234567890}
4';
5var_dump(json_decode($json));
6var_dump(json_decode($json, false, 512, JSON_BIGINT_AS_STRING));
7?>

阅读全文 »

JSON 函数 json_encode 对变量进行 JSON 编码

发表日期:2021-07-01 08:56:20 | 来源: | 分类:JSON 函数

      示例1
1<?php
2$arr array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
3echo json_encode($arr);
4?>
      示例2
01<?php
02$a array('<foo>',"'bar'",'"baz"','&blong&'"\xc3\xa9");
03echo "Normal: ",  json_encode($a), "\n";
04echo "Tags: ",    json_encode($a, JSON_HEX_TAG), "\n";
05echo "Apos: ",    json_encode($a, JSON_HEX_APOS), "\n";
06echo "Quot: ",    json_encode($a, JSON_HEX_QUOT), "\n";
07echo "Amp: ",     json_encode($a, JSON_HEX_AMP), "\n";
08echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n";
09echo "All: ",     json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "\n\n";
10$b array();
11echo "Empty array output as array: ", json_encode($b), "\n";
12echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "\n\n";
13$c array(array(1,2,3));
14echo "Non-associative array output as array: ", json_encode($c), "\n";
15echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "\n\n";
16$d array('foo' => 'bar''baz' => 'long');
17echo "Associative array always output as object: ", json_encode($d), "\n";
18echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n";
19?>
      示例3
1<?php
2echo "Strings representing numbers automatically turned into numbers".PHP_EOL;
3$numbers array('+123123''-123123''1.2e3''0.00001');
4var_dump( $numbers, json_encode($numbers, JSON_NUMERIC_CHECK));
5echo "Strings containing improperly formatted numbers".PHP_EOL;
6$strings array('+a33123456789''a123');
7var_dump( $strings, json_encode($strings, JSON_NUMERIC_CHECK));
8?>
      示例4
01<?php
02echo "连续数组".PHP_EOL;
03$sequential array("foo""bar""baz""blong");
04var_dump( $sequential, json_encode($sequential));
05echo PHP_EOL."非连续数组".PHP_EOL;
06$nonsequential array(1=>"foo", 2=>"bar", 3=>"baz", 4=>"blong");
07var_dump( $nonsequential, json_encode($nonsequential));
08echo PHP_EOL."删除一个连续数组值的方式产生的非连续数组".PHP_EOL;
09unset($sequential[1]);
10var_dump( $sequential, json_encode($sequential));
11?>
      示例5
1<?php
2var_dump(json_encode(12.0, JSON_PRESERVE_ZERO_FRACTION));
3var_dump(json_encode(12.0));
4?>

阅读全文 »

JSON 函数 json_last_error_msg Returns the error string of the last json_encode() or json_decode() call

发表日期:2021-07-01 08:56:20 | 来源: | 分类:JSON 函数

json_last_error_msg

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

json_last_error_msgReturns the error string of the last json_encode() or json_decode() call

说明

json_last_error_msg(): string

Returns the error string of the last json_encode() or json_decode() call, which did not specify JSON_THROW_ON_ERROR.

参数

此函数没有参数。

返回值

Returns the error message on success, or "No error" if no error has occurred.

参见

阅读全文 »

JSON 函数 json_last_error 返回最后发生的错误

发表日期:2021-07-01 08:56:20 | 来源: | 分类:JSON 函数

      示例1
01<?php
02// 一个有效的 json 字符串$json[] = '{
03"Organization""PHP Documentation Team"}
04';
05// 一个无效的 json 字符串会导致一个语法错误,在这个例子里我们使用 ' 代替了 " 作为引号$json[] = "{
06'Organization''PHP Documentation Team'}
07";
08foreach ($json as $string) {
09    echo 'Decoding: ' $string;
10    json_decode($string);
11    switch (json_last_error()) {
12        case JSON_ERROR_NONE:            echo ' - No errors';
13        break;
14        case JSON_ERROR_DEPTH:            echo ' - Maximum stack depth exceeded';
15        break;
16        case JSON_ERROR_STATE_MISMATCH:            echo ' - Underflow or the modes mismatch';
17        break;
18        case JSON_ERROR_CTRL_CHAR:            echo ' - Unexpected control character found';
19        break;
20        case JSON_ERROR_SYNTAX:            echo ' - Syntax error, malformed JSON';
21        break;
22        case JSON_ERROR_UTF8:            echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
23        break;
24        default:            echo ' - Unknown error';
25        break;
26    }
27    echo PHP_EOL;
28}
29?>
      示例2
1<?php
2// 无效的 UTF8 序列$text = "\xB1\x31";
3$json  = json_encode($text);
4$error = json_last_error();
5var_dump($json$error === JSON_ERROR_UTF8);
6?>

阅读全文 »

全部博文(1589)
集速网 copyRight © 2015-2022 宁ICP备15000399号-1 宁公网安备 64010402001209号
与其临渊羡鱼,不如退而结网
欢迎转载、分享、引用、推荐、收藏。