-4
我想要替换JSON对象内的字符串。 我的JSON对象看起来是这样的:如何替换JSON对象输出中的字符串?
{
"type": "text",
"timeTrigger": 34,
"title": "Sherlock Holmes",
"subtitle": "Detective",
"picture": "images/content/placehold.png",
"intro": "Sherlock Holmes is a fictional detective created by British author Sir Arthur Conan Doyle. [gallery] Holmes is known for his astute logical reasoning..."
},
我想换成是标签[gallery]
到图像对象。
我尝试这样做:
var mystring = data.text;
mystring.replace('[gallery]' , 'replaced!');
这:
var stringified = JSON.stringify(json);
stringified = stringified.replace('"[gallery]": "replaced"');
双方将导致没有任何改变。最后一个,我认为只有当您想要更改JSON中的键>值时才使用,但我只想过滤掉图库标记并将其替换为图像。因此输出将会将[gallery]
替换为图像对象。
欢迎任何建议和代码。 在此先感谢。
UPDATE
脚本波纹管将替换包含[gallery]
新的东西的data.text,但这只适用于的console.log而不是在页面上相同的文字..
var mystring = data.text;
console.log(mystring.replace('[gallery]', '=========dsiuhfodsa========='));
是。是的,这在现实领域当然是可能的。 – deceze
这不是JSON或JavaScript的功能。有很多模板语言(用JS编写的编译器)可让您以字符串的形式表示模板,并且可以将字符串存储为JSON。 – Quentin
你的问题是*实际*“是否可以替换字符串中的文本”(谷歌“替换字符串JavaScript”),答案是显而易见的是。无论数据来源如何,它都不会改变问题的简单性。 – h2ooooooo