Mutual conversion of JSON data formats

Source: Internet
Author: User
Tags format array object json key net variables

<?php
  1. $array _1 = Array (); One-dimensional arrays
  2. $array _2 = Array (); Two-dimensional array
  3. $array _1[' username '] = "Aseoe";
  4. $array _1[' website '] = "http://www.aseoe.com/";
  5. $array _2[' member ' [' ancto.net '] [' username '] = "ancto";
  6. $array _2[' member ' [' ancto.net '] [' website '] = "http://www.ancto.net/";
  7. $array _2[' member ' [' aseoe.com '] [' username '] = "Aseoe";
  8. $array _2[' member ' [' aseoe.com '] [' website '] = "http://www.aseoe.com/";
  9. Print_r ($array _2);
  10. $JSONOBJ _1 = Json_encode ($array _1); One-dimensional array into JSON data format
  11. echo $JSONOBJ _1;
  12. $JSONOBJ _2 = Json_encode ($array _2); Multidimensional array converted to JSON data format
  13. echo $JSONOBJ _2;
  14. Class aseoe{
  15. Public $puname = ' public name ';
  16. Protected $poname = "protected name";
  17. Private $prname = "private Name";
  18. Public Function GetName () {
  19. return $this->name;
  20. }
  21. }
  22. $ASEOEOBJ = new Aseoe ();
  23. $json _aseoe =json_encode ($ASEOEOBJ); object into JSON data format
  24. echo $json _aseoe; object is converted to JSON data, only public variables are converted, and private variables are not converted
  25. Print_r ($ASEOEOBJ);
  26. $jsonStr = ' {' key ': ' Value ', ' key1 ': ' value2 '} ';
  27. $json 2Array = Json_decode ($jsonStr, true); The default is object type when no 2nd argument is set
  28. Print_r ($json 2Array);
$jsonStr = ' {' key ': ' Value ', ' key1 ': ' value1 '} '; Json_decode ($JSONSTR);//After conversion is the object type Json_decode ($jsonStr, true);//After conversion is an array




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.