【问题标题】:How to convert JSON string into array PHP如何将 JSON 字符串转换为数组 PHP
【发布时间】:2018-02-27 23:31:15
【问题描述】:

如何将此 json 字符串转换为数组?我试过json_decode,但它返回null。

它以前是一个 .json 文件,我使用 file_get_contents 获取它的数据。

我也尝试过使用 foreach 循环,但 PHP 库应该可以提供答案。

我还从 stackoverflow 中查看了另一个相关主题,但没有成功。

谢谢!

{
   "1":{
      "entity_id":"1",
      "type_id":"simple",
      "sku":"dress_test",
      "status":"1",
      "visibility":"4",
      "tax_class_id":"2",
      "weight":"1.0000",
      "price":"1500.0000",
      "special_price":"1000.0000",
      "name":"Wedding dress",
      "url_key":"dress",
      "country_of_manufacture":"AO",
      "msrp_display_actual_price_type":"2",
      "gift_message_available":"1",
      "news_from_date":"2012-03-21 00:00:00",
      "news_to_date":"2012-03-24 00:00:00",
      "special_from_date":"2012-03-21 00:00:00",
      "special_to_date":"2012-03-24 00:00:00",
      "description":"White wedding dress"
   },
   "2":{
      "entity_id":"2",
      "type_id":"simple",
      "sku":"blazers",
      "status":"1",
      "visibility":"4",
      "tax_class_id":"2",
      "weight":"1.0000",
      "price":"200.0000",
      "special_price":"100.0000",
      "name":"Brazes",
      "url_key":"brazes",
      "country_of_manufacture":"AO",
      "msrp_display_actual_price_type":"2",
      "gift_message_available":"1",
      "news_from_date":"2012-03-21 00:00:00",
      "news_to_date":"2012-03-24 00:00:00",
      "special_from_date":"2012-03-21 00:00:00",
      "special_to_date":"2012-03-24 00:00:00",
      "description":"Blue brazes"
   },
   "3":{
      "entity_id":"3",
      "type_id":"simple",
      "sku":"black_sunglasses",
      "status":"1",
      "visibility":"4",
      "tax_class_id":"2",
      "weight":"0.2000",
      "price":"500.0000",
      "special_price":"300.0000",
      "name":"Sunglasses",
      "url_key":"sunglasses",
      "country_of_manufacture":"AR",
      "msrp_display_actual_price_type":"2",
      "gift_message_available":null,
      "news_from_date":null,
      "news_to_date":null,
      "special_from_date":"2012-03-21 00:00:00",
      "special_to_date":"2012-03-24 00:00:00",
      "description":"Black sunglasses"
   },
     "4":{
      "entity_id":"4",
      "type_id":"simple",
      "sku":"jacket",
      "status":"1",
      "visibility":"4",
      "tax_class_id":"2",
      "weight":"1.0000",
      "price":"800.0000",
      "name":"Jacket",
      "url_key":"jacket",
      "country_of_manufacture":"AO",
      "msrp_display_actual_price_type":"2",
      "gift_message_available":"1",
      "news_from_date":"2012-03-21 00:00:00",
      "news_to_date":"2012-03-24 00:00:00",
      "special_from_date":"2012-03-21 00:00:00",
      "special_to_date":"2012-03-24 00:00:00",
      "description":"Black jacket"
   },
     "5":{
      "entity_id":"5",
      "type_id":"simple",
      "sku":"green_shirts",
      "status":"0",
      "visibility":"4",
      "tax_class_id":"2",
      "weight":"1.0000",
      "price":"800.0000",
      "name":"Green shirts",
      "url_key":"green_shirts",
      "country_of_manufacture":"AO",
      "msrp_display_actual_price_type":"2",
      "gift_message_available":"1",
      "news_from_date":"2012-03-21 00:00:00",
      "news_to_date":"2012-03-24 00:00:00",
      "special_from_date":"2012-03-21 00:00:00",
      "special_to_date":"2012-03-24 00:00:00",
      "description":"Green shirts"
   },"6":{
      "entity_id":"6",
      "type_id":"simple",
      "sku":"orange_shirts",
      "status":"1",
      "visibility":"4",
      "tax_class_id":"2",
      "weight":"1.0000",
      "price":"800.0000",
      "name":"Orange shirts",
      "url_key":"orange",
      "country_of_manufacture":"AO",
      "msrp_display_actual_price_type":"2",
      "gift_message_available":"1",
      "news_from_date":"2012-03-21 00:00:00",
      "news_to_date":"2012-03-24 00:00:00",
      "special_from_date":"2012-03-21 00:00:00",
      "special_to_date":"2012-03-24 00:00:00",
      "description":"Orange shirts",
   }
}

【问题讨论】:

标签: php html json


【解决方案1】:

您的最后一行 json 有尾随逗号 (,),这使您的 json 无效。从

中删除该逗号

"description":"Orange shirts",

来自 jsonlint 的错误

Error: Parse error on line 123:
...: "Orange shirts",   }}
----------------------^
Expecting 'STRING', got '}'

尝试在 https://jsonlint.com/ 上验证您的 json

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    • 2017-07-20
    相关资源
    最近更新 更多