Home  >  Article  >  Web Front-end  >  Json object replacement string placeholder implementation code_json

Json object replacement string placeholder implementation code_json

WBOY
WBOYOriginal
2016-05-16 18:16:242116browse

For example:

String containing placeholders hello, {name}, your birthday is {birthday };
Provided Json object {name: "czonechan", birthday : "1989-07-02 " } ;
  Replaced with hello, czonechan, your birthday is 1989-07-02.

Implementation code:

Copy code The code is as follows:

Object.prototype .jsonToString=function(str) {
o=this;
return str.replace(/{w*}/g, function (w) {
r = w.substr(1,w.length -2);//Remove {}
return (o[r]===0)?0:(o[r] ? o[r] : "");//o[r]=== The purpose of 0 is to output 0 instead of empty when the value is 0.
});
};
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn