最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far