本文介绍了JSON/CFML - 遍历结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的企业测试一款名为 Barcode Scanner Terminal 的应用;我希望将其用作时钟的替代品.

员工可以扫描他们身份证上的条形码或二维码,这个应用程序会将其连同时间戳甚至 GPS 坐标一起发送到我的服务器,以验证他们是否在正确的时间出现在正确的地方.当手机有互联网连接时,这些信息会通过我网站上的一个特殊 URL 传递,我可以让它完美运行.

问题?

当没有互联网时,手机会在本地存储扫描结果,您可以稍后将其发送到您的服务器.这些扫描存储为 JSON 结构数组.

谢谢!

部分感谢我在这里收到的帮助.解决方案如下:

插入 TimeAppTest(员工ID,纬度,长,蒂莫斯坦波)价值观('#i.barcode#','#i.lat#','#i.long#','#i.time#')</cfquery></cfloop>
解决方案

在我看来,您可以直接访问batch"的值作为表单范围的一部分.当然,假设您确实收到了一个发布请求.

所以你可以这样做:

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

因此无需查看请求正文,也无需解码响应.

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.

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.

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.

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

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

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

<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.

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.

<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.

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>
解决方案

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.

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.

这篇关于JSON/CFML - 遍历结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:51