Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a dictionary of dictionaries in JSON?

Tags:

json

ios

plist

Right now I have this structure in JSON

"Types":[  
            {  
               "LowCadence":[  
                  {  
                     "Reinforcement":"-1",
                     "Weight":"100",
                     "Message":"Pay 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."
                  }
               ]
            }
         ]

But I would like it to have this structure

enter image description here

Does anyone know how to write it in JSON?

like image 630
Eugene Gordin Avatar asked Mar 16 '23 16:03

Eugene Gordin


2 Answers

I believe your JSON would look something like:

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.'
    }
};

As @Balder says in their answer, you can then access use dictionary-style syntax, like:

  • Types['NormalHR']['Reinforcement']

You could also use property-accessor syntax, like:

  • Types.NormalHR.Reinforcement

The reason I didn't include the "type" of each item, is that you can easily infer it for building your grid - as follows:

  • typeof Types.NormalHR.Reinforcement (this will return "number")
  • typeof Types.NormalHR.Message (this will return "string")

Similarly, to get the counts - you can count the properties of a specific object. In modern browsers, try:

  • Object.keys(Types.NormalHR).length (this will return 2)

For older browsers, refer to other methods here: How to efficiently count the number of keys/properties of an object in JavaScript?

Hope this helps!

like image 108
Troy Alford Avatar answered Mar 20 '23 00:03

Troy Alford


In objective C you can write:

NSDictonary *types = @{
@"NormalHR": @{
    @"Reinforcement": [NSNumber numberWithInt:0],
    @"Weight": [NSNumber numberWithInt:100],
    @"Message": @"Great! Your heart rate is in the zone.",
    @"EnforcementSound": @"ding"
},
@"HighHR": @{
    @"Reinforcement": [NSNumber numberWithInt:1],
    @"Weight": [NSNumber numberWithInt:100],
    @"Message": @"Slow down. Your heart rate is too high!"
},
@"LowHR": @{
    @"Reinforcement": [NSNumber numberWithInt:-1],
    @"Weight": [NSNumber numberWithInt:100],
    @"Message": @"Speed up. Low heart rate."
}

};

like image 28
simone Avatar answered Mar 19 '23 22:03

simone