2010-02-16 29 views
1

我想知道爲什麼我不能解碼這個json字符串,所有我想要的是將其轉換爲適當的Ruby哈希,任何人有一個想法?我認爲這個對象陣列正在破解它?Activesupport :: JSON.decode在此崩潰,

參數:

{"{\"origins\":"=>{"{\"origin\":\"this\"},{\"origin\":\"dont\"},{\"origin\":\"dont me please\"},{\"origin\":\"and me please\"},{\"origin\":\"dont\"},{\"origin\":\"dont\"},{\"origin\":\"dont\"},{\"origin\":\"okay\"},{\"origin\":\"dont\"},{\"origin\":\"go\"},{\"origin\":\"go\"}"=>{",\"url\":\"file:///Users/waheed/Desktop/untitled.html\",\"apik\":\"helloapik\",\"host\":\"http://localhost:3000/\"}"=>nil}}} 
現在

在我的JavaScript即時通訊做這個

//這是對象我嘗試使用具有該字符串化方法JSON.org庫和XMLHttpRequest和IM發送

function tObject(origins,url,apik){ 
    this.origins=origins; //this is an array of string 
    this.url=url; 
    this.apik=apik; 
} 

    var t = new tObject(myStringArr,"www.foo.com","welcome guys"); 

ajax = new Ajax(); //this is an xhcon class you dont worry about it url here http://xkr.us/code/javascript/XHConn/ 
ajax.connect("http://localhost:3000/","POST",JSON.stringify(t), callback); 

在我的rails應用程序中已發佈的參數如下所示:

參數{「{\」origins \「:」=> {「{\」origin \「:\」this \「},{\」origin \「:\」yo yo \「},{\」原產地「:\」我請\「},{\」原產地\「:\」和我請\「},{\」origin \「:\」here \ 「和\」},{\「origin \」:\「again \」},{\「origin \」:\「okay \」},{\「origin \」:\「yes \」},{ \ 「出身\」:\ 「走出去\」},{\ 「出身\」:\ 「走出去\」} 「=> {」,\ 「URL \」:\ 「www.foo.com \」,\」 apik \「:\」welcome guys \「}」=> nil}}}

爲什麼最後的結果爲零?我試圖解碼它,但它不能工作,因爲它責怪字符串不JSON字符串?!!?

TIA,waheedi

+0

你從哪裏得到上面的字符串?它是來自控制檯的「檢查過的」字符串嗎?或RAILS_DEFAULT_LOGGER.info(params.to_s)? – Matt

+0

這是什麼rails默認的日誌記錄器登錄控制檯時,發生請求 – Waheedi

+0

我認爲這裏的麻煩是不是一個字符串,它不能被解析。它看起來有點像JSON,但只是模糊不清,因爲很多格式都是關閉的。這是一個哈希嵌套的哈希,它們中的很多包含不正確的鍵或值,如「{」originins「:」,它本身是不完整的。我不確定你從哪裏得到這個信息,但是有些東西已經解析了一下,並把它留在了這個破碎的狀態。 – tadman

回答

0

也許你粘貼很糟糕,因爲我得到這個錯誤甚至嘗試加載該字符串:

syntax error, unexpected tASSOC, expecting $end 
s = "{\"origins\":"=>{"{\"origin\":\"this\"},{\"ori... 
        ^
+0

對不起,我已經從控制檯複製它,因爲它是現在, – Waheedi

0

這不是一個有效的JSON字符串。對於JSON,nil和=>都是無效的值。

+0

我已經更新了我的帖子,我真的很困惑什麼是造成這? – Waheedi