JSON / CFML -在一组结构体上循环

时间:2021-01-08 00:57:06

I'm testing an app called Barcode Scanner Terminal for my business; I'm looking to use it as a replacement of sorts for a timeclock.

我正在测试一个叫条形码扫描器终端的应用;我想用它来代替时间锁。

Employees can scan a barcode or QR code on their ID badge, and this app will send that, along with a timestamp and even GPS coordinates to my server to verify that they're at the right place at the right time. When the phone has an internet connection, this information is passed through via a special URL on my site and I can make it work flawlessly.

员工可以扫描他们ID卡上的条码或二维码,这个应用会把它连同时间戳甚至GPS坐标一起发送到我的服务器,以验证他们在正确的时间在正确的地点。当手机有互联网连接时,这些信息通过我网站上的一个特殊URL传递,我可以让它完美地工作。

The problem?

这个问题?

When there is no internet, the phone stores the scans locally and you can send it to your server later. These scans are stored as a JSON array of structs.

当没有internet时,电话将扫描存储在本地,您可以稍后将其发送到您的服务器。这些扫描被存储为一个JSON结构数组。

According to the documentation

根据文档

...Data stored in this manner can be sent to the server as a POST request. I was unsure about how to test all this so I just set some variables, did a scan, and had an email sent to me that dumped the values they represented.

…以这种方式存储的数据可以作为POST请求发送到服务器。我不确定如何测试所有这些,所以我设置了一些变量,进行了一次扫描,并给我发了一封邮件,把它们所代表的值都删除了。

<cfset requestBody = toString( getHttpRequestData().content )/>

Returned this monstrosity and many others like it; what you see below is from one scan.

这怪物和其他许多人都喜欢它;下面你看到的是一次扫描。

batch=%5B%7B%22barcode%22%3A%22CSS1959%22%2C%22scannerID%22%3A%223e81b04aa521a05e%22%2C%22time%22%3A%222015-08-11+08%3A28%3A20.419%22%2C%22lat%22%3A32.3999433%2C%22long%22%3A-110.040379%7D%5D

批= % 5 b % 7 b % 22条形码% 3 22% % 22 css1959 % 22% 2 c % 22 scannerid % 3 22% % 223 e81b04aa521a05e % 22% 2 c % 22时间% 3 22% % 25 222015-08-11 + 08% 3 % 3 a20.419 % 22% 2 c % 22 lat % 3 a32.3999433 % 2 c % 22长% 22% 22% 3 7 - 110.040379 % d % 5 d

So I ran that value through the URLDecode() function to get something that looks more familiar.

因此,我通过URLDecode()函数运行该值以获得更熟悉的结果。

<cfset decodedBody = #URLDecode(requestBody)#>
<!--- This would output the following. Line breaks are for ease of reading. --->
batch=[{"barcode":"CSS1959","scannerID":"3e81b04aa521a05e",
"time":"2015-08-11 08:28:20.419","lat":32.3999433,"long":-110.040379}] 

So eventually I had a bunch of scans that formed this Array of Structs in JSON and I have no clue how to handle it.

最后,我进行了一系列的扫描这些JSON结构数组我不知道该怎么处理。

batch=[
{"barcode":"CSS1035","scannerID":"3e81b04aa521a05e","time":"2015-08-11 08:30:27.232","lat":32.4001579,"long":-110.0403455},
{"barcode":"CSS1959","scannerID":"3e81b04aa521a05e","time":"2015-08-11 08:30:29.366","lat":32.4001579,"long":-110.0403455},
{"barcode":"CSS1649","scannerID":"3e81b04aa521a05e","time":"2015-08-11 08:30:31.642","lat":32.4001579,"long":-110.0403455}
] 

I'm TRYING to loop through it and insert them all into a database.

我试着对它进行循环并将它们都插入到数据库中。

So far I have this and I am getting a JSON parsing error.

到目前为止,我得到了这个,并且我得到了一个JSON解析错误。

<cfset requestBody = toString( getHttpRequestData().content ) />
<cfset decodedBody = #URLDecode(requestBody)#>
<!---This is where I falter because I have no idea how to handle it.--->
<cfset arrayOfStructs = deserializeJson(decodedBody)>
<cfloop array="#arrayOfStructs#" index="barcode">
blah blah query logic
</cfloop>

Here's the error I get.JSON / CFML -在一组结构体上循环

这是我得到的误差。

Thanks!

谢谢!

EDIT: Solved thanks in part to help I received here. Solution below:

<cfset requestBody = #replaceNoCase( toString(getHttpRequestData().content), "batch=", "" )#  />
<cfset decodedBody = #URLDecode(requestBody)#>
<cfset ArrayOfStructs = deserializeJson(decodedBody)>
<cfloop array="#ArrayOfStructs#" index="i">
<cfquery name="doodoodoo" datasource="CRM">
    INSERT INTO TimeAppTest
    (
        EmployeeID,
        lat,
        long,
        TimoStampo
        )
    VALUES
    (
        '#i.barcode#',
        '#i.lat#',
        '#i.long#',
        '#i.time#'

        )
</cfquery>
</cfloop>

2 个解决方案

#1


3  

It seems to me that you may well be able to access the value of "batch" directly as a part of the form scope. Assuming of course you are really receiving a post request.

在我看来,您很可能可以直接作为表单范围的一部分访问“batch”的值。假设你确实收到了一个post请求。

So you may well be able to just do:

所以你可以这样做:

<cfif isDefined("form.batch")>
    <cfset aData = deSerializeJSON(trim(form.batch))>
    <cfdump var="#aData#">
</cfif>

So no need to look at the request body and no need to decode the response.

因此,不需要查看请求主体,也不需要解码响应。

#2


2  

You either need to strip off the "batch=" or pass the entire thing into "evaluate()" which will set the array literal into a variable called batch. I would recommend the former option since the second could be exploited by malicious CFML content in the string.

您要么需要去掉“batch=”,要么将整个东西传递到“evaluate()”中,这将把数组文本设置成一个名为batch的变量。我建议使用前者,因为第二个选项可能被字符串中的恶意CFML内容利用。

<cfset arrayOfStructs = deserializeJson( replaceNoCase( decodedBody, "batch=", "" ) )>

#1


3  

It seems to me that you may well be able to access the value of "batch" directly as a part of the form scope. Assuming of course you are really receiving a post request.

在我看来,您很可能可以直接作为表单范围的一部分访问“batch”的值。假设你确实收到了一个post请求。

So you may well be able to just do:

所以你可以这样做:

<cfif isDefined("form.batch")>
    <cfset aData = deSerializeJSON(trim(form.batch))>
    <cfdump var="#aData#">
</cfif>

So no need to look at the request body and no need to decode the response.

因此,不需要查看请求主体,也不需要解码响应。

#2


2  

You either need to strip off the "batch=" or pass the entire thing into "evaluate()" which will set the array literal into a variable called batch. I would recommend the former option since the second could be exploited by malicious CFML content in the string.

您要么需要去掉“batch=”,要么将整个东西传递到“evaluate()”中,这将把数组文本设置成一个名为batch的变量。我建议使用前者,因为第二个选项可能被字符串中的恶意CFML内容利用。

<cfset arrayOfStructs = deserializeJson( replaceNoCase( decodedBody, "batch=", "" ) )>