JSON与javascript交互基本知识

2000阅读 3评论2013-12-19 ygfinsight
分类:JavaScript

1.什么是json?

2.上例子

点击(此处)折叠或打开

  1. //建立对json象数组
  2. var employees = [
  3.     {"firstName":"guangfei","lastName":"yin"},
  4.     {"firstName":"cong","lastName":"xu"},
  5.     {"firstName":"qiang","lastName":"wang"}
  6. ];

  7. console.log(employees[0].firstName);
  8. console.log(employees[2].lastName);
  9. //json对象
  10. var connectData = {
  11.     "hostname":"localhost",
  12.     "user":"yingf",
  13.     "password":"yingf"
  14. };

  15. console.log(connectData.hostname);
  16. console.log(connectData.user);
  17.    var str1 = '{ "name": "cxh", "sex": "man" }';
  18. //json字符串
  19. var jsonString ='{\
  20.     "hostname":"localhost",\
  21.     "user":"yingf",\
  22.     "password":"yingf"\
  23. }';

  24. //json字符串转换成json对象
  25. var jsonObj = eval('('+jsonString+')');

  26. console.log(jsonObj.hostname);
  27. console.log(jsonObj.password);
3.结果


上一篇:Dear PHP, I'm leaving and yes, she's sexier(这位兄台狠狠的黑了一把PHP)
下一篇:win7 下开发node.js c++ addon