使用javascript函数解析Coldfusion JSON - 未捕获的ReferenceError:未定义WddxRecordset

时间:2022-05-18 19:07:02

I am attempting to parse Coldfusion JSON data to make it look "normal" ColdFusion json:

我试图解析Coldfusion JSON数据,使其看起来“正常”ColdFusion json:

{"ROWCOUNT":3,"COLUMNS":["ROWID","REL","DATE","FOA","TITLE","APPRECEIPE","OPENING","KEYWORDS","DOC","PURPOSE","APP","NAME","PURPOSE"],"DATA":{"ROWID":[24842,24841,23780],"REL":["032","031","108"],...

Searched on the web and found this link:

在网上搜索,发现此链接:

http://www.tysoncadenhead.com/blog/parsing-coldfusion-json-on-the-client-side#.V-UpKfkrKUk

He offers to use this function:

他提供使用此功能:

Object.prototype.parseCFJSON = function() {
var result = [],
   data = this;
for (var j = 0; j < data.DATA.length; j++) {
  result[j] = {};
  for (var i = 0; i < data.COLUMNS.length; i++) {
     result[j][data.COLUMNS[i].toLowerCase()] = data.DATA[j][i];
  }
}
return result;
};

Here is my code

这是我的代码

<cfset jsonURL = SerializeJSON(SmartGuideSearchRet,true)>
<cfset URLd = #SmartGuideSearchRet#> //My CF Struct
<script>
<cfoutput>
var #toScript(URLd,"URLd")#;
</cfoutput>
Object.prototype.parseCFJSON = function() {

var result = [],
   data = this;

for (var j = 0; j < data.DATA.length; j++) {
  result[j] = {};
  for (var i = 0; i < data.COLUMNS.length; i++) {
     result[j][data.COLUMNS[i].toLowerCase()] = data.DATA[j][i];
  }
}

return result;
};
ujsonURL = URLd.parseCFJSON();
console.log(ujsonURL);
</script>

My console is giving me this error Uncaught ReferenceError: WddxRecordset is not defined

我的控制台给我这个错误未捕获的ReferenceError:未定义WddxRecordset

When I try to feed the json coldfusion data to this function like this:

当我尝试将json coldfusion数据提供给此函数时,如下所示:

<script>
<cfoutput>
var #toScript(URLd,"URLd")#;
</cfoutput>
Object.prototype.parseCFJSON = function() {

var result = [],
   data = this;

for (var j = 0; j < data.DATA.length; j++) {
  result[j] = {};
  for (var i = 0; i < data.COLUMNS.length; i++) {
     result[j][data.COLUMNS[i].toLowerCase()] = data.DATA[j][i];
  }
}

return result;
};
ujsonURL = jsonURL.parseCFJSON();
console.log(ujsonURL);
</script>

I am getting this error:

我收到此错误:

Uncaught TypeError: Cannot read property 'length' of undefined

未捕获的TypeError:无法读取未定义的属性“长度”

at this line

在这条线上

for (var j = 0; j < data.DATA.length; j++) {

for(var j = 0; j ;>

I am doing something wrong and I am really stumped. Any help would be appreciated. I could be using the js function completely wrong (still learning).

我做错了什么,我真的很难过。任何帮助,将不胜感激。我可能完全错误地使用js函数(仍然在学习)。

4 个解决方案

#1


1  

Have you considered converting the ColdFusion JSON data back to a ColdFusion Query object and then reconverting it back to json using JSONUtil?

您是否考虑过将ColdFusion JSON数据转换回ColdFusion Query对象,然后使用JSONUtil将其重新转换回json?

JSONUtil has better JSON support for CF7-2016 with "strictMapping" & "serializeQueryByColumns" support.

JSONUtil对CF7-2016有更好的JSON支持,支持“strictMapping”和“serializeQueryByColumns”。

<cfscript>
JSONString = {}; /* This needs to be the ColdFusion JSON query */
JSONUtil = CreateObject("component","JSONUtil");
QueryData = JSONUtil.DeserializeJSON(JSONString, false);
JSON_oldWDDX = JSONUtil.SerializeJSON(var=QueryData, serializeQueryByColumns=true, strictMapping=true);
JSON_Array = JSONUtil.SerializeJSON(var=QueryData, serializeQueryByColumns=false, strictMapping=true);
</cfscript>

#2


0  

Maybe you could try serializing the coldfusion to a string, then passing that into the function. Then, convert the string back to JS JSON with parse()

也许您可以尝试将coldfusion序列化为字符串,然后将其传递给函数。然后,使用parse()将字符串转换回JS JSON

<cfset jsonURL = SerializeJSON(SmartGuideSearchRet,true)>
<script>
<cfoutput>
var #toScript(jsonURL,"jsonURL")#;
</cfoutput>
function parseCFJSON(json) {

var result = [],
   data = json.parse();

for (var j = 0; j < data.DATA.length; j++) {
  result[j] = {};
  for (var i = 0; i < data.COLUMNS.length; i++) {
     result[j][data.COLUMNS[i].toLowerCase()] = data.DATA[j][i];
  }
}

return result;
};
parsedJSON = parseCFJSON(ujsonURL);
console.log(parsedJSON);
</script>

#3


0  

Well I figured out what was going wrong here.

好吧,我弄清楚这里出了什么问题。

This <cfoutput> var #toScript(jsonURL,"jsonURL")#; </cfoutput> Was not parsing correctly. I changed it to eval() so my updated script looks like this and returns perfectly.

var #toScript(jsonURL,“jsonURL”)#; 未正确解析。我将其更改为eval(),因此我更新的脚本看起来像这样并且返回完美。

<script>
mydata = eval(<cfoutput>#jsonURL#</cfoutput>)
<cfoutput>
</cfoutput>
Object.prototype.parseCFJSON = function() {

var result = [],
   data = this;

for (var j = 0; j < data.DATA.length; j++) {
  result[j] = {};
  for (var i = 0; i < data.COLUMNS.length; i++) {
     result[j][data.COLUMNS[i].toLowerCase()] = data.DATA[j][i];
  }
}

return result;
};
mydata = mydata.parseCFJSON();
console.log(mydata);
alert(mydata);
</script>

#4


0  

Passing "struct" to your serializeJson function also worked for me.

将“struct”传递给serializeJson函数也对我有用。

SerializeJSON(var, "struct");

#1


1  

Have you considered converting the ColdFusion JSON data back to a ColdFusion Query object and then reconverting it back to json using JSONUtil?

您是否考虑过将ColdFusion JSON数据转换回ColdFusion Query对象,然后使用JSONUtil将其重新转换回json?

JSONUtil has better JSON support for CF7-2016 with "strictMapping" & "serializeQueryByColumns" support.

JSONUtil对CF7-2016有更好的JSON支持,支持“strictMapping”和“serializeQueryByColumns”。

<cfscript>
JSONString = {}; /* This needs to be the ColdFusion JSON query */
JSONUtil = CreateObject("component","JSONUtil");
QueryData = JSONUtil.DeserializeJSON(JSONString, false);
JSON_oldWDDX = JSONUtil.SerializeJSON(var=QueryData, serializeQueryByColumns=true, strictMapping=true);
JSON_Array = JSONUtil.SerializeJSON(var=QueryData, serializeQueryByColumns=false, strictMapping=true);
</cfscript>

#2


0  

Maybe you could try serializing the coldfusion to a string, then passing that into the function. Then, convert the string back to JS JSON with parse()

也许您可以尝试将coldfusion序列化为字符串,然后将其传递给函数。然后,使用parse()将字符串转换回JS JSON

<cfset jsonURL = SerializeJSON(SmartGuideSearchRet,true)>
<script>
<cfoutput>
var #toScript(jsonURL,"jsonURL")#;
</cfoutput>
function parseCFJSON(json) {

var result = [],
   data = json.parse();

for (var j = 0; j < data.DATA.length; j++) {
  result[j] = {};
  for (var i = 0; i < data.COLUMNS.length; i++) {
     result[j][data.COLUMNS[i].toLowerCase()] = data.DATA[j][i];
  }
}

return result;
};
parsedJSON = parseCFJSON(ujsonURL);
console.log(parsedJSON);
</script>

#3


0  

Well I figured out what was going wrong here.

好吧,我弄清楚这里出了什么问题。

This <cfoutput> var #toScript(jsonURL,"jsonURL")#; </cfoutput> Was not parsing correctly. I changed it to eval() so my updated script looks like this and returns perfectly.

var #toScript(jsonURL,“jsonURL”)#; 未正确解析。我将其更改为eval(),因此我更新的脚本看起来像这样并且返回完美。

<script>
mydata = eval(<cfoutput>#jsonURL#</cfoutput>)
<cfoutput>
</cfoutput>
Object.prototype.parseCFJSON = function() {

var result = [],
   data = this;

for (var j = 0; j < data.DATA.length; j++) {
  result[j] = {};
  for (var i = 0; i < data.COLUMNS.length; i++) {
     result[j][data.COLUMNS[i].toLowerCase()] = data.DATA[j][i];
  }
}

return result;
};
mydata = mydata.parseCFJSON();
console.log(mydata);
alert(mydata);
</script>

#4


0  

Passing "struct" to your serializeJson function also worked for me.

将“struct”传递给serializeJson函数也对我有用。

SerializeJSON(var, "struct");