php - 解析错误:缺少“}”或对象成员名称
问题描述
使用 google web 控制台验证我的网站 json-ld 标记时,会发出以下解析错误:
解析错误:缺少“}”或对象成员名称
这是我的代码,因为当我尝试修复它时,谷歌的展示次数从每天 1 万次下降到 400 次
<script type="application/ld+json">
/* START TEST */
{
"@context" : "https://schema.org/",
"@type" : "JobPosting",
"title" : "{{ $post->title }}",
"description" : "{{ $post->description }}",
/* "description" : "<p>{{ \Illuminate\Support\Str::limit($post->description, 270) }}</p>", */
"identifier": {
"@type": "PropertyValue",
"name": "{{ $post->company_name }}",
"value": "{{ $post->id }}"
},
"datePosted" : "{{ $post->created_at }}",
/*"validThrough" : "2021-08-18T00:00",*/
"employmentType" : "CONTRACTOR",
"hiringOrganization" : {
"@type" : "Organization",
"name" : "{{ $post->company_name }}",
"sameAs" : "{{ $post->company_website }}",
@if (isset($post->company) and !empty($post->company))
<?php $attr = ['countryCode' => config('country.icode'), 'id' => $post->company->id]; ?>
"logo" : "{{ imgUrl($post->logo, 'medium') }}"
@else
"logo" : "{{ imgUrl($post->logo, 'medium') }}"
@endif
},
"jobLocation": {
"@type": "Place",
"address": {
"@type": "PostalAddress",
"addressLocality": "{{ $post->city->name }}"
}
},
@if ($post->salary_min > 0 or $post->salary_max > 0)
@if ($post->salary_min > 0)
"baseSalary": {
"@type": "MonetaryAmount",
"currency": "USD",
"value": {
"@type": "QuantitativeValue",
"value": {{$post->salary_max}},
@if (!empty($post->salaryType))
"unitText": "{{ $post->salaryType->name }}"
@else {
"unitText": "MONTH"
}
@endif
}
}
@else if ($post->salary_max > 0)
"baseSalary": {
"@type": "MonetaryAmount",
"currency": "USD",
"value": {
"@type": "QuantitativeValue",
"value": {{$post->salary_max}},
@if (!empty($post->salaryType))
"unitText": "{{ $post->salaryType->name }}"
@else {
"unitText": "MONTH"
}
@endif
}
}
@endif
@endif
在我忽略该错误之前它起作用了。
解决方案
如果您只查看 JSON 并删除所有 PHP 并通过 JSON 验证器对其进行解析,您将意识到错误在于您没有关闭 JSON 块。如果您删除 PHP 并替换"value": {{$post->salary_max}},
为"value": "{{$post->salary_max}}",
JSON 验证器能够吃掉它。
{
"@context" : "https://schema.org/",
"@type" : "JobPosting",
"title" : "{{ $post->title }}",
"description" : "{{ $post->description }}",
"identifier": {
"@type": "PropertyValue",
"name": "{{ $post->company_name }}",
"value": "{{ $post->id }}"
},
"datePosted" : "{{ $post->created_at }}",
"employmentType" : "CONTRACTOR",
"hiringOrganization" : {
"@type" : "Organization",
"name" : "{{ $post->company_name }}",
"sameAs" : "{{ $post->company_website }}",
"logo" : "{{ imgUrl($post->logo, 'medium') }}"
},
"jobLocation": {
"@type": "Place",
"address": {
"@type": "PostalAddress",
"addressLocality": "{{ $post->city->name }}"
}
},
"baseSalary": {
"@type": "MonetaryAmount",
"currency": "USD",
"value": {
"@type": "QuantitativeValue",
"value": "{{$post->salary_max}}",
"unitText": "{{ $post->salaryType->name }}"
}
}
您将得到预期的错误}
,或者,
得到 EOF。原因是你错过了}
第一次的结束{
。
最终的 JSON 应该如下所示:
{
"@context" : "https://schema.org/",
"@type" : "JobPosting",
"title" : "{{ $post->title }}",
"description" : "{{ $post->description }}",
"identifier": {
"@type": "PropertyValue",
"name": "{{ $post->company_name }}",
"value": "{{ $post->id }}"
},
"datePosted" : "{{ $post->created_at }}",
"employmentType" : "CONTRACTOR",
"hiringOrganization" : {
"@type" : "Organization",
"name" : "{{ $post->company_name }}",
"sameAs" : "{{ $post->company_website }}",
"logo" : "{{ imgUrl($post->logo, 'medium') }}"
},
"jobLocation": {
"@type": "Place",
"address": {
"@type": "PostalAddress",
"addressLocality": "{{ $post->city->name }}"
}
},
"baseSalary": {
"@type": "MonetaryAmount",
"currency": "USD",
"value": {
"@type": "QuantitativeValue",
"value": "{{$post->salary_max}}",
"unitText": "{{ $post->salaryType->name }}"
}
}
}
因此,您的代码应更改为包括:
{
"@context" : "https://schema.org/",
"@type" : "JobPosting",
"title" : "{{ $post->title }}",
"description" : "{{ $post->description }}",
/* "description" : "<p>{{ \Illuminate\Support\Str::limit($post->description, 270) }}</p>", */
"identifier": {
"@type": "PropertyValue",
"name": "{{ $post->company_name }}",
"value": "{{ $post->id }}"
},
"datePosted" : "{{ $post->created_at }}",
/*"validThrough" : "2021-08-18T00:00",*/
"employmentType" : "CONTRACTOR",
"hiringOrganization" : {
"@type" : "Organization",
"name" : "{{ $post->company_name }}",
"sameAs" : "{{ $post->company_website }}",
@if (isset($post->company) and !empty($post->company))
<?php $attr = ['countryCode' => config('country.icode'), 'id' => $post->company->id]; ?>
"logo" : "{{ imgUrl($post->logo, 'medium') }}"
@else
"logo" : "{{ imgUrl($post->logo, 'medium') }}"
@endif
},
"jobLocation": {
"@type": "Place",
"address": {
"@type": "PostalAddress",
"addressLocality": "{{ $post->city->name }}"
}
},
@if ($post->salary_min > 0 or $post->salary_max > 0)
@if ($post->salary_min > 0)
"baseSalary": {
"@type": "MonetaryAmount",
"currency": "USD",
"value": {
"@type": "QuantitativeValue",
"value": {{$post->salary_max}},
@if (!empty($post->salaryType))
"unitText": "{{ $post->salaryType->name }}"
@else {
"unitText": "MONTH"
}
@endif
}
}
@else if ($post->salary_max > 0)
"baseSalary": {
"@type": "MonetaryAmount",
"currency": "USD",
"value": {
"@type": "QuantitativeValue",
"value": {{$post->salary_max}},
@if (!empty($post->salaryType))
"unitText": "{{ $post->salaryType->name }}"
@else {
"unitText": "MONTH"
}
@endif
}
}
@endif
@endif
}
推荐阅读
- cplex - 如何将 Cplex 设置为仅具有原始单纯形求解模型?
- python-3.x - TypeError: 不支持的操作数类型 -: 'str' 和 'DateOffset'
- node.js - 在 Heroku 服务器中部署 Node/Angular 应用程序时出错
- pycharm - 使用 Pycharm 对栅格值进行多边形化
- python - 来自dict_keys的jinja2打印密钥
- angular - 如何使用 AWS Transcribe javascript sdk
- excel - 如果整行使用 VBA 匹配,则删除重复的行
- jquery - 如何将复选框的状态作为变量获取到 ajax 对象中
- javascript - 为什么我的反应网络应用程序返回一个白色/空白页面?
- python-3.x - 其他语言的 pyenchant