首頁  >  問答  >  主體

php - JSON資料格式化以後,資料順序改變了

{"Code":"0","Result":{"Orders":{"171865304":{"LastId":30351,"Time":"2017-06-28T14:56:28+08:00","OrderId":171865304,"Status":-6,"PayStatus":-1,"ArrivalDate":"2017-07-18T00:00:00+08:00","DepartureDate":"2017-07-19T00:00:00+08:00","TotalPrice":8,"NumberOfRooms":1,"AffiliateConfirmationId":"1326","OriginStatus":"D"},"171865316":{"LastId":30360,"Time":"2017-06-28T15:02:20+08:00","OrderId":171865316,"Status":-6,"PayStatus":-1,"ArrivalDate":"2017-07-18T00:00:00+08:00","DepartureDate":"2017-07-19T00:00:00+08:00","TotalPrice":8,"NumberOfRooms":1,"AffiliateConfirmationId":"1327","OriginStatus":"D"},"171865325":{"LastId":30361,"Time":"2017-06-28T15:05:05+08:00","OrderId":171865325,"Status":-6,"PayStatus":-1,"ArrivalDate":"2017-07-18T00:00:00+08:00","DepartureDate":"2017-07-19T00:00:00+08:00","TotalPrice":8,"NumberOfRooms":1,"AffiliateConfirmationId":"1328","OriginStatus":"D"},"171865326":{"LastId":30362,"Time":"2017-06-28T15:05:59+08:00","OrderId":171865326,"Status":-6,"PayStatus":-1,"ArrivalDate":"2017-07-18T00:00:00+08:00","DepartureDate":"2017-07-19T00:00:00+08:00","TotalPrice":8,"NumberOfRooms":1,"AffiliateConfirmationId":"1329","OriginStatus":"D"},"171865327":{"LastId":30363,"Time":"2017-06-28T15:11:32+08:00","OrderId":171865327,"Status":-6,"PayStatus":-1,"ArrivalDate":"2017-07-18T00:00:00+08:00","DepartureDate":"2017-07-19T00:00:00+08:00","TotalPrice":8,"NumberOfRooms":1,"AffiliateConfirmationId":"1330","OriginStatus":"D"},"171865294":{"LastId":30378,"Time":"2017-06-28T15:37:05+08:00","OrderId":171865294,"Status":-6,"PayStatus":-1,"ArrivalDate":"2017-06-28T00:00:00+08:00","DepartureDate":"2017-06-29T00:00:00+08:00","TotalPrice":70,"NumberOfRooms":1,"AffiliateConfirmationId":"314243799193660","OriginStatus":"D"},"171865125":{"LastId":30440,"Time":"2017-06-28T16:57:08+08:00","OrderId":171865125,"Status":-6,"PayStatus":-1,"ArrivalDate":"2017-07-06T00:00:00+08:00","DepartureDate":"2017-07-07T00:00:00+08:00","TotalPrice":601,"NumberOfRooms":1,"AffiliateConfirmationId":"H170627110452902933","OriginStatus":"D"},"171865382":{"LastId":30485,"Time":"2017-06-28T18:26:32+08:00","OrderId":171865382,"Status":-6,"PayStatus":-1,"ArrivalDate":"2017-07-18T00:00:00+08:00","DepartureDate":"2017-07-19T00:00:00+08:00","TotalPrice":8,"NumberOfRooms":1,"AffiliateConfirmationId":"1331","OriginStatus":"D"}}},"Guid":"afcbafd4-0831-43bf-8daf-6a605f0fcc05"}

上面是JSON的raw資料。
我是用postman請求的,然後格式化以後發現陣列的順序發生了改變,因為我是處理一個陣列的排序問題,檢查了很久的程式碼發現沒問題,然後發現是格式化出錯了。請問一下這大概是什麼原因導致的。
下面是經過格式化後的截圖,明顯的原始碼裡第一個值是171865304

#
曾经蜡笔没有小新曾经蜡笔没有小新2690 天前1002

全部回覆(2)我來回復

  • 高洛峰

    高洛峰2017-07-01 09:13:58

    因為你這是一個對象,對象裡都是鍵值對,順序是隨機的。具體的順序如何是瀏覽器自己決定的。如果想要有序,建議將此類物件變為如下格式數組:

    [
        { .... },
        { .... },
        { .... },
        { .... }
    ]

    回覆
    0
  • 天蓬老师

    天蓬老师2017-07-01 09:13:58

    因為你的Orders是個對象,不是個陣列啊

    回覆
    0
  • 取消回覆