備忘錄_20160105(定位) 修改 回首頁

程式 2024-05-21 16:54:20 1716281660 100
JSON string, object, array convertion (Javascript,PHP,Python)

JSON string, object, array convertion (Javascript,PHP,Python)

object/array → string string → object/array
JavaScript var str1=JSON.stringify(obj1/ary1); var obj2/ary2=JSON.parse(str1);
PHP $str1=json_encode($obj1/$ary1); $obj2/$ary2=json_decode($str1);
Python import json
str1=json.dumps(obj1/ary1)
import json
obj2/ary2=json.loads(str1)

●Python3

import json

oEx1={
  "str1": "This is a test!",
  "num1": 38.7,
  "boo1": True,
  "boo2": False,
  "null1": None,
  "obj1": {},
  "ary1": []
};
  
aEx2=["string", 99.45, True, False, None, {}, []];

strEx1=json.dumps(oEx1)
strEx2=json.dumps(aEx2)

print(strEx1) # {"str1": "This is a test!", "num1": 38.7, "boo1": true, "boo2": false, "null1": null, "obj1": {}, "ary1": []}
print(strEx2) # ["string", 99.45, true, false, null, {}, []]

ex1=json.loads(strEx1); # maybe an object or an array
ex2=json.loads(strEx2); # maybe an object or an array

print(ex1) # {'str1': 'This is a test!', 'num1': 38.7, 'boo1': True, 'boo2': False, 'null1': None, 'obj1': {}, 'ary1': []}
print(ex2) # ['string', 99.45, True, False, None, {}, []]

●Javascript
  
  var oEx1={
    "str1": "This is a test!",
    "num1": 38.7,
    "boo1": true,
    "boo2": false,
    "null1": null,
    "obj1": {},
    "ary1": []
  };
  
  var aEx2=["string", 99.45, true, false, null, {}, []];
  
  var strEx1=JSON.stringify(oEx1);
  var strEx2=JSON.stringify(aEx2);
  
  console.log(strEx1); // {"str1":"This is a test!","num1":38.7,"boo1":true,"boo2":false,"null1":null,"obj1":{},"ary1":[]}
  console.log(strEx2); // ["string",99.45,true,false,null,{},[]]
  
  var ex1=JSON.parse(strEx1); // maybe an object or an array
  var ex2=JSON.parse(strEx2); // maybe an object or an array
  
  console.log(ex1);
  console.log(ex2);

●PHP
<?php
  
  // 標準方式,比較一致。
  $oEx1=new stdClass();
  $oEx1->str1="This is a test!";
  $oEx1->num1=38.7;
  $oEx1->boo1=true;
  $oEx1->boo2=false;
  $oEx1->null1=null;
  $oEx1->obj1=new stdClass();
  $oEx1->ary1=array();
  
  // 陣列搭配 key-value pairs 就會變成是 json 的物件
  $oEx3=array(
    "str1"=>"This is a test!",
    "num1"=>38.7,
    "boo1"=>true,
    "boo2"=>false
  );
  $oEx3["null1"]=null;
  $oEx3["obj1"]=new stdClass(); // 因為是空物件,沒有 key-value pair,所以得用 new stdClass()
  $oEx3["ary1"]=array();

  
  echo $oEx1->str1 . '<br>'; // This is a test!
  echo $oEx3["str1"] . '<br>'; // This is a test!
  
  $aEx2=array();
  $aEx2[]="string";
  $aEx2[]=99.45;
  $aEx2[]=true;
  $aEx2[]=false;
  $aEx2[]=null;
  $aEx2[]=new stdClass();
  $aEx2[]=array();
  
  $strEx1=json_encode($oEx1);
  $strEx3=json_encode($oEx3);
  $strEx2=json_encode($aEx2);
  
  echo '<pre>' . $strEx1 . '</pre>'; // {"str1":"This is a test!","num1":38.7,"boo1":true,"boo2":false,"null1":null,"obj1":{},"ary1":[]}
  echo '<pre>' . $strEx3 . '</pre>'; // {"str1":"This is a test!","num1":38.7,"boo1":true,"boo2":false,"null1":null,"obj1":{},"ary1":[]}
  echo '<pre>' . $strEx2 . '</pre>'; // ["string",99.45,true,false,null,{},[]]
  
  $ex1=json_decode($strEx1);
  $ex3=json_decode($strEx3);
  $ex2=json_decode($strEx2);
  
  echo '<pre>';
  var_dump($ex1);
  echo '</pre>';
  
// object(stdClass)#5 (7) {
//   ["str1"]=>
//   string(15) "This is a test!"
//   ["num1"]=>
//   float(38.7)
//   ["boo1"]=>
//   bool(true)
//   ["boo2"]=>
//   bool(false)
//   ["null1"]=>
//   NULL
//   ["obj1"]=>
//   object(stdClass)#6 (0) {
//   }
//   ["ary1"]=>
//   array(0) {
//   }
// }

  echo '<pre>';
  var_dump($ex3);
  echo '</pre>';
  
// object(stdClass)#7 (7) {
//   ["str1"]=>
//   string(15) "This is a test!"
//   ["num1"]=>
//   float(38.7)
//   ["boo1"]=>
//   bool(true)
//   ["boo2"]=>
//   bool(false)
//   ["null1"]=>
//   NULL
//   ["obj1"]=>
//   object(stdClass)#8 (0) {
//   }
//   ["ary1"]=>
//   array(0) {
//   }
// }
  
  echo '<pre>';
  var_dump($ex2);
  echo '</pre>';
  
// array(7) {
//   [0]=>
//   string(6) "string"
//   [1]=>
//   float(99.45)
//   [2]=>
//   bool(true)
//   [3]=>
//   bool(false)
//   [4]=>
//   NULL
//   [5]=>
//   object(stdClass)#6 (0) {
//   }
//   [6]=>
//   array(0) {
//   }
// }

?>