JSON 概要

JSON 值,可以是对象数组数字字符串或者三个字面值(false、null、true) 中的一个。

值中的字面值中的英文必须使用小写。

1. 对象由花括号括起来的逗号分割的成员构成,成员是字符串键和上文所述的值由逗号分割的键值对组成,如下:

{"name": "John Doe", "age": 18, "address": {"country" : "china", "zip-code": "10000"}}

2. 数组是由方括号括起来的一组值构成,如下:

[3, 1, 4, 1, 5, 9, 2, 6]

3. 数字也与 C 或者 Java 的数值非常相似。除去未曾使用的八进制与十六进制格式。除去一些编码细节。

4. 字符串与 C 或者 Java 的字符串非常相似。字符串是由双引号包围的任意数量 Unicode 字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。

实例一:封装与调用

1.1 封装 JSON - 对象类:

<?php 
header('Content-type:application/json;charset=utf-8');
  // 索引数组

  $json_string='{"id":1,"name":"hexingxing","email":"it@hexingxing.cn","features":["technology", "internet", "computer"]} ';  

$obj=json_decode($json_string);  

echo $json_string; //prints array
/*echo "name:".$obj->name; //prints hexingxing
echo "\n";
echo "email:".$obj->email; //prints it@hexingxing.cn
echo "\n";
echo "features:".$obj->features[0]; //prints technology*/
?>

1.2 通过 PHP 调用:

<?php
    $method = $_SERVER["REQUEST_METHOD"];
    $apikey = "hexingxing";
    $response = get_data($apikey, $method);
    $json = array();
    $json = json_decode($response, true);

        function get_data($apiKey, $data = null) {
        $url = "http://localhost/json/api".$data; 
        $cURL = curl_init();

        curl_setopt($cURL, CURLOPT_URL, $url);
        curl_setopt($cURL, CURLOPT_HTTPGET, true);
        curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Accept: application/json'
        ));
        return curl_exec($cURL);
    }
?>
<?php print_r($json['name'] ); //hexingxing ?>
<?php print_r($json['features']['0'] ); //technology ?>

2.1 封装 JSON - 数组类:

<?php 
header('Content-type:application/json;charset=utf-8');
  // 索引数组

  $json_string='{"success","hexingxing","it@hexingxing.cn",["technology", "internet", "computer"]} ';  

$obj=json_decode($json_string);  

echo $json_string; //prints array
?>

2.2 通过 PHP 调用:

<?php
    $method = $_SERVER["REQUEST_METHOD"];
    $apikey = "hexingxing";
    $response = get_data($apikey, $method);
    $json = array();
    $json = json_decode($response, true);

        function get_data($apiKey, $data = null) {
        $url = "http://localhost/json/api".$data; 
        $cURL = curl_init();

        curl_setopt($cURL, CURLOPT_URL, $url);
        curl_setopt($cURL, CURLOPT_HTTPGET, true);
        curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Accept: application/json'
        ));
        return curl_exec($cURL);
    }
?>
<?php print_r($json['0'] ); //success ?>
<?php print_r($json['1'] ); //hexingxing?>
<?php print_r($json['3']['1'] ); //technology?>

实例二:开放 API 与调用

已知 JSON 数据:

{"name":"hxx","fullname":"hexingxing","like":{"color":"pink","artist":"yanni"},"addr":{"location":"shanghai","nationality":"chinese"}}

通过 PHP 调用:

<?php print_r($json[name] ); //hxx ?>
<?php print_r($json[like][color] ); //pink ?> 
<?php print_r($json[addr][location] ); //shanghai ?>
<?php print_r($json[addr][nationality] ); //chinese ?>

友情提示:本站所有文章,如无特殊说明或标注,均为何星星原创发布。与此同时,趋于近年来本站的文章内容频繁被他站盗用与机器采集,现已全局禁用网站文字内容操作,了解详情或转载文章请 点此 继续!

0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用*标注