2017-07-24 86 views
0

我寫了一個web服務(cfc)在ColdFusion接受JSON輸入數據,我在驗證後返回http狀態碼。 我的問題是 - 如何在ColdFusion中,在FORM範圍內捕獲/接受JSON數據?JSON輸入到ColdFusion webservice + RestFul

我寫了兩種接受JSON的方法,我不確定。任何人都可以請幫忙。

第一種方式:

<cfscript> 
    record=deserializeJSON(
'{ 
"customerId": #Form.CustomerID#, 
"userName": "#Form.userName#", 
"password": "#Form.Password#" 
}' 
); 

this.customerid = record.customerId; 
this.userName = record.userName; 
this.password = record.password; 
</cfscript> 

我解析輸入JSON和得到它的結構,然後在變量設置參數。

方式二:

<cfif (cgi.content_type EQ "application/json")> 

     <cfset record = deserializeJSON(ToString(getHTTPRequestData().content))> 
     <cfscript> 
this.customerId = record.customerId; 
this.userName = record.userName; 
this.password = record.password; 
</cfscript> 

</cfif> 

誰能請幫助我瞭解如何捕捉JSON輸入數據的ColdFusion?

+0

我在看你的使用deserivalizeJSON的'() '我覺得你走錯了路。我想你想建立一個結構體,然後序列化它。 –

回答

0

你說過你的問題的方式我不確定你在問什麼?看看你的代碼,它看起來像你想將表單提交值轉換成JSON?如果是這樣,只是這樣做:

<cfset myJSONForm = serializeJSON(form)> 

如果你捕捉JSON應該很容易,只要JSON格式正確:

<cfif isJSON(form.jsonString)> 
    <cfset myJSONvar = deserializeJSON(form.jsonString)> 
</cfif> 
+0

不,實際上我想將JSON值轉換爲變量。我將收到json輸入。 – Vasu