如何将AJAX返回的字符串转换为javascript对象数组

时间:2021-01-03 09:57:53

I'm using jqGrid and would like to use the ajax return from Coldfusion to build the colModel array.

我正在使用jqGrid,并希望使用Coldfusion的ajax返回来构建colModel数组。

When I create the array cm on the client like below, this works.

当我在客户机上创建数组cm时(如下所示),这是可行的。

function subTab(obj,id,tab){
var param={id:id,tab:tab}
http('POST','cfc/view/'+obj+'.cfc?method=view',subTab_RTN,param);
}
function subTab_RTN(obj){
$("#detail").html(obj.html);
if(obj.grid.display){
var cm;
switch(obj.grid.tab){
case "docs":
cm=[{name:'contactID',index:'contactID',hidden:true},
{name:'docName',index:'docName',width:200,label:'Document Name'},
{name:'docType',index:'docType',width:200,label:'Document Type'},
{name:'campaign',index:'campaign',width:200,label:'Campaign'},
{name:'campaignCode',index:'campaignCode',width:125,label:'Campaign Code'},
{name:'campaignType',index:'campaignType',width:125,label:'Campaign Type'},
{name:'downloadDate',index:'downloadDate',width:125,label:'Download     Date',formatter:'date'}];
break;
}
$("#subTabGridTbl").jqGrid({
url:obj.grid.url, 
datatype: "json", 
colModel:cm,
...

I would prefer however to create the array on the server like:

但是,我更喜欢在服务器上创建数组,比如:

 <cfset rtn.grid.cols="[{name:'contactID',index:'contactID',hidden:true},
 {name:'docName',index:'docName',width:200,label:'Document Name'},
 {name:'docType',index:'docType',width:200,label:'Document Type'},
 {name:'campaign',index:'campaign',width:200,label:'Campaign'},
 {name:'campaignCode',index:'campaignCode',width:125,label:'Campaign Code'},
 {name:'campaignType',index:'campaignType',width:125,label:'Campaign Type'},
 {name:'downloadDate',index:'downloadDate',width:125,label:'Download Date',formatter:'date'}]" />

and then use the returned obj (obj.grid.cols) to build the array.

然后使用返回的obj (object .grid.cols)构建数组。

Thanks for your help. Gary

谢谢你的帮助。加里

1 个解决方案

#1


3  

JSON.parse() is supported in most major browsers. If you need to support IE7 and below, I believe you can use jQuery.parseJSON() to get the same result. Both methods require a well-formed JSON string.

大多数主流浏览器都支持JSON.parse()。如果您需要支持IE7和以下,我相信您可以使用jQuery.parseJSON()得到相同的结果。这两个方法都需要一个格式良好的JSON字符串。

As an aside, I'd recommend creating your array as a native CF array of structs and then using serializeJSON() to convert it to a JSON string. This will help minimize any issues you'll run into by trying to write the JSON string by hand.

顺便提一下,我建议将数组创建为结构体的原生CF数组,然后使用serializeJSON()将其转换为JSON字符串。通过尝试手工编写JSON字符串,这将有助于减少遇到的任何问题。

#1


3  

JSON.parse() is supported in most major browsers. If you need to support IE7 and below, I believe you can use jQuery.parseJSON() to get the same result. Both methods require a well-formed JSON string.

大多数主流浏览器都支持JSON.parse()。如果您需要支持IE7和以下,我相信您可以使用jQuery.parseJSON()得到相同的结果。这两个方法都需要一个格式良好的JSON字符串。

As an aside, I'd recommend creating your array as a native CF array of structs and then using serializeJSON() to convert it to a JSON string. This will help minimize any issues you'll run into by trying to write the JSON string by hand.

顺便提一下,我建议将数组创建为结构体的原生CF数组,然后使用serializeJSON()将其转换为JSON字符串。通过尝试手工编写JSON字符串,这将有助于减少遇到的任何问题。