admin管理员组文章数量:1434363
So I'm currently trying to develop a JSON document which will be easiest to iterate through and access objects from. I will be using underscore, so accessing objects isn't necessarily difficult, but I'm wondering what the best way of going about formatting is - best practices, etc.
Here're the two formats I'm considering: The first is an array
{
"defaultViews" : [
{
"name" : "view1",
"title" : "View 1"
},
{
"name" : "view2",
"title" : "View 2"
}
]
}
Or, the other way, which is more.... "object oriented"...
{
"defaultViews" : {
"view1" : {
"title" : "View 1"
},
"view2" : {
"title" : "View 2"
}
}
}
So in the first example it's easier to iterate through the objects, whereas in the second example it's easier to access the object directly (obj.defaultViews.view1
). I suppose this is quite subjective, but again I'm looking for what's hopefully considered a "best practice". Thanks!
So I'm currently trying to develop a JSON document which will be easiest to iterate through and access objects from. I will be using underscore, so accessing objects isn't necessarily difficult, but I'm wondering what the best way of going about formatting is - best practices, etc.
Here're the two formats I'm considering: The first is an array
{
"defaultViews" : [
{
"name" : "view1",
"title" : "View 1"
},
{
"name" : "view2",
"title" : "View 2"
}
]
}
Or, the other way, which is more.... "object oriented"...
{
"defaultViews" : {
"view1" : {
"title" : "View 1"
},
"view2" : {
"title" : "View 2"
}
}
}
So in the first example it's easier to iterate through the objects, whereas in the second example it's easier to access the object directly (obj.defaultViews.view1
). I suppose this is quite subjective, but again I'm looking for what's hopefully considered a "best practice". Thanks!
- You've answered to your question yourself: "So in the first example it's easier to iterate through the objects, whereas in the second example it's easier to access the object directly (obj.defaultViews.view1)." – Marat Tanalin Commented Feb 1, 2013 at 7:20
- Yes I suppose haha, so there's no real standard for this? – iLoch Commented Feb 1, 2013 at 7:23
-
Exact format generally depends on your goal. Also, second notation is applicable only when keys (
view1
,view2
) are unique while in first notation there is not such limitation. – Marat Tanalin Commented Feb 1, 2013 at 7:25 - Soliciting opinions about code design belong in codereview.stackexchange., not here. – jfriend00 Commented Feb 1, 2013 at 7:26
- 'more.... "object oriented"' - I can't tell if you mean that as a joke, or if you think it somehow follows "object oriented" coding principles? The first way is perfectly fine from an OO point of view. (By the way, you don't iterate through JSON, you parse the JSON and iterate through the resulting object/array.) – nnnnnn Commented Feb 1, 2013 at 7:37
1 Answer
Reset to default 1there is nothing wrong with using an array in object orientation principles. also the first examples that uses an array is a better approach because what you actually want is an array
本文标签:
版权声明:本文标题:javascript - How to format JSON: Array of objects, or document containing elements with objects? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745637400a2667643.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论