簡體   English   中英

無法解析從頻道發送的json

[英]Cannot parse json sent from a channel

好的,在我的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方法不起作用。 沒有警報彈出。 如果我刪除分析行並將其放置,則彈出警報,如果我只是更改警報並保持分析,它仍然無濟於事;

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

那么idk為什么它不正確解析。 我假設它與報價有關。 我想我又遇到麻煩了。

UPDATE

請看下面我的回答,我解決了問題。

所以我這樣解決了這個問題;

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

看起來m.data畢竟不是字符串,因此您需要進行強制轉換。

也許嘗試:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM