te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - JSON that contains functions - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON that contains functions - Stack Overflow

programmeradmin3浏览0评论

I have a website that returns a JSON-like data structure like this:

{
    "name":"tom jones",
    "no": 123,
     "storedproc": function(){
                      callbuyer(0123);
                    }
}

I'm getting this data using $.ajax() with dataType "JSON". Unfortunately, my $.ajax() calls the error callback because my data contains a function(). How can I parse this correctly? I really need to store the function in a variable and call it later.

I have a website that returns a JSON-like data structure like this:

{
    "name":"tom jones",
    "no": 123,
     "storedproc": function(){
                      callbuyer(0123);
                    }
}

I'm getting this data using $.ajax() with dataType "JSON". Unfortunately, my $.ajax() calls the error callback because my data contains a function(). How can I parse this correctly? I really need to store the function in a variable and call it later.

Share Improve this question edited Oct 9, 2016 at 9:44 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Jul 19, 2012 at 7:45 NetoricaNetorica 19.3k19 gold badges77 silver badges109 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 7

That is simply not legal JSON (as you know given the title of the question) See the offical JSON syntax. The nice thing about real JSON is that one can use JSON.parse which safely wraps an eval call.

While eval could be used, I would suggest revisiting the architecture of your application and find some other way to do what you are trying to do.

In particular, I would have the server return the 0123 only, and let your client keep the logic that lets it know, in certain cases, which functions apply (in the scenario here, the function would be callbuyer).

This should work because you say you want to call the function which is the value of the storedproc later. Since the body of this function contains a call to callbuyer it follows that your client side script knows what callbuyer is. The trick is for your server not to send back arbitrary, unconstrained functions, but rather data that your client can exploit somehow using the knowledge it has about the overall application.

Could you arrange to have the server return JSON like this:

{"name":"tom jones",
  "no": 123,
 "storeprocFn": callbuyer,
 "arg": "0123"};

Then your callback function can call the callbuyer function and pass arg

Use eval to interpret the string as a javascript object. You won't be able to use the JSON data type though. I believe what you need to do is use 'text' as the dataType for the $.ajax call. Then do something like:

var data = eval('(' + text + ')');

Should work. Of course, eval is evil. But it would solve your problem. As long as you can guarantee there isn't anything malicious in the text (no unsanitized, user entered data) then you should be ok.

AFAIK, functions are left out when using JSON.stringify, it's just not meant to be used to clone full objects (props and methods). However, you might be able to pass the function body as a string.
Say you decide on a string format like func=>var foo = 'bar'; return foo;. This should be passed as a regular JSON string, after parsing the object you could then iterate all properties, and convert those strings to functions like so:

for (var prop in parsedObj)
{
    if (parsedObj.hasOwnProperty(prop) && parsedObj[prop].match(/^func\=\>/))
    {
        parsedObj[prop] = new Function(parsedObj[prop].replace('func=>',''));
    }
}

Though, seriously, I'd say you might want to rethink your approach, this is not what JSON is for. It's unsafe, all JSON strings are eval'ed, after having made sure they contain no harmful code. This approach is creating a loophole/vulnerability that the JSON people worked hard for to seal off.

For your example will this work: 'user.storeproc = function() { callbuyer( user.no);};'

The Var 'user' is the object of the parsed json.

Ps: maybe you have to format user.no, from 123 to 0123

Following JSON extension, "JFON", does transport of functions and array-properties.
JFON uses eval and is intended for case if:

1) your data is from trusted source ( like not-derived from user input or is a code from your own server), and
2) you know there are no undesired side effects with context of "eval"
(it is a context of eval in function "fromJFON", line 127 )
3) it is costly to refactor your app to use "functionless" JSON;
4) JFON is one-day work, so may be needs more testing;

The idea: use selected property name to escape functions and arrays like
in strings when selected character "\" is used to pass \n and \ for itself.

In JFON, name "wrap" is selected to pass functions and itself: "wrap" : { "fun" : ... and "wrap" : { "esc" : ...

demo: http://landkey/Sandbox/z/spaceen86/js/btb/tests/jfon.htm

code ( use mit 0.0.86 ):
https://github./lancelab/spaceen/blob/master/js/btb/JFON.js
test: github./lancelab/spaceen/blob/master/js/btb/tests/jfon.htm

Here is another, "JWON" extension: JSON-ments, here-documents, monkey-patching of JSONs:
github./lancelab/Boardspirator/blob/master/diary/play/tp/jwon.js

发布评论

评论列表(0)

  1. 暂无评论