简体   繁体   English

PHP关联数组的JSON模式

[英]JSON schema for PHP associative array

I'm trying to generate JSON from data organized via PHP, and I want to make sure I'm structuring it in a way that makes sense as JSON. 我正在尝试从通过PHP组织的数据生成JSON,并且我想确保以一种与JSON有意义的方式构造它。 Having dealt with data like this more often in PHP than JSON, I think I may be overusing PHP associative arrays / JSON objects with data I'd like to keep ordered for populating a form select box. 在PHP中比JSON处理更多这样的数据时,我认为我可能会过度使用PHP关联数组/ JSON对象中的数据,而这些数据我希望保持顺序以填充表单选择框。

The data involves items that each have an array of associated dates; 数据涉及每个项目都有一组关联的日期; each date has an array of times and each time has a location and URL that may be unique to that date-time combination. 每个日期都有一个时间数组,每个时间都有一个可能是该日期时间组合唯一的位置和URL。 Right now, I'm using dates and times as keys in JSON objects, which is seeming both awkward to manipulate with JS and possibly a problem for keeping the data ordered. 现在,我将日期和时间用作JSON对象中的键,使用JS进行操作似乎很尴尬,而且在保持数据有序方面可能存在问题。 The JSON currently looks like the following: JSON当前如下所示:

{
  "1":{
    "name":"Item 1",
    "dates":{
      "July 21, 2015":{
        "4:30PM":{
          "location":"Place 1",
          "url":"example.com/item1-timed-430"
        }
      },
      "July 22, 2015":{
        "9:30AM":{
          "location":"Earth Hall",
          "url":"example.com/item1-timed-930"
        }
      },
      "July 23, 2015":{
        "9:30AM":{
          "location":"Earth Hall",
          "url":"example.com/item1-timed-930"
        }
      }
    },
    "base_url":"example.com/base-item1"
  },
  "2":{
    "name":"Item 2",
    "dates":{
      "July 21, 2015":{
        "9:30AM":{
          "location":"Place 3",
          "url":"example.com/item2-timed-930"
        },
        "12:30PM":{
          "location":"Place 3",
          "url":"example.com/item2-timed-1230"
        }
      },
      "July 22, 2015":{
        "4:30PM":{
          "location":"Place 1",
          "url":"example.com/item2-timed-430"
        }
      }
    },
    "base_url":"example.com/base-item2"
  }
}

Original questions : First of all - should this be refactored to use JSON lists (and PHP non-associative arrays) if I need chronological order to be preserved? 原始问题 :首先-如果需要保留时间顺序,是否应该将其重构为使用JSON列表(和PHP非关联数组)? It seems to be preserved currently, but I realize that may be different across browsers. 它似乎目前已保留,但我意识到在不同的浏览器中可能有所不同。

Second - how should I refactor? 第二-我应该如何重构? Add to each item a list of dates, a list of times, and use the current dates object as a map of dates to associated times with the unique location and URL data? 向每个项目添加日期列表,时间列表,并使用当前的dates对象作为日期映射到具有唯一位置和URL数据的关联时间? Is there a simpler way to do it? 有更简单的方法吗?

Edited question to specify a coding problem : Can I expect chronological order to be preserved across browsers? 编辑问题以指定编码问题 :我可以期望在浏览器中保留时间顺序吗? If not, what should I change in the data structure to ensure I can access an ordered list of dates and times, along with the specific date-time URL and location data? 如果没有,我应该在数据结构中进行哪些更改以确保我可以访问日期和时间的有序列表以及特定的日期时间URL和位置数据?

This is an opinionated question... 这是一个自以为是的问题...

However, the format of the JSON depends heavily on the application that's going to utilize it. 但是,JSON的格式在很大程度上取决于将要使用它的应用程序。 If it's easier to deal with associative arrays, and looping through them isn't a problem, then go for it. 如果处理关联数组比较容易,并且遍历它们不是问题,那就去解决。 Ideally, your data should be structured in a hierarchical structure that makes sense to the developer and the application. 理想情况下,您的数据应采用对开发人员和应用程序有意义的层次结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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