首页 > 解决方案 > 解析错误:缺少“}”或对象成员名称

问题描述

使用 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 

在我忽略该错误之前它起作用了。

标签: phpjson-ld

解决方案


如果您只查看 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 
}

推荐阅读