0

引用符を追加せずに、ある 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": "pkane@a.co.uk",
            "Personal_Email": "p.kane@gmail.com",
            "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;
        }
    }
}
4

2 に答える 2

1

jsonを使用/解析するには、有効なjsonである必要があります...そしてそれらの**"**文字はそれを無効にします。

ここに貼り付けて処理し、意味を確認してください:http: //jsonformat.com/

于 2012-07-26T17:08:21.677 に答える
0

JSONオブジェクトは単なる文字列にすぎません。達成しようとしていることを達成するには、JSON文字列をデコードしてから再エンコードする必要があります。

$jsonData['data'] = json_decode($data, TRUE);
于 2012-07-26T17:08:37.197 に答える