javascript - How to format JSON: Array of objects, or document containing elements with objects? - Stack Overflow

So I'm currently trying to develop a JSON document which will be easiest to iterate through and ac

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!

Share Improve this question asked Feb 1, 2013 at 7:16 iLochiLoch 7693 gold badges13 silver badges33 bronze badges 5
  • 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
Add a ment  | 

1 Answer 1

Reset to default 1

there 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745637400a4637478.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信