【问题标题】:How to create JSON from cfquery?如何从 cfquery 创建 JSON?
【发布时间】:2016-01-22 19:37:28
【问题描述】:

这是我在 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    example@gmail.com
2   01112016    example@gmail.com
3   01132016    example@gmail.com
3   01162016    example@gmail.com
4   01182016    example@gmail.com
5   01192016    example@gmail.com

正如您在我的输出中看到的那样,我只是得到结束日期,但我没有得到介于两者之间的日期。我应该得到从开始到结束日期的所有日期。如果我的开始日期是 2015 年 1 月 20 日,结束日期是 2015 年 1 月 24 日,我想在两者之间设置日期。我不确定我的 JSON 是否正确创建或者我的 cfloop 中有什么问题。如果有人可以帮助解决这个问题,请告诉我。

【问题讨论】:

  • 如果你转储你的查询结果,你会看到所有你期望的记录吗?
  • ColdFusion 真的可以循环使用日期变量吗? &lt;cfloop from="#PickDateTime#" to="#DropDateTime#" index="i" step="#CreateTimeSpan(1,0,0,0)#"&gt; 看起来很可疑
  • @JamesAMohler - 是的,它有效,因为日期对象在内部是数字。嗯...大部分有效。与 createTimeSpan 结合使用时,索引转换为java.lang.Double 创建subtle rounding issues in some cases。就个人而言,我避免使用它,而是使用整数循环 + 日期函数。
  • @user3023588 - 您最近的帖子似乎已经解释了how you are trying to solve some unknown task, without having explained what that task is first。可能有更简单的选择,但如果没有明确的最终目标,就很难提出选择。最终目标到底是什么(填充日历等......)?还有,myQuery的内容是什么?执行查询的 cfdump 并发布屏幕截图或使用 trycf.compost a sample query
  • 您使用的是什么版本的 ColdFusion?在 CF10 中:SerializeJSON 可能会导致 json 无效。

标签: javascript json date coldfusion


【解决方案1】:

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

从 ColdFusion 8 开始支持。See SerializeJSON

【讨论】:

    【解决方案2】:

    请试试这个

    SerializeJSON: 将 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 2016-07-10
      • 2015-08-31
      • 2015-11-22
      • 1970-01-01
      • 2021-04-11
      • 2017-01-14
      相关资源
      最近更新 更多