任何人都可以帮助我如何将一个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