功能JSON格式 - 可能吗? [英] function in JSON format - possible?
问题描述
我有以下的JSON(被剪断空间),你可以在测试看和工具提示我有一个需要包含一个函数格式化(注意,这JSON是从在读的属性XML文件,并转换成JSON在.NET)
I have the below JSON (been snipped for space), as you can see in the "test" and "tooltip" I have a property that needs to contain a function "formatter" (note this JSON is read in from an XML file and converted to JSON in .NET)
{
"test": {
"formatter": function(){return '<b>'+ this.point.name +'<\/b>: '+ this.y +' %';}
},
"title": {
"align": "center",
"text": "Your chart title here"
},
"tooltip": {
"formatter": function(){return '<b>'+ this.point.name +'<\/b>: '+ this.y +' %';}
}
}
不幸的是我收到产生JSON文件ASPX页面上的错误
Unfortunatly I'm getting an error on the ASPX page that produces the JSON file
There was an error parsing the JSON document. The document may not be well-formed.
此错误是由于一个事实,即格式化之后,位在引号,因为它认为这是一个字符串。但如果我把它周围的字符串,那么使用JSON不看功能前端的HTML页面。
This error is due to the fact that the bit after the "formatter" is not in quotation marks as it thinks it's a string. but If I put a string around it then the front end html page that uses the JSON doesn't see the function.
是否有可能通过这一个功能,而不是一个字符串?
Is it possible to pass this as a function and not a string?
非常感谢。
编辑:
感谢您的快速回信。正如我所说的,我知道的是,以上是不正确的JSON由于事实,即功能(){...}部分未在引号。前端读取JSON文件是第三方,所以我想知道我怎么可以通过传递函数,我都懂注入的问题(从一个SQL点)和明白为什么这是不可能的JSON(有没有工作JSON之前)。
Thanks for the quick replys. As I said I know that the above isn't correct JSON due to the fact that the "function(){...}" part isn't in quote marks. The front end that reads the JSON file is 3rd party so I was wondering how I could pass the function through, I understand about the problems of injection (from a SQL point of view) and understand why it's not possible in JSON (not worked with JSON before).
推荐答案
如果您通过它,你的字符串可以的使用的Javascript功能EVAL,但EVAL是邪恶的。
If you passed it as a string you could use Javascripts EVAL function, but EVAL is EVIL.
怎么样满足其一半的方式,并使用对象标注格式? 这是我在工作中使用的模板jQuery插件,在$ .fn.extend显示了这种标记格式。
What about meeting it half way and using Object Notation format ? This is a template jquery plugin that I use at work, the $.fn.extend shows this notation format.
/*jslint browser: true */
/*global window: true, jQuery: true, $: true */
(function($) {
var MyPlugin = function(elem, options) {
// This lets us pass multiple optional parameters to your plugin
var defaults = {
'text' : '<b>Hello, World!</b>',
'anotherOption' : 'Test Plugin'
};
// This merges the passed options with the defaults
// so we always have a value
this.options = $.extend(defaults, options);
this.element = elem;
};
// Use function prototypes, it's a lot faster.
// Loads of sources as to why on the 'tinternet
MyPlugin.prototype.Setup = function()
{
// run Init code
$(this.element).html(this.options.text);
};
// This actually registers a plugin into jQuiery
$.fn.extend({
// by adding a jquery.testPlugin function that takes a
// variable list of options
testPlugin: function(options) {
// and this handles that you may be running
// this over multiple elements
return this.each(function() {
var o = options;
// You can use element.Data to cache
// your plugin activation stopping
// running it again;
// this is probably the easiest way to
// check that your calls won't walk all
// over the dom.
var element = $(this);
if (element.data('someIdentifier'))
{
return;
}
// Initialise our plugin
var obj = new MyPlugin(element, o);
// Cache it to the DOM object
element.data('someIdentifier', obj);
// Call our Setup function as mentioned above.
obj.Setup();
});
}
});
})(jQuery);
这篇关于功能JSON格式 - 可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!