dqjmq28248 2013-10-28 22:33
浏览 60
已采纳

网站导航树作为JSON - 这是不好的做法? [关闭]

I have to implement x-level deep navigation menu for a website. After I spent few hours trying to come up with a good way for storing, manipulating, and displaying this tree, I decided to go with JSON over MySQL. I'd either put it in a file, or in a field in a MySQL (Tho I think it may be an overkill since it is just one row with one field).

I am using jQuery nestable list plugin, and the reason why I'd like to go with JSON is that I can directly manipulate items data as they are DOM elements (which I find much easier than passing ID's and taking care of which elements are for deletion, which should be updated, and which should be added), and get serialized output, save it "as is" in a file, and when it comes to laying it out into horizontal multi-level dropdown menu, I can do it with help of PHP respecting items orders and nesting made by plugin.

Is this bad practice for some reason? Performance wise?

  • 写回答

1条回答 默认 最新

  • douzhaiya3968 2013-10-29 01:33
    关注

    There are some things to be aware of:

    1) Storing tree structure using MySQL isn't really that straight-forward

    2) But database engine takes care for some elementary locking (think about what happens if two users are writing tree structure to the file at the same exact time)

    3) What happens if you serialize and store structure "as is", but html and/or java-script structure changes? Will it still work? Most likely you would have to write a function to convert all serialized data to be compatible with new structure.

    4) Performance wise it really doesn't make any difference whether you use json + files / json + mysql / only mysql - those couple of milliseconds won't make any difference. Keep your code logical, readable and easily maintainable - is most cases it is cheaper to upgrade your hardware rather than to maintain and develop "hacky" code.

    To sum it up, it seems to be OK to store this kind of data as json, just be aware of some possible problems mentioned above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)