【问题标题】:Prevent JSON Object Being Sorted by Number Automatically防止 JSON 对象自动按数字排序
【发布时间】:2021-09-23 15:36:03
【问题描述】:

我使用 ajax 从外部 API 获取 JSON 对象。 这是我得到的 JSON 对象:

"pricing_options": {
            "7": {
                "lodging_id": 7,
                "lodging_url": "https://exampleapiurl.com/7",
                "price": 520.0,
                "name": "Private Room w/EnSuite Bath",
                "max_occupancy": 2,
                "image_url": "https://exampleapiurl.com/assets/282/2017/09/Karuna.jpg"
            },
            "552": {
                "lodging_id": 552,
                "lodging_url": "https://exampleapiurl.com/552",
                "price": 400.0,
                "name": "Private Room w/Queen Bed & Hall Bath",
                "max_occupancy": 2,
                "image_url": "https://exampleapiurl.com/assets/282/2019/03/Khandro-300x240.jpg"
            },
            "80": {
                "lodging_id": 80,
                "lodging_url": "https://exampleapiurl.com/80",
                "price": 400.0,
                "name": "Private Room w/Twin Bed & Hall Bath",
                "max_occupancy": 1,
                "image_url": "https://exampleapiurl.com/assets/282/2017/09/Basement-4-e1624825910642.jpg"
            }
        }

但是,它会自动按其 ID(7、80、552)重新排序。我想保持这种状态(以完全相同的顺序)。

【问题讨论】:

标签: json ajax wordpress


【解决方案1】:

JSON 协议没有定义字典条目的顺序。因此,在所有实现中都无法设置或保留特定顺序。

如果要设置顺序,请修改 JSON 架构,使包含这些条目的结构改为列表。例如:

{
   "pricing_options":[
      {
         "lodging_id":7,
         "lodging_url":"https://exampleapiurl.com/7",
         "price":520.0,
         "name":"Private Room w/EnSuite Bath",
         "max_occupancy":2,
         "image_url":"https://exampleapiurl.com/assets/282/2017/09/Karuna.jpg"
      },
      {
         "lodging_id":80,
         "lodging_url":"https://exampleapiurl.com/80",
         "price":400.0,
         "name":"Private Room w/Twin Bed & Hall Bath",
         "max_occupancy":1,
         "image_url":"https://exampleapiurl.com/assets/282/2017/09/Basement-4-e1624825910642.jpg"
      },
      {
         "lodging_id":552,
         "lodging_url":"https://exampleapiurl.com/552",
         "price":400.0,
         "name":"Private Room w/Queen Bed & Hall Bath",
         "max_occupancy":2,
         "image_url":"https://exampleapiurl.com/assets/282/2019/03/Khandro-300x240.jpg"
      },
   ]
}

或者,解析 JSON 并构建以特定顺序维护条目的内部数据结构。

【讨论】:

  • 不幸的是,我无法修改结构,因为它来自第三方服务,我们正在使用他们的 API 来获取数据。有没有办法保持原样?当我直接使用 Chrome 访问 API URL 时,它显示正确的顺序(7、552、80)。但是,当我在 Chrome 控制台上看到日志时,它是按 ID(7、80、552)排序的。
  • 我不知道。为什么需要特定的订购?为什么您的应用程序中不能有一个层来解析此 JSON 并以您的首选格式生成另一个 JSON?如果您只是想在浏览器的本机 JSON 查看器中查看 JSON 有效负载,那么 AFAIK 您将受到该浏览器实现的行为(同样,不是由协议定义)的限制。
  • 我需要保持顺序与它在 JSON 对象上的顺序完全相同,以便我可以在 API 和我的网站之间制作精确的数据副本。
  • 呈现的顺序不需要相同,JSON 是相同的。作为类比,数学中的集合是无序的唯一元素集合。它们用大括号表示,里面有项目。集合{1, 2, 3, 4}{1, 4, 2, 3} 在每个数学意义上都是相同的,尽管表示不同。同样的原则也适用于 JSON。如果您想详细说明您的用例,那么我可能会更好地理解您为什么觉得需要这样做。
猜你喜欢
  • 2016-01-25
  • 2013-12-28
  • 1970-01-01
  • 2018-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多