我正在获取WordPress帖子的数据。在WordPress中,数据是tinymce编辑字段,因此以HTML格式显示。在该字段中,数据是这样的
<div class="col-sm-12 col-lg-6">
Caratteristiche principali:
<ul>
<li>Sistema multimediale Toyota Touch� 2 with Go con display 8'' e telecamera posteriore</li>
<li>Sedile guidatore con supporto lombare regolabile elettricamente</li>
<li>Sedili anteriori riscaldabili</li>
<li>Toyota Safety Sense</li>
</ul>
</div>
我用此字段数据和其他几个数据组成了一个数组。 现在,我想以一种可以使用
以JSON格式获取数组数据的方式对上述内容进行编码json_encode
功能。由于上面的html数据,当json_encode运行时,它不会返回json。您能否为我提出解决方案,我如何才能正确地做到这一点。接下来,我需要在python中使用此JSON数据并将其保存在psql中。我想将数据保留在HTML formate的psql中。
这是我在做什么
<?php //php 7.0.8
$data_arr = Array
(
'post_name' => 'toyota-c-hr-1-8-ibrida-122cv-cvt-business-24-mesi',
'caratteristiche_tecniche' => '<div class="col-sm-12 col-lg-6">
Caratteristiche principali:
<ul>
<li>Sistema multimediale Toyota Touch® 2 with Go con display 8'' e telecamera posteriore</li>
<li>Sedile guidatore con supporto lombare regolabile elettricamente</li>
<li>Sedili anteriori riscaldabili</li>
<li>Toyota Safety Sense
<table class="table">
<tbody>
<tr>
<td>?</td>
<td>Sistema Pre-Collisione</td>
</tr>
<tr>
<td>?</td>
<td>Avviso superamento corsia</td>
</tr>
<tr>
<td>?</td>
<td>Abbaglianti automatici</td>
</tr>
<tr>
<td>?</td>
<td>Riconoscimento segnaletica stradale</td>
</tr>
<tr>
<td>?</td>
<td>Sistema Pre-Collisione con rilevamento pedoni</td>
</tr>
<tr>
<td>?</td>
<td>Cruise Control adattivo</td>
</tr>
</tbody>
</table>
</li>
</ul>
</div>
<div class="col-sm-12 col-lg-6">
<div class="col-sm-12 col-lg-6">
<table class="table table-list table-striped margin_zero">
<tbody>
<tr>
<td>Marca</td>
<td><strong class="ng-binding">TOYOTA</strong></td>
</tr>
<tr>
<td>Modello</td>
<td><strong class="ng-binding">C-HR</strong></td>
</tr>
<tr>
<td>Allestimento</td>
<td><strong class="ng-binding">1.8H (122CV) E-CVT Business</strong></td>
</tr>
<tr>
<td>Cilindrata Effettiva</td>
<td><strong class="ng-binding">1798 Cm3</strong></td>
</tr>
<tr>
<td>Cilindrata</td>
<td><strong class="ng-binding">1798 L</strong></td>
</tr>
<tr>
<td>Pacchetto (Tipo Eletta, Sport)</td>
<td><strong class="ng-binding">1.8H (122CV) E-CVT Business</strong></td>
</tr>
<tr>
<td>Model Year</td>
<td><strong class="ng-binding">2017</strong></td>
</tr>
<tr>
<td>Peso</td>
<td><strong class="ng-binding">1455 Kg</strong></td>
</tr>
<tr>
<td>Portata</td>
<td><strong>405 Kg</strong></td>
</tr>
<tr>
<td>Tipo Veicolo</td>
<td><strong class="ng-binding">Manufacturer Vehicle</strong></td>
</tr>
<tr>
<td>Emissioni C02</td>
<td><strong class="ng-binding">86 G/Km</strong></td>
</tr>
<tr>
<td>Trazione</td>
<td><strong class="ng-binding">Front</strong></td>
</tr>
</tbody>
</table>
</div>
<div class="col-sm-12 col-lg-6">
<table class="table table-list table-striped margin_zero">
<tbody>
<tr>
<td>Consumo Combinato</td>
<td><strong class="ng-binding">3.8 Litri Per 100km</strong></td>
</tr>
<tr>
<td>Porte</td>
<td><strong class="ng-binding">5-Door</strong></td>
</tr>
<tr>
<td>Posti</td>
<td><strong class="ng-binding">5</strong></td>
</tr>
<tr>
<td>Alimentazione</td>
<td><strong class="ng-binding">Unleaded - Electric / </strong></td>
</tr>
<tr>
<td>Cambio</td>
<td><strong class="ng-binding">AUT</strong></td>
</tr>
<tr>
<td>Potenza Massima KW</td>
<td><strong class="ng-binding">90 KW</strong></td>
</tr>
<tr>
<td>Potenza Massima CV</td>
<td><strong class="ng-binding">122 CV</strong></td>
</tr>
<tr>
<td>Cavalli Fiscali</td>
<td><strong class="ng-binding">19</strong></td>
</tr>
<tr>
<td>Corpo Veicolo</td>
<td><strong class="ng-binding">Sport Utility Vehicle</strong></td>
</tr>
<tr>
<td>Codice Ruote Anteriori</td>
<td><strong class="ng-binding">215/60 C17 96H</strong></td>
</tr>
<tr>
<td>Codice Ruote Posteriori</td>
<td><strong class="ng-binding">215/60 C17 96H</strong></td>
</tr>
</tbody>
</table>
</div>
</div>',
'marca' => 'Toyota'
);
echo json_encode($data_arr);
?>
在我看来,问题就像是在html内寻找分隔符一样。
答案 0 :(得分:1)
由于您是将HTML手动存储在PHP字符串中,因此必须确保正确地在字符串中转义了引号。 8''
特别是您的问题。
您应该看到以下错误:
解析错误:语法错误,意外的” e远程摄像机海报”(T_CONSTANT_ENCAPSED_STRING),期望在第150行的C:\ path \ to \ file.php中出现')'
如果没有看到错误,请检查错误日志或在文件顶部添加error_reporting(E_ALL);
。
此错误告诉您,为了在单引号引起来的字符串中包含单引号,您需要应用反斜杠以创建文字单引号,如下所示:
<?php //php 7.0.8
$data_arr = Array
(
'post_name' => 'toyota-c-hr-1-8-ibrida-122cv-cvt-business-24-mesi',
'caratteristiche_tecniche' => '<div class="col-sm-12 col-lg-6">
Caratteristiche principali:
<ul>
<li>Sistema multimediale Toyota Touch® 2 with Go con display 8\'\' e telecamera posteriore</li>
<li>Sedile guidatore con supporto lombare regolabile elettricamente</li>
<li>Sedili anteriori riscaldabili</li>
<li>Toyota Safety Sense
<table class="table">
<tbody>
<tr>
<td>?</td>
<td>Sistema Pre-Collisione</td>
</tr>
<tr>
<td>?</td>
<td>Avviso superamento corsia</td>
</tr>
<tr>
<td>?</td>
<td>Abbaglianti automatici</td>
</tr>
<tr>
<td>?</td>
<td>Riconoscimento segnaletica stradale</td>
</tr>
<tr>
<td>?</td>
<td>Sistema Pre-Collisione con rilevamento pedoni</td>
</tr>
<tr>
<td>?</td>
<td>Cruise Control adattivo</td>
</tr>
</tbody>
</table>
</li>
</ul>
</div>
<div class="col-sm-12 col-lg-6">
<div class="col-sm-12 col-lg-6">
<table class="table table-list table-striped margin_zero">
<tbody>
<tr>
<td>Marca</td>
<td><strong class="ng-binding">TOYOTA</strong></td>
</tr>
<tr>
<td>Modello</td>
<td><strong class="ng-binding">C-HR</strong></td>
</tr>
<tr>
<td>Allestimento</td>
<td><strong class="ng-binding">1.8H (122CV) E-CVT Business</strong></td>
</tr>
<tr>
<td>Cilindrata Effettiva</td>
<td><strong class="ng-binding">1798 Cm3</strong></td>
</tr>
<tr>
<td>Cilindrata</td>
<td><strong class="ng-binding">1798 L</strong></td>
</tr>
<tr>
<td>Pacchetto (Tipo Eletta, Sport)</td>
<td><strong class="ng-binding">1.8H (122CV) E-CVT Business</strong></td>
</tr>
<tr>
<td>Model Year</td>
<td><strong class="ng-binding">2017</strong></td>
</tr>
<tr>
<td>Peso</td>
<td><strong class="ng-binding">1455 Kg</strong></td>
</tr>
<tr>
<td>Portata</td>
<td><strong>405 Kg</strong></td>
</tr>
<tr>
<td>Tipo Veicolo</td>
<td><strong class="ng-binding">Manufacturer Vehicle</strong></td>
</tr>
<tr>
<td>Emissioni C02</td>
<td><strong class="ng-binding">86 G/Km</strong></td>
</tr>
<tr>
<td>Trazione</td>
<td><strong class="ng-binding">Front</strong></td>
</tr>
</tbody>
</table>
</div>
<div class="col-sm-12 col-lg-6">
<table class="table table-list table-striped margin_zero">
<tbody>
<tr>
<td>Consumo Combinato</td>
<td><strong class="ng-binding">3.8 Litri Per 100km</strong></td>
</tr>
<tr>
<td>Porte</td>
<td><strong class="ng-binding">5-Door</strong></td>
</tr>
<tr>
<td>Posti</td>
<td><strong class="ng-binding">5</strong></td>
</tr>
<tr>
<td>Alimentazione</td>
<td><strong class="ng-binding">Unleaded - Electric / </strong></td>
</tr>
<tr>
<td>Cambio</td>
<td><strong class="ng-binding">AUT</strong></td>
</tr>
<tr>
<td>Potenza Massima KW</td>
<td><strong class="ng-binding">90 KW</strong></td>
</tr>
<tr>
<td>Potenza Massima CV</td>
<td><strong class="ng-binding">122 CV</strong></td>
</tr>
<tr>
<td>Cavalli Fiscali</td>
<td><strong class="ng-binding">19</strong></td>
</tr>
<tr>
<td>Corpo Veicolo</td>
<td><strong class="ng-binding">Sport Utility Vehicle</strong></td>
</tr>
<tr>
<td>Codice Ruote Anteriori</td>
<td><strong class="ng-binding">215/60 C17 96H</strong></td>
</tr>
<tr>
<td>Codice Ruote Posteriori</td>
<td><strong class="ng-binding">215/60 C17 96H</strong></td>
</tr>
</tbody>
</table>
</div>
</div>',
'marca' => 'Toyota'
);
echo json_encode($data_arr);
?>
答案 1 :(得分:-1)
您是否在发送的内容中正确转义了 public class Test
{
public string EmployeeName { get; set; }
}
和"
?我猜您正在尝试转换手动输入的给定字符串,而不是从其他位置获取该字符串作为变量?这段代码:
'
正在工作,并且结果
<?php
$string = "<div class=\"col-sm-12 col-lg-6\">
Caratteristiche principali:
<ul>
<li>Sistema multimediale Toyota Touch� 2 with Go con display 8'' e telecamera posteriore</li>
<li>Sedile guidatore con supporto lombare regolabile elettricamente</li>
<li>Sedili anteriori riscaldabili</li>
<li>Toyota Safety Sense</li>
</ul>
</div>";
$array = array($string, "sample_value" => 2);
$data = json_encode($array);
print $data;
如果仍然有问题,请提供有关它的更多详细信息。