如何在JSON字符串中包含Javascript字符串构造函数?

如何在JSON字符串中包含Javascript字符串构造函数?,javascript,jquery,html,json,Javascript,Jquery,Html,Json,我正在构建Jquery移动列表视图的模板。要使模板完全通用,我需要传递listitem文本。因此,当前我的listview配置如下所示: <ul data-role="listview" data-create="false" class="template" data-config='{ "type":"listview", "data":"getRetailers", "custom_classes":["embedded_filter updateResult

我正在构建Jquery移动列表视图的模板。要使模板完全通用,我需要传递listitem文本。因此,当前我的listview配置如下所示:

<ul data-role="listview" data-create="false" class="template" data-config='{
    "type":"listview",
    "data":"getRetailers",
    "custom_classes":["embedded_filter updateResults f-875","nMT pickList f-875 widget_listview","f-n",""],
    "lib":"static_listview.html",
    "tmp":"tmp_listview_inset",
    "lang":"locale_search",
    ...
    }'></ul>
...
"text":"inv.company+', '+ inv.zip + ', ' + inv.city"
}
"text": function(){ return inv.company+', '+ inv.zip + ', ' + inv.city }
但是像这样插入它:

<ul data-role="listview" data-create="false" class="template" data-config='{
    "type":"listview",
    "data":"getRetailers",
    "custom_classes":["embedded_filter updateResults f-875","nMT pickList f-875 widget_listview","f-n",""],
    "lib":"static_listview.html",
    "tmp":"tmp_listview_inset",
    "lang":"locale_search",
    ...
    }'></ul>
...
"text":"inv.company+', '+ inv.zip + ', ' + inv.city"
}
"text": function(){ return inv.company+', '+ inv.zip + ', ' + inv.city }
不起作用

问题

如何在JSON中包含Javascript构造函数

通常您会这样做:

<ul data-role="listview" data-create="false" class="template" data-config='{
    "type":"listview",
    "data":"getRetailers",
    "custom_classes":["embedded_filter updateResults f-875","nMT pickList f-875 widget_listview","f-n",""],
    "lib":"static_listview.html",
    "tmp":"tmp_listview_inset",
    "lang":"locale_search",
    ...
    }'></ul>
...
"text":"inv.company+', '+ inv.zip + ', ' + inv.city"
}
"text": function(){ return inv.company+', '+ inv.zip + ', ' + inv.city }
但我不确定它是否有效。您可以尝试任何方式。

尝试以下代码:

"text":"'"+ inv.company+ "', '"+ inv.zip + "','" + inv.city+ "'";

嗯。不太好,但很有效:

1) 在我的JSON中,我只传递一个函数名,如下所示:

<ul data-role="listview" data-create="false" class="template" data-config='{  
    "type":"listview",
    "data":"getRetailers",
    "custom_classes":["embedded_filter updateResults f-875","nMT pickList f-875 widget_listview","f-n",""],
    "lib":"static_listview.html",
    "tmp":"tmp_listview_inset",
    "lang":"locale_search",
    "text":"buildRetailers",
    ...
}'></ul>
3) 并从我的listview字符串生成器调用:

....
listItem = listItem.replace( widget.options.re_theme, li_theme );
listItem = listItem.replace( widget.options.re_iconpos, li_iconpos );
listItem = listItem.replace( widget.options.re_icon, li_icon );
// call the function to return the list item text
listItem = listItem.replace( widget.options.re_text, dynoData[ li_text ](inv) );

所以我似乎无法在JSON配置中传递函数字符串,所以我需要添加一个函数来构造我所需要的。如果有人知道更好的方法,请张贴。我将进行测试和检查。

您不能通过inv对象的对象文字版本吗

"text" : {
    "company": "some value here",
    "zip": "another value here",
    "city": "another value"
}

JSON中没有函数。哦,那么我在JavaScript端所做的所有工作都是针对宇宙的。这是圣诞节的奇迹。你可能已经想到了对象文字。JSON源于它们的语法,但它们并不相同。您可以在这里看到,JSON中没有函数的语法:。不需要snide。唯一的另一种方法是在JSON双引号文本中编写函数并使用
eval
,但
eval
最好避免使用,因为
inv
包含大约30个字段,我必须循环使用它们来创建地址列表。如果是单个项目,那么是的,我可能会这样做。