2017-07-24 62 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