Php json format and js cross-origin call code

Source: Internet
Author: User
Tags php json
Php json format and js cross-origin call code

  1. Function jsontest ()

  2. {
  3. Var json = [{'username': 'Crystal ', 'userage': '20'}, {'username': 'Candy', 'userage ': '24'}];
  4. Alert (json [1]. username );

  5. Var json2 = [['crystal ', '20'], ['Candy', '24'];

  6. Alert (json2 [0] [0]);
  7. }

This function, the First alert (json [1]. username); will prompt "candy ". A json variable is an array object. Therefore, you must use the format obj. username for calling. The second alert (json2 [0] [0]); "crystal" is displayed ". The json2 variable is in full json format. Json and json2 variables have the same effect, but json2 is much simpler than json. This is the json format of JavaScript. Let's take a look at the json format in php. first look at a piece of code:

  1. $ Arr = array (

  2. Array (
  3. 'Catid' => '4 ',
  4. 'Catname' => 'rongrong ',
  5. 'Meta _ title' => 'Rong Rong blog'
  6. ),

  7. Array (

  8. 'Catid' => '6 ',
  9. 'Catname' => 'climber ',
  10. 'Meta _ title' => 'crawler ',
  11. )
  12. );
  13. $ Jsonstr = json_encode ($ arr );
  14. Echo $ jsonstr;

In this code, $ arr is an array. We use json_encode to convert $ arr to json format. This code will output: [{"catid": "4", "catname": "\ u7a0b \ u7a0b", "meta_title ": "\ u7a0b \ u7a0b \ u535a \ u5ba2" },{ "catid": "6", "catname": "climber", "meta_title ": "\ u6500 \ u767b \ u8005"}] this is php's processing of json data. For json data, php can also use the json_decode () function to convert json data into arrays. For example, in the above code, we use the json_decode function for processing. The above array is printed.

  1. $ Jsonstr = json_encode ($ arr );
  2. $ Jsonstr = json_decode ($ jsonstr );
  3. Print_r ($ jsonstr );

Next, let's take a look at how php json data and js json data are called each other.

Create the php_json.php file:

  1. $ Arr = array (

  2. Array (
  3. 'Catid' => '4 ',
  4. 'Catname' => 'rongrong ',
  5. 'Meta _ title' => 'Rong Rong blog'
  6. ),

  7. Array (

  8. 'Catid' => '6 ',
  9. 'Catname' => 'climber ',
  10. 'Meta _ title' => 'crawler ',
  11. )
  12. );
  13. $ Jsonstr = json_encode ($ arr );
  14. ----- The following is written outside the php range -----
  15. Var jsonstr = <? = $ Jsonstr? >;

Note: var jsonstr = <? = $ Jsonstr? >. This is to assign the value of json data to the jsonstr variable. Create another file json.html:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.