最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论