2 jaswhen jaswhen 于 2014.11.27 10:17 提问

求助:Json对象会无形中缺少子对象,请问是为什么?

我有一个Json对象,但是在实际操作中,会无形中少了一些子对象,但是又不知道是为啥
希望各位大大帮我解惑一下
比如下面这个Json对象 解析出来的时候
就只有两个子对象,少了两个子对象

var JsonObj= {
008: {
name: "(197) 1 1/2”",
no: "008",
size: "1 1/2寸",
width: "157mm",
height: "307mm",
listedtime: "2012-01-01",
production: "否",
recordtime: "2014-01-01",
remarks:"描述197",

imgurl1: "image/Products/197_150x150.jpg",
imgurl2: "image/Products/197_300x300.jpg",
imgurl3: "image/Products/197_300x300.jpg"
},
009: {
name: "(198) 1 1/4”",
no: "009",
size: "1 1/4寸",
width: "158mm",
height: "308mm",
listedtime: "2012-01-01",
production: "否",
recordtime: "2014-01-01",
remarks:"描述198",

imgurl1: "image/Products/198_150x150.jpg",
imgurl2: "image/Products/198_300x300.jpg",
imgurl3: "image/Products/198_300x300.jpg"
},
010: {
name: "(190) 3/4”",
no: "010",
size: "3/4寸",
width: "150mm",
height: "300mm",
listedtime: "2012-01-01",
production: "否",
recordtime: "2014-01-01",
remarks:"描述190",

imgurl1: "image/Products/190_150x150.jpg",
imgurl2: "image/Products/190_300x300.jpg",
imgurl3: "image/Products/190_300x300.jpg"
},
011: {
name: "(191) 2”",
no: "011",
size: "2寸",
width: "151mm",
height: "301mm",
listedtime: "2012-01-01",
production: "否",
recordtime: "2014-01-01",
remarks:"描述191",

imgurl1: "image/Products/191_150x150.jpg",
imgurl2: "image/Products/191_300x300.jpg",
imgurl3: "image/Products/191_300x300.jpg"
}
}

6个回答

shijing266
shijing266   2014.11.27 10:36

应该与你所设置的数字有关,你尝试将08、09、10、11 都加上双引号试试

jaswhen
jaswhen 加上双引号是可以了,但是为啥没加就不行呢?
3 年多之前 回复
showbo
showbo   Ds   Rxr 2014.11.27 11:17

去掉前面的0,要不就会当做8进制了,010对应的10进制是8,010对应的是9,键名称重复了导致前面的8,9键值被覆盖了

既然是json对象,你的键名称最好用引号括起

wgw335363240
wgw335363240   2014.11.27 12:00

把你的json内容值拷贝到这个网站:http://www.bejson.com/
格式化一下,看看是不是真的少了一部分内容,通常是不会少内容的,除非你的json格式不正确,少了引号或者但引号。
{
008: {
name: "(197) 1 1/2”",
no: "008",
size: "1 1/2寸",
width: "157mm",
height: "307mm",
listedtime: "2012-01-01",
production: "否",
recordtime: "2014-01-01",
remarks: "描述197",
imgurl1: "image/Products/197_150x150.jpg",
imgurl2: "image/Products/197_300x300.jpg",
imgurl3: "image/Products/197_300x300.jpg"
},
009: {
name: "(198) 1 1/4”",
no: "009",
size: "1 1/4寸",
width: "158mm",
height: "308mm",
listedtime: "2012-01-01",
production: "否",
recordtime: "2014-01-01",
remarks: "描述198",
imgurl1: "image/Products/198_150x150.jpg",
imgurl2: "image/Products/198_300x300.jpg",
imgurl3: "image/Products/198_300x300.jpg"
},
010: {
name: "(190) 3/4”",
no: "010",
size: "3/4寸",
width: "150mm",
height: "300mm",
listedtime: "2012-01-01",
production: "否",
recordtime: "2014-01-01",
remarks: "描述190",
imgurl1: "image/Products/190_150x150.jpg",
imgurl2: "image/Products/190_300x300.jpg",
imgurl3: "image/Products/190_300x300.jpg"
},
011: {
name: "(191) 2”",
no: "011",
size: "2寸",
width: "151mm",
height: "301mm",
listedtime: "2012-01-01",
production: "否",
recordtime: "2014-01-01",
remarks: "描述191",
imgurl1: "image/Products/191_150x150.jpg",
imgurl2: "image/Products/191_300x300.jpg",
imgurl3: "image/Products/191_300x300.jpg"
}
}

ForestDB
ForestDB   Rxr 2014.11.27 13:26

008不是一个valid的javascript的identifier。

ForestDB
ForestDB   Rxr 2014.11.27 13:29

var JsonObj = {
name: "bill"
};
这里的name是个语法糖,实际上是"name": "bill"
只有name是个合法的JS的标识符的时候,才能不用引号(所谓的糖语法)
引用时,也可以JsonObj.name这样
但是如果name不是合法的JS的标识符的时候,就必须用引号了
且引用时,只能这样JsonObj["008"]
这是基本的JS的语法规则,可以找资料参考。

jasondu264
jasondu264   2014.11.27 19:34

json对象值跟键都要加双引号

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!