2016-01-22 86 views
-1

这是我的查询,我把它放在JSON中,然后在表中存储信息。当我在表格中检查结果时,我看到输出结果并没有给我我想要的东西。我的想法是在每个开始日期和结束日期之间获取所有日期。我的代码只给了我结束日期。这里是我的代码:如何从cfquery创建JSON?

//Here is my query 
<cfquery name="myQuery" datasource="test"> 
    Select UserID, UserEmail, PickDateTime, DropDateTime 
    From UserInfo 
    Order by PickDateTime 
</cfquery> 

//This is my JSON 
<script> 
    myJSON = { 
    <cfoutput query="myQuery"> 
     <cfloop from="#PickDateTime#" to="#DropDateTime#" index="i" step="#CreateTimeSpan(1,0,0,0)#"> 
      "#currentrow#":{"ID":"#UserID#","date":"#dateformat(i,'mmddyyyy')#","email":"#UserEmail#"}, 
     </cfloop> 
    </cfoutput> 
    } 

//Here is my function that creates the table 
function getData(){ 
    myVar="<table><tr><td>ID</td><td>Date</td><td>Email</td></tr><tbody>" 
    for(key in myJSON){ 
     myVar+= 
      "<tr>"+ 
      "<td>"+myJSON[key].ID+"</td>"+ 
      "<td>"+myJSON[key].date+"</td>"+ 
      "<td>"+myJSON[key].email+"</td>"+ 
      "</tr>" 
    } 
    myVar+="</tbody></table>" 
    document.getElementById('myTable').innerHTML = myVar  
} 
</script> 

,这里是我的html:

<div id='myTable'></div> 

这里是我的表,电流输出:

ID Date   Email 
1 01092016 [email protected] 
2 01112016 [email protected] 
3 01132016 [email protected] 
3 01162016 [email protected] 
4 01182016 [email protected] 
5 01192016 [email protected] 

正如你可以在我的输出,我看到刚刚结束日期,但我没有得到日期之间。我应该从开始到结束日期获取所有日期。如果我的开始日期是2015年1月20日,而我的结束日期是2015年1月24日,我希望在两者之间有日期。我不确定我的JSON是否正确创建或者我的cfloop中有什么错误。如果有人能帮助解决这个问题,请告诉我。

+1

如果你倾倒你的查询结果你看到所有你所期望的记录? –

+0

ColdFusion是否可以真正循环使用日期变量? ''这看起来很可疑 –

+0

@JamesAMohler - 是的,它起作用,因为日期对象是内部的数字。那么... *大多数*的作品。当与createTimeSpan结合使用时,索引被转换为'java.lang.Double',在某些情况下创建[微妙舍入问题](http://stackoverflow.com/a/29214873/104223)。就个人而言,我避免它,并使用整数循环+日期函数。 – Leigh

回答

3

将ColdFusion数据转换为JSON的最简单方法是使用SerializeJSON()函数。

因为ColdFusion的支持8. See SerializeJSON

0

请试试这个

SerializeJSON:在data.Returns的 ColdFusion数据转换成JSON(JavaScript对象符号)表示,它包含一个JSON字符串表示参数值。但如果您使用的是ColdFusion 10: SerializeJSON may results in invalid json

<cfscript> 
    // Create an input string that has two different uses of the "u" character. 
    input = "Hello \u1111 u+2222 world"; 
    // Output the original value. 
    writeOutput(input & "<br />"); 
    // Output the serialized value produced by serializeJson(). 
    // CAUTION: The "u+" string will be accidentally replaced with "\u". 
    writeOutput(serializeJson(input)); 
</cfscript> 

为了避免这种情况,并使其在任何的ColdFusion版本,你可以做到这一点运行:

<cfscript> 
    input = "\\u+1234 hello \u1111 , \u+2222 , u+2222 world"; 
    writeOutput(input & "<br />"); 
    serializedInput = serializeJson(input); 
    writeOutput(serializedInput & "<br />"); 
    savecontent variable = "pattern" { 
     writeOutput("(?x)"); 
     writeOutput("(^ | [^\\] | (?: \\ \\)+)"); 
     writeOutput("\\u"); 
    } 

    safeOutput = javaCast("string", serializedInput).replaceAll(
     javaCast("string", pattern), 
     javaCast("string", "$1u+") 
    ); 


    writeOutput(safeOutput & "<br />"); 
    writeOutput(deserializeJson(safeOutput)); 
</cfscript> 

REFERENCE