ios - How to make a dictionary of dictionaries in JSON? -
right have structure in json
"types":[ { "lowcadence":[ { "reinforcement":"-1", "weight":"100", "message":"pay attention. you're running low cadence. cadence %d steps per minute." } ] }, { "normalcadence":[ { "reinforcement":"0", "weight":"100", "message":"great, cadence on target. cadence %d steps per minute.", "enforcementsound":"ding" } ] }, { "highcadence":[ { "reinforcement":"1", "weight":"100", "message":"slow down. you're running on planned cadence. cadence %d steps per minute." } ] } ]
but have structure
does know how write in json?
i believe json like:
var types = { normalhr: { reinforcement: 0, weight: 100, message: 'great! heart rate in zone.', enforcementsound: 'ding' }, highhr: { reinforcement: 1, weight: 100, message: 'slow down. heart rate high!' }, lowhr: { reinforcement: -1, weight: 100, message: 'speed up. low heart rate.' } };
as @balder says in answer, can access use dictionary-style syntax, like:
types['normalhr']['reinforcement']
you use property-accessor syntax, like:
types.normalhr.reinforcement
the reason didn't include "type" of each item, can infer building grid - follows:
typeof types.normalhr.reinforcement
(this return"number"
)typeof types.normalhr.message
(this return"string"
)
similarly, counts - can count properties of specific object. in modern browsers, try:
object.keys(types.normalhr).length
(this return2
)
for older browsers, refer other methods here: how efficiently count number of keys/properties of object in javascript?
hope helps!
Comments
Post a Comment