OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 JSON 中制作字典字典? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:00
标题: ios - 如何在 JSON 中制作字典字典?

现在我在 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."
                  }
               ]
            }
         ]

但我希望它有这样的结构

enter image description here

有人知道如何用 JSON 编写吗?



Best Answer-推荐答案


我相信你的 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 在他们的回答中所说,您可以使用字典样式的语法,例如:

您还可以使用属性访问器语法,例如:

我没有包括每个项目的“类型”的原因是你可以很容易地推断出它来构建你的网格 - 如下:

同样,要获得计数 - 您可以计算特定对象的属性。在现代浏览器中,尝试:

对于较旧的浏览器,请参阅此处的其他方法:How to efficiently count the number of keys/properties of an object in JavaScript?

希望这会有所帮助!

关于ios - 如何在 JSON 中制作字典字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30925963/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4