【发布时间】:2021-09-04 03:13:37
【问题描述】:
我有一个对服务器的 cURL 请求,它返回这个 jsonp 类型的数据,但我不知道为什么 PHP json_decode 在解析这个 JSON 文本时什么都不返回。
这是我收到的请求:
{
"data": {
"name": "Color Collections",
"category": "collection",
"palettes": [
{
"name": "America's Colors",
"category": "collection",
"colors": [
{
"number": "AC-1",
"name": "Coastal Fog",
"family": "Neutral",
"url": "http://www.benjaminmoore.com/en-us/paint-color/ac-1?apiKey=9471009bd36bd062c688",
"shortURL": "/ac-1",
"hex": "CAC4B0",
"r": 202,
"g": 196,
"b": 176,
"exteriorAvailability": "available",
"wetSampleSKU": null,
"drySampleSKU": null,
"eStoreAvailable": false,
"productTypesAvailable": "paint",
"stainOpacitiesAvailable": null
},
],
"colorList": [
"AC-1",
"AC-2",
"AC-3",
"AC-4",
"AC-5",
"AC-6",
"AC-7",
"AC-8",
"AC-9",
"AC-10",
"AC-11",
"AC-12",
"AC-13",
"AC-14",
"AC-15",
"AC-16",
"AC-17",
"AC-18",
"AC-19",
"AC-20",
"AC-21",
"AC-22",
"AC-23",
"AC-24",
"AC-25",
"AC-26",
"AC-27",
"AC-28",
"AC-29",
"AC-30",
"AC-31",
"AC-32",
"AC-33",
"AC-34",
"AC-35",
"AC-36",
"AC-37",
"AC-38",
"AC-39",
"AC-40",
"AC-41",
"AC-42"
],
"code": "AC",
"description": "42 soft hues inspired by the pale gray of our beautiful coastlines to the rich earth tones of our Southwestern deserts. ",
"url": "http://www.benjaminmoore.com/en-us/for-your-home/color-gallery?apiKey=9471009bd36bd062c688#&ce_vm=2&ce_col=AC?apiKey=9471009bd36bd062c688",
"rows": 7,
"totalColors": 42,
"eStoreProductCode": null
},
]
},
"error": "",
"countryCode": "en-us",
"brand": "BenjaminMoore,Corotech,Coronado,Inslx,Lenmar,Maxum"
}
但即使我删除了一些部分以使其工作如此短,但 PHP json_decode 仍然没有返回任何内容。这是我的代码外观并告诉我这里发生了什么?为什么 PHP 不能解析这个?
$jsonData = '{
"data": {
"name": "Color Collections",
"category": "collection",
},
"error": "",
"countryCode": "en-us",
"brand": "BenjaminMoore,Corotech,Coronado,Inslx,Lenmar,Maxum"
}';
print_r( json_decode($jsonData, true));
【问题讨论】:
-
首先转义单引号 "name": "America's Colors" **** 其次删除右花括号中多余的逗号。 “stainOpacitiesAvailable”:null },和“eStoreProductCode”:null },