识别 JSON 内容
识别 JSON 内容
我有以下数据:
{
total: "156",
list: [
{
"nodeRef": "workspace://SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67",
"id": "e364714d-14bc-4e13-bfff-c1f86a8cbe67",
"name": "Morning Class_Dadi Janki_29-05-12_H_London.mp4",
"mimetype": "video/mp4",
"title" : "Morning Class" ,
"author": "Dadi Janki",
"class_date": "May 29, 2012 12:00:00 AM",
"created": "May 29, 2012 12:32:44 PM",
"size": "97,156,420",
"lang": "h",
"totalViews": "11",
"totalDownloads": "0",
"downloadUrl": "/d/a/workspace/SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67/Morning%20Class_Dadi%20Janki_29-05-12_H_London.mp4"
}
]
}
当我尝试使用var_dump对其进行打印时,结果为null。我如何知道这些数据是否已经进行了JSON编码?
编辑:
以下是代码:
我通过get_contents从url获取上述内容
$url = ""; // URL
$contents = file_get_contents($url);
$data = json_decode($contents);
var_dump($data);
识别JSON内容的问题可能出现的原因是JSON格式不正确。为了解决这个问题,可以使用以下方法:首先,可以使用JSON验证器和格式化工具来检查JSON是否有效。可以尝试使用以下JSON验证器:https://jsonformatter.curiousconcept.com/ 和 https://www.jsonlint.org/。其次,在代码中可以使用json_last_error函数来查找错误。另外,使用JSON的编码和解码功能可以对URL值进行处理。以下是一个有效的JSON示例:
{
"total":"156",
"list":[
{
"nodeRef":"workspace://SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67",
"id":"e364714d-14bc-4e13-bfff-c1f86a8cbe67",
"name":"Morning Class_Dadi Janki_29-05-12_H_London.mp4",
"mimetype":"video/mp4",
"title":"Morning Class",
"author":"Dadi Janki",
"class_date":"May 29, 2012 12:00:00 AM",
"created":"May 29, 2012 12:32:44 PM",
"size":"97,156,420",
"lang":"h",
"totalViews":"11",
"totalDownloads":"0",
"downloadUrl":"/d/a/workspace/SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67/Morning%20Class_Dadi%20Janki_29-05-12_H_London.mp4"
}
]
}
以下是JSON的有效格式:
{
"total":"156",
"list":[
{
"nodeRef":"workspace://SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67",
"id":"e364714d-14bc-4e13-bfff-c1f86a8cbe67",
"name":"Morning Class_Dadi Janki_29-05-12_H_London.mp4",
"mimetype":"video/mp4",
"title":"Morning Class",
"author":"Dadi Janki",
"class_date":"May 29, 2012 12:00:00 AM",
"created":"May 29, 2012 12:32:44 PM",
"size":"97,156,420",
"lang":"h",
"totalViews":"11",
"totalDownloads":"0",
"downloadUrl":"/d/a/workspace/SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67/Morning%20Class_Dadi%20Janki_29-05-12_H_London.mp4"
}
]
}
问题的出现原因是JSON内容的格式不符合标准,属性名没有加双引号。
解决方法是使用正则表达式将属性名加上双引号,然后使用json_decode函数将字符串转换为JSON对象。
以下是整理后的
在PHP中,我们经常需要处理JSON数据。然而,有时候我们可能会遇到一些格式错误的JSON数据,例如属性名没有加双引号。这样的数据在使用json_decode函数转换时会出现错误。
下面的代码展示了一个包含错误格式的JSON字符串的例子:
$str = '{ total: "156", list: [ { "nodeRef": "workspace://SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67", "id": "e364714d-14bc-4e13-bfff-c1f86a8cbe67", "name": "Morning Class_Dadi Janki_29-05-12_H_London.mp4", "mimetype": "video/mp4", "title" : "Morning Class" , "author": "Dadi Janki", "class_date": "May 29, 2012 12:00:00 AM", "created": "May 29, 2012 12:32:44 PM", "size": "97,156,420", "lang": "h", "totalViews": "11", "totalDownloads": "0", "downloadUrl": "/d/a/workspace/SpacesStore/e364714d-14bc-4e13-bfff-c1f86a8cbe67/Morning%20Class_Dadi%20Janki_29-05-12_H_London.mp4" } ] }';
为了解决这个问题,我们可以使用正则表达式将属性名加上双引号。下面的代码展示了如何使用preg_replace函数来实现这一点:
$str = preg_replace('#([^\s\"]+): #is', '"\\1": ', $str); echo $str;
运行以上代码后,我们得到了一个修复了属性名格式的JSON字符串。
接下来,我们可以使用json_decode函数将修复后的JSON字符串转换为JSON对象。下面的代码展示了如何使用json_decode函数来实现这一点:
$str = json_decode($str);
现在,我们可以通过对象的属性来访问JSON数据中的值。例如,要访问list数组中第一个对象的title属性,我们可以使用以下代码:
$title = $str->list[0]->title;
如果我们想要将JSON对象转换为关联数组,我们可以将json_decode函数的第二个参数设置为true。下面的代码展示了如何将JSON对象转换为关联数组,并访问list数组中第一个对象的title属性:
$str = json_decode($str, true); $title = $str['list'][0]['title'];
通过以上的方法,我们可以正确地访问JSON数据中的属性值,而不会因为格式错误而导致错误。