dongye9991 2015-06-11 17:50
浏览 34

清理JSON数据以用作JavaScript对象

I'm going to be dynamically generating a JSON file which is then passed to SCEditor as the emoticons object; this data will come from the database, so essentially it should be safe, but one can never be 100% sure.

This is how it is being called:

// Create var to store emoticons
var emoticons = false;

$.getJSON('../../images/emoticons/default/emoticons.json')
.done(function(response) {
    emoticons = response;
    console.log(emoticons);
})
.always(function() {
    // always initialize sceditor
    $(".sceditor").sceditor({
        // Other options.....
        plugins: "bbcode",
        emoticons: emoticons,
    });
});

An example of the JSON file would look like:

{
    "dropdown": {
        ":)": "smile.png",
        ":angel:": "angel.png",
        ":angry:": "angry.png",
        "8-)": "cool.png",
        ":'(": "cwy.png",
    }
}

So the emoticon code and filename are pulled from the database. Is there anything I need to do here other than escape double quotes? Whilst this data will be coming from the database, it's possible the codes/filenames will be provided by the user.

When I store them in the database I will be stripping tags with PHP's strip_tags function.

I wanted to avoid turning the data into html entities as it doesn't seem to play nice with the editor as it doesn't turn the emoticons into smileys within the editor if you say set the code as :") - it will literally output in the editor as :") rather than show the smiley.

Edit: To see an example of how the code is used check out the SCEditor demo. Only difference is the demo uses the default codes provided within the JS file itself and mine will be provided via a JSON file passed as an option.

What is are my best options here?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!