zoukankan      html  css  js  c++  java
  • jquery.post获取处理json数据

    摘要:

      使用jquery.post获取json数据,并对返回的json数据进行处理。php相应post请求返回json数据,js将返回数据转换为json对象进行处理。

    实现:

      前端:使用jquery.post请求数据

    $.post(url,data,success(data))

      响应请求:使用php响应请求(前面借鉴了phpwind的响应ajax请求,所以返回的是xml格式数据)

    $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
    $output = json_encode($arr);
    //{"a":1,"b":2,"c":3,"d":4,"e":5}
    
    header("Content-Type: text/xml;charset=utf-8");
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?><ajax><![CDATA[" . $output . "]]></ajax>";

      前端处理返回数据:首先取出xml中数节点值,然后将其转换为json对象

    function success(result){
        if(typeof(result) == "object"){
            result = result.lastChild.firstChild.nodeValue;//xml取节点值
            eval("result = "+result);//转换为json对象
        }
        ....
    }

    相关知识点:

      PHP中的JSON 

      1、json_encode():该函数主要用来将数组和对象,转换为json格式

    $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
    echo json_encode($arr);
    //{"a":1,"b":2,"c":3,"d":4,"e":5}
    $obj->body = 'another post';
    $obj->id = 21;
    $obj->approved = true;
    $obj->favorite_count = 1;
    $obj->status = NULL;
    echo json_encode($obj);
    /*
    $obj->body = 'another post';
    $obj->id = 21;
    $obj->approved = true;
    $obj->favorite_count = 1;
    $obj->status = NULL;
    echo json_encode($obj);
    */

      注:json只能是utf-8编码,son_encode()的参数必须是utf-8编码,否则会得到空字符或者null

      2、索引数组和关联数组


      索引数组

    $arr = Array('one', 'two', 'three');
    echo json_encode($arr);
    //"[]"(数组)
    //["one","two","three"]

      关联数组

    $arr = Array('1'=>'one', '2'=>'two', '3'=>'three');
    echo json_encode($arr);
    //"{}"(对象) 
    //{"1":"one","2":"two","3":"three"}

      将"索引数组"强制转化成"对象"

    json_encode( (object)$arr );
    //或
    //json_encode ( $arr, JSON_FORCE_OBJECT );

      3、类(class)的转换
      公开变量(public),其他东西(常量、私有变量、方法等等)都遗失

    class Foo {
        const ERROR_CODE = '404';
        public $public_ex = 'this is public';
        private $private_ex = 'this is private!';
        protected $protected_ex = 'this should be protected'; 
        public function getErrorCode() {
          return self::ERROR_CODE;
        }
    }
    
    $foo = new Foo;
    $foo_json = json_encode($foo);
    echo $foo_json;
    //{"public_ex":"this is public"}


      4、json_decode()
      将json文本转换为相应的PHP数据结构
      json_decode()总是返回一个PHP对象
      如果想要强制生成PHP关联数组,json_decode()需要加一个参数true

    $json = '{"foo": 12345}';
    $obj = json_decode($json);
    print $obj->{'foo'}; 
    // 12345

      5、注意
      1)、json只能用来表示对象(object)和数组(array),如果对一个字符串或数值使用json_decode(),将会返回null
      2)、json的分隔符(delimiter)只允许使用双引号,不能使用单引号
      3)、json名值对的"名"(冒号左边的部分),任何情况下都必须使用双引号
      4)、最后一个值之后不能添加逗号(trailing comma)

       处理返回json数据

      将返回数据转换为json,私用eval(),但报"invalid label"错误,解决方法

    var json = eval('(' + response + ')'); 
    //或 eval('var json = ' + response);

    参考:

      [1] 在PHP语言中使用JSON
      [2] 01 Sep 08 Js处理Json的”invalid label”错

    知识共享许可协议
    作品Tim Zhang创作,采用知识共享署名 3.0 中国大陆许可协议进行许可。 。
  • 相关阅读:
    array and ram
    char as int
    pointer of 2d array and address
    Install SAP HANA EXPRESS on Google Cloud Platform
    Ubuntu remount hard drive
    Compile OpenSSL with Visual Studio 2019
    Install Jupyter notebook and tensorflow on Ubuntu 18.04
    Build OpenCV text(OCR) module on windows with Visual Studio 2019
    Reinstall VirtualBox 6.0 on Ubuntu 18.04
    Pitfall in std::vector<cv::Mat>
  • 原文地址:https://www.cnblogs.com/ccdc/p/2467664.html
Copyright © 2011-2022 走看看