JSON不添加最后一个逗号

时间:2017-12-04 19:28:04

标签: javascript php arrays json regex

我有脚本:

<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"
                    }

                    },

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

3 个答案:

答案 0 :(得分:4)

您应该使用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

答案 1 :(得分:0)

  "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++; } ?>
              ]

此代码绝对有效,实际上该代码在所有数组的末尾都用回音逗号表示,但根据条件,最后一个数组永远不会回音逗号

答案 2 :(得分:-2)

<?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++; } ?>

我认为应该有效