section这个对象的xml格式如下
Book-Signing Event把上面的section对象转化成json对象
1.首先section作为一个对象
var s = {
section:{
}
};
2.它里面具有title,siging这两个属性
var s = {
section:{
"title": "Book-Signing Event",
"signing":
}
};
3.section这个对象中有两个名字都为signing的属性,所以这两个属性应该是一个数组
var s = {
section:{
"title": "Book-Signing Event",
"signing": []
}
};
4.signing这个数组中又有author,book这两个属性,所有author和book又是一个对象,该对象作为数组的一个元素
var s = {
section:{
"title": "Book-Signing Event",
"signing": [
{
"author":
}
]
}
};
5.author又有title,name两个属性,所以这两个属性又构成了一个对象
var s = {
section:{
"title": "Book-Signing Event",
"signing": [
{
"author": {"title": "Mr","name": "Vikram Seth"},
}
]
}
};
6.book又有title,price两个属性,所以这两个属性也构成了一个对象
var s = {
section:{
"title": "Book-Signing Event",
"signing": [
{
"author": {"title": "Mr","name": "Vikram Seth"},
"book": {"title": "A Suitable Boy","price": "$22.95"}
}
]
}
};
7.接下来是另外一个具有author,book两个属性的对象,如下就完成了上面xml到json对对象的转化
var s = {
"section":{
"title": "Book-Signing Event",
"signing": [
{
"author": {"title": "Mr","name": "Vikram Seth"},
"book": {"title": "A Suitable Boy","price": "$22.95"}
},
{
"author": {"title": "Dr","name": "Oliver Sacks"},
"book": {"title": "The Island","price": "$12.95"}
}
]
}
};
//测试
alert(s.section.title); //Book-Signing Event
//测试获取数组元素
alert(s.section.signing[0].author.title); //Mr