【问题标题】:JSON do not add the last commaJSON 不添加最后一个逗号
【发布时间】:2017-12-04 19:28:04
【问题描述】:

我有脚本:

<script type="application/ld+json">
            {
              "@context": "http://schema.org",
              "@type": "Product",
              "aggregateRating": {
                "@type": "AggregateRating",

                "ratingValue": "<?php echo $rating;?>",



              "reviewCount": "<?php echo $review_total; ?>"
              },

              "description": "<?php echo $description; ?>",

              "name": "<?php echo $heading_title; ?>",


              "image": "<?php echo $thumb; ?>",           
              "review": [        
                <?php foreach($reviewss as $review) {  ?>
                {
                  "@type": "Review",
                  "author": "<?php echo $review['author'];?>",
                  "datePublished": "<?php echo $review['date_added'];?>",
                  "description": "<?php echo $review['text'];?>",
                  "name": "<?php echo $review['author'];?>",
                  "reviewRating": {
                    "@type": "Rating",
                    "bestRating": "5",
                    "ratingValue": "<?php echo $review['rating'];?>",
                    "worstRating": "1"
                    }

                    }     
                <?php } ?>
                ]

            }
        </script>

如果我只有一条评论,它工作正常。 如果我有几条评论,我需要在每条评论后添加逗号:

{
                  "@type": "Review",
                  "author": "<?php echo $review['author'];?>",
                  "datePublished": "<?php echo $review['date_added'];?>",
                  "description": "<?php echo $review['text'];?>",
                  "name": "<?php echo $review['author'];?>",
                  "reviewRating": {
                    "@type": "Rating",
                    "bestRating": "5",
                    "ratingValue": "<?php echo $review['rating'];?>",
                    "worstRating": "1"
                    }

                    },

但是在最后一个值之后,逗号应该被删除。 有人可以帮忙吗?如何去掉最后一个逗号?

【问题讨论】:

  • 请参阅this 答案,了解如何在使用foreach 循环时确定您是否位于数组的最后一个元素上。然后让, 反映这一点。
  • 永远不要手动创建 json。它很容易出错,需要额外的工作,并且很容易编码完整的数组

标签: javascript php arrays json regex


【解决方案1】:

你应该使用json_encode()而不是自己生成json

抱歉花了这么长时间,不得不剖析你的 json。

<?php
// your arbitrary vars
$rating = 1;
$review_total = 100;
$description = 'How do I not break json';
$heading_title = 'As above...';
$thumb = 'http://.../thumb.jpg';

$json = [
    '@context' => 'http://schema.org',
    '@type' => 'Product',
    'aggregateRating' => [
        [
            '@type' => 'AggregateRating',
            'ratingValue' => $rating,
            'reviewCount' => $review_total,
        ]
    ],
    'description' => $description,
    'name' => $heading_title,
    'image' => $thumb,
    'review' => [],
];

// query reviews from db
$reviews = [
    [
        'author' => 'Lawrence Cherone',
        'date_added' => '11/11/17 00:00:00',
        'text' => 'You should instead use json_encode',
        'rating' => 1
    ],
];

foreach ($reviews as $review) {
    $json['review'][] = [
        '@type' => 'Review',    
        'author' => $review['author'],       
        'datePublished' => $review['date_added'],    
        'description' => '',    
        'name' => $review['author'], 
        'reviewRating' => [
            '@type' => 'Rating',
            'bestRating' => 5,
            'ratingValue' => $review['rating'],
            'worstRating' => 1
        ]
    ];
}
?>

<script type="application/ld+json">
<?= json_encode($json, JSON_PRETTY_PRINT) ?>
</script>

结果将是完美有效的 json。

<script type="application/ld+json">
{
    "@context": "http:\/\/schema.org",
    "@type": "Product",
    "aggregateRating": [
        {
            "@type": "AggregateRating",
            "ratingValue": 1,
            "reviewCount": 100
        }
    ],
    "description": "How do I not break json",
    "name": "As above...",
    "image": "http:\/\/...\/thumb.jpg",
    "review": [
        {
            "@type": "Review",
            "author": "Lawrence Cherone",
            "datePublished": "11\/11\/17 00:00:00",
            "description": "",
            "name": "Lawrence Cherone",
            "reviewRating": {
                "@type": "Rating",
                "bestRating": "5",
                "ratingValue": 1,
                "worstRating": "1"
            }
        }
    ]
}</script>

https://3v4l.org/u0Fd3

【讨论】:

    【解决方案2】:
      "review": [        
                    <?php $i=0; foreach($reviewss as $review) { ?>
                    {
                      "@type": "Review",
                      "author": "<?php echo $review['author'];?>",
                      "datePublished": "<?php echo $review['date_added'];?>",
                      "description": "<?php echo strip_tags($review['text']);?>",
                      "name": "<?php echo $review['author'];?>",
                      "reviewRating": {
                        "@type": "Rating",
                        "bestRating": "5",
                        "ratingValue": "<?php echo $review['rating'];?>",
                        "worstRating": "1"
                      }
                    }
           <?php
    
                        if($i<count($reviewss) -1) echo ',';?>
                    <?php $i++; } ?>
                  ]
    

    此代码绝对有效实际上此代码在所有数组的末尾回显逗号,但根据条件的最后一个数组从不回显逗号

    【讨论】:

      【解决方案3】:
      <?php $i=0; foreach($reviewss as $review) {  ?>
                      {
                        "@type": "Review",
                        "author": "<?php echo $review['author'];?>",
                        "datePublished": "<?php echo $review['date_added'];?>",
                        "description": "<?php echo $review['text'];?>",
                        "name": "<?php echo $review['author'];?>",
                        "reviewRating": {
                          "@type": "Rating",
                          "bestRating": "5",
                          "ratingValue": "<?php echo $review['rating'];?>",
                          "worstRating": "1"
                          }
      
                          }
                          <?php
                          if($i<count($reviewss)) echo ',';
                          ?>
                      <?php $i++; } ?>
      

      我觉得应该可以

      【讨论】:

      • 这是一种创建 json 的可怕方式
      • 我知道,我只是向他展示了防止添加最后一个逗号的方法。
      • 最好向别人展示如何正确地完成整个过程
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多