简体   繁体   中英

How to implement this json feed into a database structure?

I am currently working on an app where I am adding stores from which customers can buy a product. Now I have a json feed with stores which I can pull all the store's data from.

Fields like phone, postcode and address are standard and easy to implement. But the json feed also gives me an array of weekdays when the store will open/close.

I don't have experience with implement something like this into a database. How would I go about implement this? I think just dumping the array in a single field is probably a bad idea but than again I won't have to idea it since it is only being update from the json feed via api calls.

I would probably create something like an opening_hours table. I would probably use:

  • store_id
  • weekday
  • opening_time
  • closing_time

But each store would have their weekdays of 1,2,3,4,5,6 corresponding with monday till saturday. This would probably cause a lot of repeat data but I guess there is simply no way around this?

Hopefully somebody can guide me in the right direction.

Example of the json feed:

openinghours: [
    {
        closing: "18:00",
        weekday: 0,
        opening: "12:00"
    },
    {
        closing: "18:00",
        weekday: 1,
        opening: "09:00"
    },
    {
        closing: "18:00",
        weekday: 2,
        opening: "09:00"
    },    
    {
        closing: "18:00",
        weekday: 3,
        opening: "09:00"
    },    
    {
        closing: "18:00",
        weekday: 4,
        opening: "09:00"
    },    
    {
        closing: "17:30",
        weekday: 5,
        opening: "09:00"
    },        
    {
        closing: null,
        weekday: 6,
        opening: null
    }
],
lng: 4.6217623,
id: 5014

First, do you need to store this data for your app? It's perfectly acceptable to ignore opening/closing hours if you are never going to use them.

If you are going to use this data, will it be for display only? If so, you could just store the JSON array in its original format in an "hours" field for your stores table, and decode it when needed. If the data needs to be searchable , then yes, you are probably going to create a one-to-many table as you outlined above. Alternatively, you could create a table with columns for each day:

store_id  mon_op  mon_clos  tue_op  tue_clos . . .  sat_op  sat_clos

This would avoid the one-to-many problem, but really isn't that much clearer. Whichever format you choose, however, I would still recommend converting the hour string into an appropriate date/time format, for easy searching.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM