2012-07-26 105 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