douyou1857 2014-04-09 14:44
浏览 71

在服务器上存储嵌套的JSON

I have (mock) product data hard coded into a JSON file that I use to generate "catalogue" pages in my application. However, the ultimate plan is to allow users to add new products via a CMS style admin panel.

Obviously I need this data to be persistent. The obvious choice would be to use MySQL or some other database, but since this a a pretty complex data structure with lots of nesting I can't imagine how that could be achieved painlessly. I could simply store the JSON in a text file on the server but then I lose the ability to manipulate it and have to send the whole set each time I make a change. Then again that would only be when people added and removed products so perhaps it wouldn't be so bad. Perhaps I could decode the JSON with PHP, manipulated it and then re-serialize it into JSON and save it into a text file.

Sorry, I'm rambling here. What do you think are my best options?

{
        currency: currency,
        currencySymbol: CurrencySymbol(currency),
        shoes: {
            title: 'Shoes',
            items:[ 
                [
                    {
                        name: 'Light Shoes', 
                        price: 99.99, 
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/shoes/darkshoes.jpg'
                    },{
                        name: 'Dark Shoes', 
                        price: 59.99, 
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/shoes/darkshoes.jpg'
                    },{
                        name: 'Brown Shoes',
                        price: 5.99,
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/shoes/darkshoes.jpg'
                    }
                ],[
                    {
                        name: 'Gold Shoes',
                        price: 999.99,
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/shoes/darkshoes.jpg'
                    },{
                        name: 'Dark Shoes', 
                        price: 59.99, 
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/shoes/darkshoes.jpg'
                    },{
                        name: 'Brown Shoes',
                        price: 5.99,
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/shoes/darkshoes.jpg'
                    }
                ]
            ]
        },
        dance: {
            title: 'Dance Gear',
            items: [ 
                [
                    {
                        name: 'Cool Cap', 
                        price: 99.99, 
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/dance/dance.jpg'
                    },{
                        name: 'Baggy Pants', 
                        price: 59.99, 
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/dance/dance.jpg'
                    },{
                        name: 'Jacket',
                        price: 5.99,
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/dance/dance.jpg'
                    }
                ],[
                    {
                        name: 'T-Shirt',
                        price: 99.99,
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/dance/dance.jpg'
                    },{
                        name: 'Hip-Hip Hoodie', 
                        price: 59.99, 
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/dance/dance.jpg'
                    },{
                        name: 'Belt',
                        price: 5.99,
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/dance/dance.jpg'
                    }
                ]
            ]

        }
    }
  • 写回答

1条回答 默认 最新

  • dongle3217 2014-04-09 14:56
    关注

    Store json objects with a .json extension. No need for a text file here, and therefore no need to keep struggling with sending the correct headers.

    For the persistence part: In your database table only reference the json files on the local file system along with other information you need (time it was updated etc.).

    A sample table could look like this:

    documents(id:pk, file, created_at, updated_at)

    (Of course you could also store the raw json string in your table but you would run into danger using a data-type that is not suited to store the json and therefore cannot hold all of the data)

    However this approach would only be suitable when you need to use relational database systems


    However, a better approach would be to use document database systems such as mongoDB which also has been recommended in the comments. It's definitely worth a look, but it can be a bit awkward at first if you are coming from a relational dbms background.

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?