Json对象替换字符串占位符实现代码
更新时间:2010年11月17日 18:35:05 作者:
实现根据提供的Json对象去替换字符串中相应的占位符。需要的朋友可以参考下。
例如:
含有占位符的字符串hello,{name},your birthday is {birthday };
提供的Json对象{name: "czonechan", birthday : "1989-07-02" } ;
替换后为 hello,czonechan,your birthday is 1989-07-02。
实现代码:
Object.prototype.jsonToString=function(str) {
o=this;
return str.replace(/\{\w*\}/g, function (w) {
r = w.substr(1,w.length-2);//去除{}
return (o[r]===0)?0:(o[r] ? o[r] : "");//o[r]===0这句是为了实现当值为0时输出0而不是空。
});
};
含有占位符的字符串hello,{name},your birthday is {birthday };
提供的Json对象{name: "czonechan", birthday : "1989-07-02" } ;
替换后为 hello,czonechan,your birthday is 1989-07-02。
实现代码:
复制代码 代码如下:
Object.prototype.jsonToString=function(str) {
o=this;
return str.replace(/\{\w*\}/g, function (w) {
r = w.substr(1,w.length-2);//去除{}
return (o[r]===0)?0:(o[r] ? o[r] : "");//o[r]===0这句是为了实现当值为0时输出0而不是空。
});
};
相关文章
Jquery+asp.net后台数据传到前台js进行解析的方法
我们经常用jquery读取后台数据,后台返回数据。后台数据格式就有很多了,但是js里面没有什么类型之分2014-05-05


最新评论