json数据解析及typeof

   
// json格式
var people='{"authors": [{"firstName": "AAA","lastName": "BBB"},'
+' {"firstName": "CCC","lastName": "DDD"}],'
+'"people": [{"firstName": "EEE","lastName": "FFF"},'
+' {"firstName": "GGG","lastName": "HHH"}]}'
// 1、用JSON.parse需加外层单引号------》 将 JSON 字符串转换为对象
// document.write(JSON.parse(people).authors[1].lastName);



// 2、$.parseJSON
// jQuery.parseJSON()函数用于将格式完好的JSON字符串转为与之对应的JavaScript对象。
// 所谓"格式完好",就是要求指定的字符串必须符合严格的JSON格式,例如:属性名称必须加双引号、字符串值也必须用双引号。

document.write("<br/>");


// 3、访问json 不需加外层单引号
// alert(people.people[1].lastName);

// 4、修改不需加外层单引号
// people.people[1].lastName = "ZZZ";
// 修改后json数据
// alert(people.people[1].lastName);

// 5、对象转化json格式
var student = new Object();
student.name = "Lanny";
student.age = "25";
student.location = "China";
var json = JSON.stringify(student);
document.write(json);
document.write("<br/>");
// json格式转化为对象(同上)
// var objectStudent = JSON.parse(json);
// alert(objectStudent.name);

// 6、例子
/*
var objectStudent =new Array();
var students ='{"name":"wz","age":"18"}';
objectStudent=JSON.parse(students);
document.write(objectStudent.name);
document.write("<br/>");

var jsontext = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';
var contact = JSON.parse(jsontext);
document.write(contact.surname + ", " + contact.firstname+", "+contact.phone[1] );
document.write("<br/>");

var jsontext = '{ "hiredate": "2008-01-01T12:00:00Z", "birthdate": "2008-12-25T12:00:00Z" }';
var dates = JSON.parse(jsontext, dateReviver);
document.write(dates.birthdate.toUTCString());
document.write("<br/>");
function dateReviver(key, value) {

var a;
if (typeof value === 'string') {
a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
if (a) {
return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
+a[5], +a[6]));
}
}
return value;
};
*/
var aa = "test string";
document.write(typeof aa); // 'string'
document.write("<br/>");
document.write(typeof 90); // 'number'
document.write("<br/>");
// 'undefined' --- 这个值未定义;
// 'boolean' --- 这个值是布尔值;
// 'string' --- 这个值是字符串;
// 'number' --- 这个值是数值;
// 'object' --- 这个值是对象或null;
// 'function' --- 这个值是函数。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值