如何将JSON字符串转换为数组PHP

时间:2018-02-27 23:31:16

标签: php html json

如何将此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",
   }
}

1 个答案:

答案 0 :(得分:3)

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

中删除该逗号

"description":"Orange shirts",

jsonlint的错误

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

尝试在https://jsonlint.com/

上验证您的json