现在我在 JSON 中有这个结构
"Types":[
{
"LowCadence":[
{
"Reinforcement":"-1",
"Weight":"100",
"Message":"ay attention. You're running low cadence. Your cadence is %d steps per minute."
}
]
},
{
"NormalCadence":[
{
"Reinforcement":"0",
"Weight":"100",
"Message":"Great, your cadence is on target. Cadence is %d steps per minute.",
"EnforcementSound":"ding"
}
]
},
{
"HighCadence":[
{
"Reinforcement":"1",
"Weight":"100",
"Message":"Slow down. You're running over your planned cadence. Cadence is %d steps per minute."
}
]
}
]
但我希望它有这样的结构
有人知道如何用 JSON 编写吗?
我相信你的 JSON 看起来像:
var Types = {
NormalHR: {
Reinforcement: 0,
Weight: 100,
Message: 'Great! Your heart rate is in the zone.',
EnforcementSound: 'ding'
},
HighHR: {
Reinforcement: 1,
Weight: 100,
Message: 'Slow down. Your heart rate is too high!'
},
LowHR: {
Reinforcement: -1,
Weight: 100,
Message: 'Speed up. Low heart rate.'
}
};
正如@Balder 在他们的回答中所说,您可以使用字典样式的语法,例如:
类型['NormalHR']['Reinforcement']
您还可以使用属性访问器语法,例如:
Types.NormalHR.Reinforcement
我没有包括每个项目的“类型”的原因是你可以很容易地推断出它来构建你的网格 - 如下:
typeof Types.NormalHR.Reinforcement
(这将返回 "number"
)typeof Types.NormalHR.Message
(这将返回 "string"
)同样,要获得计数 - 您可以计算特定对象的属性。在现代浏览器中,尝试:
Object.keys(Types.NormalHR).length
(这将返回 2
)对于较旧的浏览器,请参阅此处的其他方法:How to efficiently count the number of keys/properties of an object in JavaScript?
希望这会有所帮助!
关于ios - 如何在 JSON 中制作字典字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30925963/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |