简体   繁体   English

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

[英]How to convert JSON string into array PHP

How can I convert this json string into an array? 如何将这个json字符串转换为数组? I tried json_decode, but it returns null. 我尝试了json_decode,但它返回null。

It was previously a .json file that i got its data using file_get_contents. 以前是.json文件,我使用file_get_contents获取了数据。

I also tried using an foreach loop butPHP libraries should provide an answer. 我也尝试过使用foreach循环,但是PHP库应该提供答案。

I also have look another related topics from stackoverflow without success. 我也从stackoverflow看了另一个相关主题,但没有成功。

Thanks! 谢谢!

{
   "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",
   }
}

Your last line of json has trailing comma(,), which is making your json invalid. json的最后一行末尾有逗号(,),这使json无效。 Remove that comma from 从中删除该逗号

"description":"Orange shirts",

The error from jsonlint 来自jsonlint的错误

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

try to validate your json on https://jsonlint.com/ 尝试在https://jsonlint.com/上验证您的json

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

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