I am looking to create a开发者_运维问答n HTML version of a JSON, through JavaScript(jQuery), specifically the unofficial Google Dictionary JSON (http://googlesystem.blogspot.com/2009/12/on-googles-unofficial-dictionary-api.html).
The problem I am facing is that the JSON is not readable because it has this in front of it "dict_api.callbacks.id100(
" and is trailed by ",200,null)
".
How can I remove this and then put it into a JSON object such that I can attach the elements to the HTML. Thanks in advance.
This is a JSONP response.
It calls a function specified by the callback
parameter to give you the response.
You should create a global function to handle the response, and pass its name as the callback
.
jQuery will do this for you:
$.getJSON(
"http://www.google.com/dictionary/json?q=test&sl=en&tl=en&restrict=pr%2Cde&client=te&callback=?",
function(response) {
alert(response);
}
)
One thing you can do is write a regular expression (or a pair of them) to do something like:
string.replace(/Regular expression that matches start/, "") string.replace(/Regulat expression that matches the finish/, "")
Be careful not to use greedy regular expressions if you go this route, I have seen entertaining-but-not-very-helpful results with greed exressions in cases like this.
Otherwise, I would definitely go with the other response by SLaks related to using JQuery, as it should also give you additonal abilities to work with the data later, should your need change after the fact.
精彩评论