2

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?

Eugene Gordin
  • 3,735
  • 1
  • 35
  • 71
  • In JSON, the "correct" name for this thing seems to be "object", not "dictionary". https://tools.ietf.org/html/rfc8259#section-4 – Jonny Nov 30 '18 at 03:40

3 Answers3

3

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!

Community
  • 1
  • 1
Troy Alford
  • 24,997
  • 8
  • 60
  • 77
2

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."
}

};

simone
  • 105
  • 9
  • I need a JSON...so can pass it cross-platform – Eugene Gordin Jun 18 '15 at 22:52
  • 1
    So the correct structure of needed json is the Troy Alford's one. If you need to serialize it into a NSDictonary take a look here: http://stackoverflow.com/a/10121757/1306216 – simone Jun 19 '15 at 06:47
0

You dont have "dictionaries" in json, but you have key/value arrays which works pretty much like them.

In your code if you want to access:

Normal Cadence > Message you only must do:

Types["NormalCadence"]["Message"] 

Is similar to a .Net Dictionary, but is not exactly a Dictionary

Manjar
  • 2,849
  • 27
  • 39
  • I don't believe this will work, as you're using a property-style accessor to try and get a value out of an array. – Troy Alford Jun 18 '15 at 21:31
  • Yes, because after json is proccess it becames an array in javascript – Manjar Jun 18 '15 at 21:32
  • which language is that? Is it Swift? I'm using Objective-c...in the array JSON case, I have to parse this into a dictionary before I can use it, which is an overhead over the plist way..so I was curious if it's possible to mimic the same with JSON by formatting – Eugene Gordin Jun 18 '15 at 21:33
  • Ahh - my answer may or may not help, then. Perhaps I misunderstood. Languages which serialize into / out of JSON handle dictionaries in their own way. I'm most familiar with .NET's serialization - which is based on how you declare the object serializer itself - and essentially just uses objects. – Troy Alford Jun 18 '15 at 21:47