任何人都可以幫助我如何將一個JSON對象作爲字段傳遞給另一個,而不需要添加引號?基本上我有一個函數需要能夠在一些預先解析成JSON的數據中附加一個'頭部',或者只是解析其他數據。存儲嵌套JSON對象的問題
問題是一切工作正常,直到我嘗試傳遞一個JSON對象作爲標題的「有效內容」存儲,此時JSON由於附加的額外引用集而變爲無效。
,我試圖使用的對象是:
{
"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": ""
}
]"
}
的問題是之後的「數據」鍵,並沒有這些一切的最後柯利括號驗證罰款前引號。 正如我所說的我使用PHP我曾試過正則表達式子字符串等,但似乎沒有工作。
我的PHP如下:
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;
}
}
}
這似乎不是一個有效的JSON – PoX 2012-07-26 17:08:42
[SSCCE(HTTP:// robzu。 com/sscce-short-self-contained-correct-compilable-example) – RobB 2012-07-26 17:11:05
@PoX:當然這不是有效的json,這就是OP要求的原因。 – knittl 2012-07-26 17:11:25