简体   繁体   English

在PHP中将无效的JSON转换为有效的JSON

[英]Convert invalid json into valid json in php

I am saving some values in database via api(So I can not modify it manually) 我正在通过api将一些值保存在数据库中(因此我无法手动修改它)

When I get back it from database, the json value is not in valid form. 当我从数据库取回它时,json值格式无效。 I do not want to modify each value in database. 我不想修改数据库中的每个值。 I want something in php which can convert that into valid format. 我想要一些可以将其转换为有效格式的php文件。

Something like this in my db: 在我的数据库中是这样的:

$invalid='{
    "response": {

                "id": "16"",  <--------------------- Invalid Format(It can be for any key)
                "event_name": "testing",
                "image": "images/Penguins.jpg",
                "event_date": "2014-12-13",
                "event_time": "02:10",
                "time_interval": "4",
                "location": "mohali",
                "event_type": "Rock",
                "detail": "sfdsf fgf ghb\t",
                "delivery": "dggh fghgfh\t\t",
                "status": "1"
            }
           }';

So how can I convert it in valid json by php like this: 因此,如何通过php将其转换为有效的json:

$invalid='{
    "response": {

                "id": "16",  <--------------------- valid Format
                "event_name": "testing",
                "image": "images/Penguins.jpg",
                "event_date": "2014-12-13",
                "event_time": "02:10",
                "time_interval": "4",
                "location": "mohali",
                "event_type": "Rock",
                "detail": "sfdsf fgf ghb\t",
                "delivery": "dggh fghgfh\t\t",
                "status": "1"
            }
           }';

For this exact case, this will do: 对于这种确切的情况,它将执行以下操作:

<?php
$invalid='{
  "response": {        
    "id": "16"", 
    "event_name": "testing",
    "image": "images/Penguins.jpg",
    "event_date": "2014-12-13",
    "event_time": "02:10",
    "time_interval": "4",
    "location": "",
    "event_type": "Rock"",
    "detail": "sfdsf fgf ghb\t",
    "delivery": "dggh fghgfh\t\t",
    "status": "1""
  },
  "bar":{ "test": ""
  }
}';
$invalid = explode("\n",$invalid);
foreach($invalid as $idx => &$line) {
  $num = 0;
  for($z=0;$z<strlen($line);++$z) {
    $ch = $line[$z];
    if($ch == "\\") {
      ++$z;
    } else if($ch == '"') {
      ++$num;
      $last_pos = $z;
    }
  }
  if($num % 2 == 1) {
    $line[$last_pos] = ' ';
  }             
}       
$valid = implode("\n",$invalid);
print_r($valid);

Output: 输出:

{
  "response": {        
    "id": "16" , 
    "event_name": "testing",
    "image": "images/Penguins.jpg",
    "event_date": "2014-12-13",
    "event_time": "02:10",
    "time_interval": "4",
    "location": "",
    "event_type": "Rock" ,
    "detail": "sfdsf fgf ghb\t",
    "delivery": "dggh fghgfh\t\t",
    "status": "1" 
  },
  "bar":{ "test": ""
  }
}

But it's really recommended to fix the data on the database and the form/code that produce those invalid JSON ^^ 但实际上建议修复数据库中的数据以及产生无效JSON ^^的表单/代码

In what ways can it be invalid? 它在哪些方面可能无效? If it's only the existence of double quotes just use preg_replace: 如果仅存在双引号,则使用preg_replace:

$valid = preg_replace('/""/','"',$invalid);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM