2012-07-26 106 views
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": "[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; 
     } 
    } 
} 
+0

這似乎不是一個有效的JSON – PoX 2012-07-26 17:08:42

+0

[SSCCE(HTTP:// robzu。 com/sscce-short-self-contained-correct-compilable-example) – RobB 2012-07-26 17:11:05

+1

@PoX:當然這不是有效的json,這就是OP要求的原因。 – knittl 2012-07-26 17:11:25

回答

0

JSON對象只不過是一個單純的字符串。爲了達到你想達到什麼目的,你可能需要解碼,然後重新編碼您的JSON字符串:

$jsonData['data'] = json_decode($data, TRUE); 
+0

權利和編碼的JSON使用JavaScript的JSON對象.. var jsonObj = JSON.stringify(origObject); – dano 2012-07-26 17:09:45

+1

是的,但'JSON.stringify'返回一個字符串(即使它是一個對象的方法,這與本次討論無關) – knittl 2012-07-26 17:10:49

+0

Ahh,謝謝knittl你的建議似乎已經奏效,在錯誤的地方沒有討厭的引號和JSON似乎現在驗證,我必須用我的ajax東西正確測試它明天,但它的好進展再次感謝。 – user1555360 2012-07-26 17:54:36

1

爲了使用/解析JSON,它需要有效 JSON ......而那些**"**字符使其無效。

膏和工藝這裏明白我的意思:http://jsonformat.com/

+0

** **是我試圖突出問題的引號;-) – user1555360 2012-07-26 17:39:22

+0

哦!哈哈。對不起 – Kristian 2012-07-26 18:06:35