1

好的,在我的 python 代码中。我有这个

   foo = "aaa"; //foo and bar are variables that change, just an example here
   bar = "bbb";
   json = {"here": foo, "there": bar} //also tried single quotes
   message = simplejson.dumps(json) //also tried just json instead of simplejson
   channel.send_message(user_id(), message)

在 JavaScript 中

 onMessage = function(m) {
    var a = JSON.parse(m.data);
    alert(a.here); // foo should pop up but it doesnt
  }

似乎 parse 方法不起作用。没有弹出警报。如果我删除 parse line 并把它比 alert 弹出,如果我只是更改 alert 并继续解析它仍然没有做任何事情;

alert(m.data) // this prints out {"here": "aaa", "there": "bbb"}

所以我知道为什么它解析不正确。我假设它与引号有关。我想我又遇到了他们的麻烦。

更新

请看下面我的回答,我解决了这个问题。

4

2 回答 2

1

所以我这样解决了这个问题;

var a = JSON.parse(String(m.data));

看起来 m.data 毕竟不是字符串,所以你需要转换它。

于 2013-02-16T23:00:40.057 回答
0

也许试试:

alert (m.data['here']);

于 2013-02-16T16:41:42.067 回答