AccDa
AccDa

Reputation: 145

Recommended way to lay out JSON if it were to be parsed to a model

I am creating a JSON response which will contain categories, and each category will contain multiple items of that category. Would the following JSON response make sense if it were to be parsed in a model?

[{
    "category":"car",
    "vehicle":[
        {
        "name":"series 1",
        "make":"bmw"
        },     
        {
        "name":"series 2",
        "make":"bmw"
        }               
    ]
},
{
    "category":"lorry",
    "vehicle":[
        {
        "name":"model A19",
        "make":"mercedes benz"
        }               
    ]
}]

Once it has been parsed, I am hoping to then be able to list all the categories, and if a user selects a category they will then see all the items of that specific category.

Upvotes: 0

Views: 124

Answers (1)

hoipolloi
hoipolloi

Reputation: 8044

No, your JSON is invalid. You can validate your JSON for syntax errors here: http://jsonformatter.curiousconcept.com/

Aside from the syntax issues, I think it would make more sense to have an array of vehicles, each with a category field. You may also want to address whether a vehicle can conceivably belong to more than one category (i.e. 'tags' rather than 'categories').

For example:

{  
   "vehicles":[  
      {  
         "name":"model A19",
         "make":"mercedes benz",
         "category":"car"
      },
      {  
         "name":"ram 1500",
         "make":"dodge",
         "category":[  
            "car",
            "truck"
         ]
      }
   ]
}

Upvotes: 1

Related Questions