Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从cfquery创建JSON?_Javascript_Json_Date_Coldfusion - Fatal编程技术网

Javascript 如何从cfquery创建JSON?

Javascript 如何从cfquery创建JSON?,javascript,json,date,coldfusion,Javascript,Json,Date,Coldfusion,这是我在JSON中输入的查询,然后将信息存储在表中。当我检查表中的结果时,我发现输出并没有给出我真正想要的结果。我的想法是让所有日期都在每个开始日期和结束日期之间。我的代码只给出了结束日期。这是我的密码: //Here is my query <cfquery name="myQuery" datasource="test"> Select UserID, UserEmail, PickDateTime, DropDateTime From UserInfo

这是我在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>

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

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


从ColdFusion 8开始支持

请试试这个

序列化JSON:将ColdFusion数据转换为数据的JSON(JavaScript对象表示法)表示形式。返回包含参数值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>

//创建一个具有两种不同“u”字符用法的输入字符串。
输入=“你好\u1111 u+2222世界”;
//输出原始值。
写输出(输入&“
”); //输出serializeJson()生成的序列化值。 //警告:“u+”字符串将意外替换为“\u”。 writeOutput(序列化JSON(输入));
要避免这种情况并使其在任何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> 

input=“\\u+1234你好\u1111\u+2222,u+2222世界”;
写输出(输入&“
”); serializedInput=serializeJson(输入); 写输出(序列化输入&“
”); savecontent变量=“模式”{ 写输出(“(?x)”); 写输出((^ |[^\\]|(?:\\\\)+)); 注销输出(“u”); } safeOutput=javaCast(“字符串”,serializedInput).replaceAll( javaCast(“字符串”,模式), javaCast(“字符串”,“$1u+”) ); 写输出(安全输出&“
”); writeOutput(反序列化JSON(安全输出));

如果转储查询结果,是否可以看到预期的所有记录?ColdFusion真的可以循环使用带有日期的变量吗<代码>看起来很像suspect@JamesAMohler-是的,它可以工作,因为日期对象在内部是数字。好大部分是有效的。当与createTimeSpan结合使用时,将转换索引
java.lang.Double
creating。就我个人而言,我避免使用整数循环+日期函数。@user3023588-似乎您最近的线程已经解释过了。可能有更简单的选择,但如果没有明确的最终目标,就很难提出选择。最终目标是什么(填写日历等)?另外,
myQuery
的内容是什么?对查询进行cfdump并发布屏幕截图或用于。您使用的是哪个版本的ColdFusion?在CF10中:序列化json可能会导致无效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>
<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>