Est-ce que quelqu'un pourrait m'aider à transmettre un objet JSON en tant que champ à un autre sans avoir à ajouter de guillemets? Fondamentalement, j'ai une fonction qui doit être capable d'ajouter un 'en-tête' à un ensemble de données pré-paramétré dans JSON dans certains cas ou simplement analyser les données dans d'autres. Le problème est que tout fonctionne correctement jusqu'à ce que j'essaie de passer un objet JSON pour être stocké en tant que "payload" pour un en-tête, à quel point le JSON devient invalide en raison de l'ensemble supplémentaire de citations jointes.Problèmes de stockage des objets JSON imbriqués
L'objet que je suis en train d'utiliser est:
{
"header": {
"table": "user",
"action": "insert",
"opType": "string",
"message": "Insert sucessful for user: 6",
"start of records": false,
"end of records": true
},
"data": "[
{
"id": "6",
"Surname": "Peter",
"Forename": "Kane",
"Email": "[email protected]",
"Personal_Email": "[email protected]",
"Home_Phone_No": "01216045429",
"Work_Phone_No": "087852489",
"Mobile_Phone_No": "77245455598",
"Address_Line_1": "1aplace",
"Address_Line_2": "thistown",
"Address_Line_3": "Someplace",
"Address_Line_4": "whereever",
"Post_Code": "W549AJ",
"Mode_ID": "44",
"Route_ID": "g12",
"image": ""
}
]"
}
Le problème est entre guillemets après la touche « données » et avant la dernière accolade Curley sans ces tout valide bien. Comme je l'ai dit Im en utilisant PHP J'ai essayé des expressions regex substring etc, mais rien ne semble fonctionner.
mon PHP est la suivante:
public function dataToJSON($operationType, $table, $action, $data, $message, $header = true, $firstRecord = null) {
if ((!($operationType) === 'recordSet') and (!($operationType === 'error')) and (!($operationType === 'string'))) {
throw new Exception("Operation type:" . ' ' . $operationType . ' ' . 'passed to the dataToJSON function not recogonised');
}
if (!(is_null($firstRecord))) {
$isFirstRecord = $firstRecord;
$isLastRecord = !$firstRecord;
} else {
$isFirstRecord = false;
$isLastRecord = false;
}
if ($header) {
$jsonData = array('header' => array(
'table' => "$table",
'action' => "$action",
'opType' => "$operationType",
'message' => "$message",
'start of records' => $isFirstRecord,
'end of records' => $isLastRecord),
);
} else {
$jsonData = array();
}
$recordSet = array();
if ($operationType === 'recordSet') {
while ($row = mysql_fetch_assoc($data)) {
array_push($recordSet, $row);
}
if ($header) {
$jsonData ['data'] = $recordSet;
return json_encode($jsonData);
} else {
return json_encode($recordSet);
}
} else if (($operationType === 'error') || ($operationType === 'string')) {
if ($header) {
$jsonData ['data'] = $data;
return stripslashes(json_encode($jsonData));
} else {
return $data;
}
}
}
Il ne semble pas être un JSON valide – PoX
[SSCCE] (http: // robzu. com/sscce-short-self-contained-correct-compilable-exemple) – RobB
@PoX: bien sûr, ce n'est pas valide json, c'est pourquoi l'OP demande. – knittl