【发布时间】:2009-04-13 16:30:38
【问题描述】:
我正在将 JSON 用于我正在开发的 Web 应用程序。但是由于各种原因,我需要根据服务调用的 JSON 响应创建已经在客户端脚本上定义的“对象”。为此,我想使用正则表达式将“新”语句插入 JSON 响应中。
function Customer(cust)
{
this.Name = null;
this.ReferencedBy = null;
this.Address = null;
if (cust != null)
{
this.Name = cust.Name;
this.ReferencedBy = cust.ReferencedBy;
this.Address = cust.Address;
}
}
JSON 响应由 ASP.NET AJAX 服务返回,它包含一个“__type”成员,可用于确定对象类型并插入“new”语句。
示例 JSON:
{"__type":"Customer", "ReferencedBy":{"__type":"Customer", "Name":"Rita"}, "Name":"Joseph", "Address":"123 {drive}"}
生成的字符串如下所示:
new Customer({"ReferencedBy":new Customer({"Name":"Rita"}), "Name":Joseph", "Address":"123 {drive}"})
到目前为止,我得到了这个,但它不适用于 ReferencedBy 成员。
匹配:
({"__type":"Customer",)(.*?})
替换:
new Customer({$2})
【问题讨论】:
标签: regex json asp.net-ajax