I am using axios to retrieve data from Wikipedia Api. This is the code I have writtent.
let axiosData = function(){
let searchString = $('#searchString').val();
console.log(searchString);
let Url = "https://en.wikipedia.org/w/api.php?action=opensearch&search="+ searchString +
"&origin=*&callback=";
axios.get(Url)
.then(function(res){
var linkLists = res.data;
console.log(linkLists);
})
.catch(function(){
console.log("Error")
});
return false;
}
$('form').submit(axiosData);
I am able to get an output when I console log it. Which is the following: In this case I am searching for the name Jon Snow. How am able to access the json?
/**/(["jon snow",["Jon Snow (character)","Jon Snow (journalist)","Jon Snow","John Snow (cricketer)","Jon Snoddy","John Snow","Jon Snodgrass (musician)","Jon Snodgrass","John Snow College, Durham","John Snow, Inc"],["Jon Snow is a fictional character in the A Song of Ice and Fire series of fantasy novels by American author George R. R.","Jonathan George Snow HonFRIBA (born 28 September 1947) is an English journalist and television presenter.","Jon Snow may refer to:","John Augustine Snow (born 13 October 1941) is a retired English cricketer. He played for Sussex and England in the 1960s and 1970s.","Jon Snoddy is an American technology expert who is currently the Advanced Development Studio Executive SVP at Walt Disney Imagineering.","John Snow (15 March 1813 \u2013 16 June 1858) was an English physician and a leader in the development of anaesthesia and medical hygiene.","Jon Snodgrass is \"the guy with the glasses from Drag the River\".","Jon Snodgrass is a Panamanian author, born on July 27, 1941 in Col\u00f3n, Panama to John Alphonso and Olivia Jane (Chestnut) Snodgrass.","John Snow College is one of 16 constituent colleges of the University of Durham in England. The College takes its name from the nineteenth-century Yorkshire physician Dr John Snow.","John Snow, Inc. (JSI) is a public health research and consulting firm in the United States and around the world."],["https://en.wikipedia.org/wiki/Jon_Snow_(character)","https://en.wikipedia.org/wiki/Jon_Snow_(journalist)","https://en.wikipedia.org/wiki/Jon_Snow","https://en.wikipedia.org/wiki/John_Snow_(cricketer)","https://en.wikipedia.org/wiki/Jon_Snoddy","https://en.wikipedia.org/wiki/John_Snow","https://en.wikipedia.org/wiki/Jon_Snodgrass_(musician)","https://en.wikipedia.org/wiki/Jon_Snodgrass","https://en.wikipedia.org/wiki/John_Snow_College,_Durham","https://en.wikipedia.org/wiki/John_Snow,_Inc"]])
At The beginning of the json has '/**/(' and at the end ')' characters which have cut by substring. After parsed it.
let Url = "https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=jon%20snow&origin=*&callback=";
axios.get(Url)
.then(function(res){
console.log(res);
var linkLists = JSON.parse(res.data.substring(5, res.data.length-1));
console.log(linkLists)
})
.catch(function(){
console.log("Error...")
});
If res.data is the JSON response for your http request you can parse that into JSON with the JSON.parse() function
var linkJSON = JSON.parse(res.data)
That will give you the json data in the linkJSON object.
Related
I'm using bert pre-trained model for question and answering. It's returning correct result but with lot of spaces between the text
The code is below :
def get_answer_using_bert(question, reference_text):
bert_model = BertForQuestionAnswering.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad')
bert_tokenizer = BertTokenizer.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad')
input_ids = bert_tokenizer.encode(question, reference_text)
input_tokens = bert_tokenizer.convert_ids_to_tokens(input_ids)
sep_location = input_ids.index(bert_tokenizer.sep_token_id)
first_seg_len, second_seg_len = sep_location + 1, len(input_ids) - (sep_location + 1)
seg_embedding = [0] * first_seg_len + [1] * second_seg_len
model_scores = bert_model(torch.tensor([input_ids]),
token_type_ids=torch.tensor([seg_embedding]))
ans_start_loc, ans_end_loc = torch.argmax(model_scores[0]), torch.argmax(model_scores[1])
result = ' '.join(input_tokens[ans_start_loc:ans_end_loc + 1])
result = result.replace('#', '')
return result
Followed by code below :
reference_text = 'Mukesh Dhirubhai Ambani was born on 19 April 1957 in the British Crown colony of Aden (present-day Yemen) to Dhirubhai Ambani and Kokilaben Ambani. He has a younger brother Anil Ambani and two sisters, Nina Bhadrashyam Kothari and Dipti Dattaraj Salgaonkar. Ambani lived only briefly in Yemen, because his father decided to move back to India in 1958 to start a trading business that focused on spices and textiles. The latter was originally named Vimal but later changed to Only Vimal His family lived in a modest two-bedroom apartment in Bhuleshwar, Mumbai until the 1970s. The family financial status slightly improved when they moved to India but Ambani still lived in a communal society, used public transportation, and never received an allowance. Dhirubhai later purchased a 14-floor apartment block called Sea Wind in Colaba, where, until recently, Ambani and his brother lived with their families on different floors.'
question = 'What is the name of mukesh ambani brother?'
get_answer_using_bert(question, reference_text)
And the output is :
'an il am ban i'
Can anyone help me how to fix this issue. It would be really helpful.
You can just use the tokenizer decode function:
bert_tokenizer.decode(input_ids[ans_start_loc:ans_end_loc +1])
Output:
'anil ambani'
In case you do not want to use decode, you can use:
result.replace(' ##', '')
I am trying to create a dataframe out of nested JSON files and am running into issues. So far, I've tried using tidyjson to stack the different columns like author, source, etc. However, when I get to "readers" (see the second JSON record), instead of column headers proceeding the ":" there is a unique id. I can't figure out how to get around that and create a dataframe. Here is the code I have so far:
art_df <- artJSON %>%
as.tbl_json %>%
gather_array %>%
enter_object("articles") %>% gather_array %>% # stack the articles
enter_object("author") %>% gather_array %>% # stack the authors
spread_values(articles.author = jstring("author")) %>% # extract item name and price
spread_values(articles.date = jstring("date")) %>%
) %>%
And the JSON file is structured like:
"articles" : {
"-2145189873" : {
"author" : "",
"date" : "",
"partialRecord" : true,
"source" : "reuters",
"text" : "",
"title" : "U.S. government loses to Russia's disinformation campaign: advisers | Reuters",
"url" : "reuters.com/article/us-usa-russia-disinformation-analysis-idUSKBN1492PA"
},
"-2143576379" : {
"author" : [ "", "", "CNN" ],
"date" : "Updated 8:21 AM ET, Mon June 19, 2017",
"readers" : {
"112851530401572877131" : true,
"115171523766335420644" : true
},
"source" : "cnn",
"text" : "Story highlightsThe higher the pressure, the more defiant the President becomesTrump continues to call the Russia probe a 'witch hunt' Washington (CNN)America is edging ever closer to a new long, national nightmare, given President Donald Trump's explosive response to signs that special counsel Robert Mueller is approaching the epicenter of White House power.The investigation has taken a serious turn in recent days, raising the clear possibility that whether Mueller finds wrongdoing or not, a period of political stress and upheaval is inevitable. Trump appears willing to test the bounds of convention and his own powers against a legal and political establishment that he believes is conspiring against him. The higher the pressure, the more defiant the President becomes -- even if it puts him at odds with his legal team.Trump has decided that \"this is a political fight and he's going to fight it,\" a White House official told CNN's Jeff Zeleny.But the sense of chaos around the White House is deepening. The President again called the Russia probe a \"witch hunt\" over the weekend while a legal adviser tried to walk back Trump's admission Friday that he is under investigation.Read More\"The MAKE AMERICA GREAT AGAIN agenda is doing very well despite the distraction of the Witch Hunt,\" Trump tweeted on Sunday.var id = '//platform.twitter.com/widgets.js'.replace(/\\s+/g, '');!!document.getElementById(id) || (function makeEmbedScript(d, id) (document, id));The MAKE AMERICA GREAT AGAIN agenda is doing very well despite the distraction of the Witch Hunt. Many new jobs, high business enthusiasm,..— Donald J. Trump (#realDonaldTrump) June 18, 2017The political stakes rose significantly in recent days amid signs the probe on Russian election hacking could eventually expand to consider Trump's actions.The President and his allies have declared open season on Mueller and Deputy Attorney General Rod Rosenstein, who is overseeing the probe. \"You are witnessing the single greatest WITCH HUNT in American political history - led by some very bad and conflicted people! #MAGA.\" Trump said in a tweet last week that signified an attempt to undermine both men./* global CNN, Modernizr */'use strict';CNN.INJECTOR.executeFeature('video').then(function () {CNN.VideoPlayer.handleUnmutePlayer = function handleUnmutePlayer(containerId, dataObj) {'use strict';var playerInstance,playerPropertyObj,rememberTime,unmuteCTA,unmuteIdSelector = 'unmute_' + containerId,isPlayerMute;dataObj = dataObj || {};if (CNN.VideoPlayer.getLibraryName(containerId) === 'fave') else isPlayerMute = (typeof dataObj.muted === 'boolean') ? dataObj.muted : false;if (CNN.VideoPlayer.playerProperties && CNN.VideoPlayer.playerProperties[containerId]) if (playerPropertyObj.mute && playerPropertyObj.contentPlayed) };CNN.VideoPlayer.showFlashSlate = function showFlashSlate(container) ;CNN.autoPlayVideoExist = (CNN.autoPlayVideoExist === true) ? true : false;var configObj = ,autoStartVideo = false,callbackObj,containerEl,currentVideoCollection = [],currentVideoCollectionId = '',isLivePlayer = false,moveToNextTimeout,mutePlayerEnabled = false,nextVideoId = '',nextVideoUrl = '',turnOnFlashMessaging = false,videoPinner,videoEndSlateImpl;if (CNN.autoPlayVideoExist === false) configObj.autostart = autoStartVideo;CNN.VideoPlayer.setPlayerProperties(configObj.markupId, autoStartVideo, isLivePlayer, mutePlayerEnabled);CNN.VideoPlayer.setFirstVideoInCollection(currentVideoCollection, configObj.markupId);videoEndSlateImpl = new CNN.VideoEndSlate('body-text_15');/*** Finds the next video ID and URL in the current collection, if available.* #param currentVideoId The video that is currently playing* #param containerId The parent container Id of the video element*/function findNextVideo(currentVideoId) findNextVideo('tv/2017/06/18/reliable-sources-journalists-trump-twitter-habit.cnn');/*** Navigates to the video leaf page of the next video in the current collection, if available.* #param currentVideoId The video that is currently playing* #param containerId The parent container Id of the video element*/function navigateToNextVideo(currentVideoId, containerId) callbackObj = ;if (typeof configObj.context !== 'string' || configObj.context.length 0) CNN.autoPlayVideoExist = (CNN.autoPlayVideoExist === true) ? true : false;CNN.VideoPlayer.getLibrary(configObj, callbackObj, isLivePlayer);});/* videodemanddust is a default feature of the injector */CNN.INJECTOR.scriptComplete('videodemanddust');JUST WATCHEDWhy journalists like Trump's Twitter habitReplayMore Videos ...MUST WATCHWhy journalists like Trump's Twitter habit 05:59The tweet sparked speculation that Trump may pull a nuclear option by firing Mueller and Rosenstein, a move senior aides have fervently advised against -- given that it could provoke a constitutional crisis.\"It looks like we have got a simmering tea kettle here, and the President seems to be testing the waters about whether he ought to go ahead and fire Robert Mueller,\" said Richard Ben-Veniste, a former Watergate prosecutor who is now a CNN legal analyst.\"You don't mess with the criminal justice system in this way ... the President is on very tenuous ground here,\" Ben-Veniste told CNN's Don Lemon Friday.Cuba to Trump: US in no 'condition to lecture us' on human rightsThe scorched-earth approach may have grave political consequences.Trump and his aides may well end up in the clear, but months of recriminations ahead will further polarize the capital and cast a shadow over his administration, which is already struggling for traction after a tumultuous first five months.If Mueller decides there is a case to answer for either the President or his aides in the issue of alleged collusion with Russia or over a potential cover-up, the political uproar will be incalculable.At best, it could further complicate Trump's efforts to pass a meaningful legislative agenda and ultimately his hopes of a successful presidency.At worst, it could force the nation to confront a third debate on whether to remove an elected President in just 45 years. The lessons of history suggest such traumas raise questions of political legitimacy and constitutional process that have the potential to sour life in Washington for years in the future./* global CNN, Modernizr */'use strict';CNN.INJECTOR.executeFeature('video').then(function () {CNN.VideoPlayer.handleUnmutePlayer = function handleUnmutePlayer(containerId, dataObj) {'use strict';var playerInstance,playerPropertyObj,rememberTime,unmuteCTA,unmuteIdSelector = 'unmute_' + containerId,isPlayerMute;dataObj = dataObj || {};if (CNN.VideoPlayer.getLibraryName(containerId) === 'fave') else isPlayerMute = (typeof dataObj.muted === 'boolean') ? dataObj.muted : false;if (CNN.VideoPlayer.playerProperties && CNN.VideoPlayer.playerProperties[containerId]) if (playerPropertyObj.mute && playerPropertyObj.contentPlayed) };CNN.VideoPlayer.showFlashSlate = function showFlashSlate(container) ;CNN.autoPlayVideoExist = (CNN.autoPlayVideoExist === true) ? true : false;var configObj = ,autoStartVideo = false,callbackObj,containerEl,currentVideoCollection = [],currentVideoCollectionId = '',isLivePlayer = false,moveToNextTimeout,mutePlayerEnabled = false,nextVideoId = '',nextVideoUrl = '',turnOnFlashMessaging = false,videoPinner,videoEndSlateImpl;if (CNN.autoPlayVideoExist === false) configObj.autostart = autoStartVideo;CNN.VideoPlayer.setPlayerProperties(configObj.markupId, autoStartVideo, isLivePlayer, mutePlayerEnabled);CNN.VideoPlayer.setFirstVideoInCollection(currentVideoCollection, configObj.markupId);videoEndSlateImpl = new CNN.VideoEndSlate('body-text_25');/*** Finds the next video ID and URL in the current collection, if available.* #param currentVideoId The video that is currently playing* #param containerId The parent container Id of the video element*/function findNextVideo(currentVideoId) findNextVideo('politics/2017/06/18/jay-sekulow-trump-obstruction-of-justice-sotu.cnn');/*** Navigates to the video leaf page of the next video in the current collection, if available.* #param currentVideoId The video that is currently playing* #param containerId The parent container Id of the video element*/function navigateToNextVideo(currentVideoId, containerId) callbackObj = ;if (typeof configObj.context !== 'string' || configObj.context.length 0) CNN.autoPlayVideoExist = (CNN.autoPlayVideoExist === true) ? true : false;CNN.VideoPlayer.getLibrary(configObj, callbackObj, isLivePlayer);});/* videodemanddust is a default feature of the injector */CNN.INJECTOR.scriptComplete('videodemanddust');JUST WATCHEDSekulow: POTUS has power to fire FBI directorReplayMore Videos ...MUST WATCHSekulow: POTUS has power to fire FBI director 00:10Trump's in 'a political fight and he's going to fight it'Back in the present, the President's mood is fueling the sense of crisis.\"One thing we've learned I think from the testimony of multiple people now is the President's pretty fired up about this, OK?\" Republican Sen. Marco Rubio said on NBC's \"Meet the Press\" Sunday.\"He, from every pronouncement we have seen, feels very strongly that he did nothing wrong, and he wants people to say that, because he feels very strongly about it,\" Rubio said. \"I don't think that's a mystery. And he's expressing himself in that way.\"But the more frustrated that Trump gets, the more he often hurts himself politically. A case in point is the firing of Comey and Trump's subsequent admission that he had Russia on his mind when he dismissed the FBI director, an admission that could provide a rationale for an obstruction of justice probe.\"Trump has a compulsion to counter-attack and is very pugnacious. I don't think it serves him well,\" former House Speaker Newt Gingrich, a Trump supporter, on ABC's \"This Week,\" adding that the President was right to be infuriated by \"this whole Russia baloney.\"/* global CNN, Modernizr */'use strict';CNN.INJECTOR.executeFeature('video').then(function () {CNN.VideoPlayer.handleUnmutePlayer = function handleUnmutePlayer(containerId, dataObj) {'use strict';var playerInstance,playerPropertyObj,rememberTime,unmuteCTA,unmuteIdSelector = 'unmute_' + containerId,isPlayerMute;dataObj = dataObj || {};if (CNN.VideoPlayer.getLibraryName(containerId) === 'fave') else isPlayerMute = (typeof dataObj.muted === 'boolean') ? dataObj.muted : false;if (CNN.VideoPlayer.playerProperties && CNN.VideoPlayer.playerProperties[containerId]) if (playerPropertyObj.mute && playerPropertyObj.contentPlayed) };CNN.VideoPlayer.showFlashSlate = function showFlashSlate(container) ;CNN.autoPlayVideoExist = (CNN.autoPlayVideoExist === true) ? true : false;var configObj = ,autoStartVideo = false,callbackObj,containerEl,currentVideoCollection = [],currentVideoCollectionId = '',isLivePlayer = false,moveToNextTimeout,mutePlayerEnabled = false,nextVideoId = '',nextVideoUrl = '',turnOnFlashMessaging = false,videoPinner,videoEndSlateImpl;if (CNN.autoPlayVideoExist === false) configObj.autostart = autoStartVideo;CNN.VideoPlayer.setPlayerProperties(configObj.markupId, autoStartVideo, isLivePlayer, mutePlayerEnabled);CNN.VideoPlayer.setFirstVideoInCollection(currentVideoCollection, configObj.markupId);videoEndSlateImpl = new CNN.VideoEndSlate('body-text_34');/*** Finds the next video ID and URL in the current collection, if available.* #param currentVideoId The video that is currently playing* #param containerId The parent container Id of the video element*/function findNextVideo(currentVideoId) findNextVideo('politics/2017/06/18/sotu-rubio-trump-talked-russia-probe-in-private.cnn');/*** Navigates to the video leaf page of the next video in the current collection, if available.* #param currentVideoId The video that is currently playing* #param containerId The parent container Id of the video element*/function navigateToNextVideo(currentVideoId, containerId) callbackObj = ;if (typeof configObj.context !== 'string' || configObj.context.length 0) CNN.autoPlayVideoExist = (CNN.autoPlayVideoExist === true) ? true : false;CNN.VideoPlayer.getLibrary(configObj, callbackObj, isLivePlayer);});/* videodemanddust is a default feature of the injector */CNN.INJECTOR.scriptComplete('videodemanddust');JUST WATCHEDRubio: Trump talked Russia probe in privateReplayMore Videos ...MUST WATCHRubio: Trump talked Russia probe in private 01:05Legal team contradicts the clientIn another sign of the gravity of the situation, Trump's legal team blitzed Sunday talk shows to contradict the President's tweet Friday where he said he is already under investigation.\"The President is not a subject or target of an investigation. That tweet was in response to a Washington Post story that ran with five unnamed sources, without identifying the agencies they represented, saying that the special counsel had broadened out his investigation to include the president,\" said Jay Sekulow, a Trump legal adviser on CNN's \"State of the Union.\"Sekulow also opened a conflict between the President's political team and his legal one, hinting that the President's tweets should be discounted even though the White House said they are an authentic expression of his views.\"It was 141 characters. There's a limitation on Twitter, as we all know,\" Sekulow said.Then, in a subsequent interview on \"Fox News Sunday,\" Sekulow sowed further confusion by saying Trump was under investigation -- then denied he said anything of the sort.Special counsel moving forwardLaw enforcement sources have told CNN that the special counsel is gathering information and considering whether there is evidence to launch a full scale obstruction investigation. Mueller's investigators have asked for information and will talk to Director of National Intelligence Dan Coats and National Security Agency Director Adm. Mike Rogers, according to a source familiar with the matter. Investigators have also sought information from recently retired NSA Deputy Richard Ledgett, according to the source. Ledgett wrote a memo, according to the source, documenting a conversation in which the president allegedly urged Rogers to help get the FBI to lift the cloud of the Russia investigation.Any confirmation that Mueller has expanded the investigation to consider whether the President obstructed justice with the Comey firing would carry no guarantee of a guilty finding.But it would deal a political blow to the White House in the battle of perception, since the idea that Trump is not under investigation has been at the centerpiece of his administration's defense strategy./* global CNN, Modernizr */'use strict';CNN.INJECTOR.executeFeature('video').then(function () {CNN.VideoPlayer.handleUnmutePlayer = function handleUnmutePlayer(containerId, dataObj) {'use strict';var playerInstance,playerPropertyObj,rememberTime,unmuteCTA,unmuteIdSelector = 'unmute_' + containerId,isPlayerMute;dataObj = dataObj || {};if (CNN.VideoPlayer.getLibraryName(containerId) === 'fave') else isPlayerMute = (typeof dataObj.muted === 'boolean') ? dataObj.muted : false;if (CNN.VideoPlayer.playerProperties && CNN.VideoPlayer.playerProperties[containerId]) if (playerPropertyObj.mute && playerPropertyObj.contentPlayed) };CNN.VideoPlayer.showFlashSlate = function showFlashSlate(container) ;CNN.autoPlayVideoExist = (CNN.autoPlayVideoExist === true) ? true : false;var configObj = ,autoStartVideo = false,callbackObj,containerEl,currentVideoCollection = [],currentVideoCollectionId = '',isLivePlayer = false,moveToNextTimeout,mutePlayerEnabled = false,nextVideoId = '',nextVideoUrl = '',turnOnFlashMessaging = false,videoPinner,videoEndSlateImpl;if (CNN.autoPlayVideoExist === false) configObj.autostart = autoStartVideo;CNN.VideoPlayer.setPlayerProperties(configObj.markupId, autoStartVideo, isLivePlayer, mutePlayerEnabled);CNN.VideoPlayer.setFirstVideoInCollection(currentVideoCollection, configObj.markupId);videoEndSlateImpl = new CNN.VideoEndSlate('body-text_48');/*** Finds the next video ID and URL in the current collection, if available.* #param currentVideoId The video that is currently playing* #param containerId The parent container Id of the video element*/function findNextVideo(currentVideoId) findNextVideo('tv/2017/06/18/inside-politics-6-18-notebooks.cnn');/*** Navigates to the video leaf page of the next video in the current collection, if available.* #param currentVideoId The video that is currently playing* #param containerId The parent container Id of the video element*/function navigateToNextVideo(currentVideoId, containerId) callbackObj = ;if (typeof configObj.context !== 'string' || configObj.context.length 0) CNN.autoPlayVideoExist = (CNN.autoPlayVideoExist === true) ? true : false;CNN.VideoPlayer.getLibrary(configObj, callbackObj, isLivePlayer);});/* videodemanddust is a default feature of the injector */CNN.INJECTOR.scriptComplete('videodemanddust');JUST WATCHEDTrump heads west to POTUS-friendly IowaReplayMore Videos ...MUST WATCHTrump heads west to POTUS-friendly Iowa 04:17",
"title" : "Trump defiant as pressure grows",
"url" : "cnn.com/2017/06/19/politics/trump-investigation/index.html"
},
Any help would be greatly appreciated. I've been stuck trying to read this file in for some time and cant figure it out. Thanks!!!
see https://rdrr.io/cran/jsonlite/man/flatten.html
data2 <- fromJSON("so.json", flatten = TRUE)
I am having issues on converting JSON data to readable text. Right now it comes out something like this:
{"id":82,"url":"http://www.tvmaze.com/shows/82/game-of-thrones","name":"Game of Thrones","type":"Scripted","language":"English","genres":["Drama","Adventure","Fantasy"],"status":"Running","runtime":60,"premiered":"2011-04-17","schedule":{"time":"21:00","days":["Sunday"]},"rating":{"average":9.3},"weight":10,"network":{"id":8,"name":"HBO","country":{"name":"United States","code":"US","timezone":"America/New_York"}},"webChannel":null,"externals":{"tvrage":24493,"thetvdb":121361,"imdb":"tt0944947"},"image":{"medium":"http://static.tvmaze.com/uploads/images/medium_portrait/53/132622.jpg","original":"http://static.tvmaze.com/uploads/images/original_untouched/53/132622.jpg"},"summary":"<p>Based on the bestselling book series <em>A Song of Ice and Fire</em> by George R.R. Martin, this sprawling new HBO drama is set in a world where summers span decades and winters can last a lifetime. From the scheming south and the savage eastern lands, to the frozen north and ancient Wall that protects the realm from the mysterious darkness beyond, the powerful families of the Seven Kingdoms are locked in a battle for the Iron Throne. This is a story of duplicity and treachery, nobility and honor, conquest and triumph. In the <em>\"Game of Thrones\"</em>, you either win or you die.</p>","updated":1485102249,"_links":{"self":{"href":"http://api.tvmaze.com/shows/82"},"previousepisode":{"href":"http://api.tvmaze.com/episodes/729575"},"nextepisode":{"href":"http://api.tvmaze.com/episodes/937256"}}}
How would I go about converting this data so I can view it as ID, Name, Genre etc.?
If you just want to print it to the console, try something like this:
var json = '{"id":82,"url":"http://www.tvmaze.com/shows/82/game-of-thrones","name":"Game of Thrones","type":"Scripted","language":"English","genres":["Drama","Adventure","Fantasy"],"status":"Running","runtime":60,"premiered":"2011-04-17","schedule":{"time":"21:00","days":["Sunday"]},"rating":{"average":9.3},"weight":10,"network":{"id":8,"name":"HBO","country":{"name":"United States","code":"US","timezone":"America/New_York"}},"webChannel":null,"externals":{"tvrage":24493,"thetvdb":121361,"imdb":"tt0944947"},"image":{"medium":"http://static.tvmaze.com/uploads/images/medium_portrait/53/132622.jpg","original":"http://static.tvmaze.com/uploads/images/original_untouched/53/132622.jpg"},"summary":"<p>Based on the bestselling book series <em>A Song of Ice and Fire</em> by George R.R. Martin, this sprawling new HBO drama is set in a world where summers span decades and winters can last a lifetime. From the scheming south and the savage eastern lands, to the frozen north and ancient Wall that protects the realm from the mysterious darkness beyond, the powerful families of the Seven Kingdoms are locked in a battle for the Iron Throne. This is a story of duplicity and treachery, nobility and honor, conquest and triumph. In the <em>Game of Thrones</em>, you either win or you die.</p>","updated":1485102249,"_links":{"self":{"href":"http://api.tvmaze.com/shows/82"},"previousepisode":{"href":"http://api.tvmaze.com/episodes/729575"},"nextepisode":{"href":"http://api.tvmaze.com/episodes/937256"}}}'
var obj = jQuery.parseJSON(json)
function printLine(obj){
for( var i in obj){
if(typeof obj[i] != 'object') console.log(i + ": " +obj[i]);
else printLine(obj[i]);
}
}
printLine(obj);
I am new to ionic framework and I had stuck in the following issue :
data.json:
{ "speakers" : [
{
"name":"Mr Bellingham",
"shortname":"Barot_Bellingham",
"reknown":"Royal Academy of Painting and Sculpture",
"bio":"Barot has just finished his final year at The Royal Academy of Painting and Sculpture, where he excelled in glass etching paintings and portraiture. Hailed as one of the most diverse artists of his generation, Barot is equally as skilled with watercolors as he is with oils, and is just as well-balanced in different subject areas. Barot's collection entitled \"The Un-Collection\" will adorn the walls of Gilbert Hall, depicting his range of skills and sensibilities - all of them, uniquely Barot, yet undeniably different"
},
{
"name":"Jonathan G. Ferrar II",
"shortname":"Jonathan_Ferrar",
"reknown":"Artist to Watch in 2012",
"bio":"The Artist to Watch in 2012 by the London Review, Johnathan has already sold one of the highest priced-commissions paid to an art student, ever on record. The piece, entitled Gratitude Resort, a work in oil and mixed media, was sold for $750,000 and Jonathan donated all the proceeds to Art for Peace, an organization that provides college art scholarships for creative children in developing nations"
},
{
"name":"Hillary Hewitt Goldwynn-Post",
"shortname":"Hillary_Goldwynn",
"reknown":"New York University",
"bio":"Hillary is a sophomore art sculpture student at New York University, and has already won all the major international prizes for new sculptors, including the Divinity Circle, the International Sculptor's Medal, and the Academy of Paris Award. Hillary's CAC exhibit features 25 abstract watercolor paintings that contain only water images including waves, deep sea, and river."
},
{
"name":"Hassum Harrod",
"shortname":"Hassum_Harrod",
"reknown":"Art College in New Dehli",
"bio":"The Art College in New Dehli has sponsored Hassum on scholarship for his entire undergraduate career at the university, seeing great promise in his contemporary paintings of landscapes - that use equal parts muted and vibrant tones, and are almost a contradiction in art. Hassum will be speaking on \"The use and absence of color in modern art\" during Thursday's agenda."
},
{
"name":"Jennifer Jerome",
"shortname":"Jennifer_Jerome",
"reknown":"New Orleans, LA",
"bio":"A native of New Orleans, much of Jennifer's work has centered around abstract images that depict flooding and rebuilding, having grown up as a teenager in the post-flood years. Despite the sadness of devastation and lives lost, Jennifer's work also depicts the hope and togetherness of a community that has persevered. Jennifer's exhibit will be discussed during Tuesday's Water in Art theme."
},
{
"name":"LaVonne L. LaRue",
"shortname":"LaVonne_LaRue",
"reknown":"Chicago, IL",
"bio":"LaVonne's giant-sized paintings all around Chicago tell the story of love, nature, and conservation - themes that are central to her heart. LaVonne will share her love and skill of graffiti art on Monday's schedule, as she starts the painting of a 20-foot high wall in the Rousseau Room of Hotel Contempo in front of a standing-room only audience in Art in Unexpected Places."
},
{
"name":"Constance Olivia Smith",
"shortname":"Constance_Smith",
"reknown":"Fullerton-Brighton-Norwell Award",
"bio":"Constance received the Fullerton-Brighton-Norwell Award for Modern Art for her mixed-media image of a tree of life, with jewel-adorned branches depicting the arms of humanity, and precious gemstone-decorated leaves representing the spouting buds of togetherness. The daughter of a New York jeweler, Constance has been salvaging the discarded remnants of her father's jewelry-making since she was five years old, and won the New York State Fair grand prize at the age of 8 years old for a gem-adorned painting of the Manhattan Bridge."
},
{
"name":"Riley Rudolph Rewington",
"shortname":"Riley_Rewington",
"reknown":"Roux Academy of Art, Media, and Design",
"bio":"A first-year student at the Roux Academy of Art, Media, and Design, Riley is already changing the face of modern art at the university. Riley's exquisite abstract pieces have no intention of ever being understood, but instead beg the viewer to dream, create, pretend, and envision with their mind's eye. Riley will be speaking on the \"Art of Abstract\" during Thursday's schedule"
},
{
"name":"Xhou Ta",
"shortname":"Xhou_Ta",
"reknown":"China International Art University",
"bio":"A senior at the China International Art University, Xhou has become well-known for his miniature sculptures, often the size of a rice granule, that are displayed by rear projection of microscope images on canvas. Xhou will discuss the art and science behind his incredibly detailed works of art."
}
]}
app.js :
angular.module('starter', ['ionic'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
.controller('ListController', ['$scope','$http',function($scope,$http){
$http.get('js/data.json').success(function(data){
$scope.artists = data;
});
}])
index.html :
<ion-content ng-controller="ListController" class="has-subheader">
<ion-list>
<ion-item ng-repeat='item in artists' class="item-thumbnail-left item-text-wrap">
<img src="img/{{item.shortname}}_tn.jpg">
<h2>{{item.shortname}}</h2>
<h3>{{item.reknown}}</h3>
<p>
{{item.bio}}
</p>
</ion-item>
</ion-list>
</ion-content>
As you all can see I am trying to list down the data from data.json file but it does not display any data. On console-Network tab I am able to see it call to data.json file but in html it is unable to display. What I am doing wrong? Please help me on this, thanks in advance
Try like this
ng-repeat='item in artists.speakers'
instead of
ng-repeat='item in artists'
Consider this string value:
LCD Soundsystem was the musical project of producer James Murphy, co-founder of dance-punk label DFA Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of alternative dance and post punk, along with elements of disco and other styles. <br />
How can all html tags be removed in Swift?
So the result has to be:
LCD Soundsystem was the musical project of producer James Murphy, co-founder of dance-punk label DFA Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of alternative dance and post punk, along with elements of disco and other styles.
You may use a regular expression, notice the one I've created:
var str = "LCD Soundsystem was the musical project of producer <a href='http://www.last.fm/music/James+Murphy' class='bbcode_artist'>James Murphy</a>, co-founder of <a href='http://www.last.fm/tag/dance-punk' class='bbcode_tag' rel='tag'>dance-punk</a> label <a href='http://www.last.fm/label/DFA' class='bbcode_label'>DFA</a> Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of <a href='http://www.last.fm/tag/alternative%20dance' class='bbcode_tag' rel='tag'>alternative dance</a> and <a href='http://www.last.fm/tag/post%20punk' class='bbcode_tag' rel='tag'>post punk</a>, along with elements of <a href='http://www.last.fm/tag/disco' class='bbcode_tag' rel='tag'>disco</a> and other styles. <br />"
let regex:NSRegularExpression = NSRegularExpression(
pattern: "<.*?>",
options: NSRegularExpressionOptions.CaseInsensitive,
error: nil)!
let range = NSMakeRange(0, countElements(str))
let htmlLessString :String = regex.stringByReplacingMatchesInString(str,
options: NSMatchingOptions.allZeros,
range:range ,
withTemplate: "")
println(htmlLessString)
It converts:
"LCD Soundsystem was the musical project of producer <a href='http://www.last.fm/music/James+Murphy' class='bbcode_artist'>James Murphy</a>, co-founder of <a href='http://www.last.fm/tag/dance-punk' class='bbcode_tag' rel='tag'>dance-punk</a> label <a href='http://www.last.fm/label/DFA' class='bbcode_label'>DFA</a> Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of <a href='http://www.last.fm/tag/alternative%20dance' class='bbcode_tag' rel='tag'>alternative dance</a> and <a href='http://www.last.fm/tag/post%20punk' class='bbcode_tag' rel='tag'>post punk</a>, along with elements of <a href='http://www.last.fm/tag/disco' class='bbcode_tag' rel='tag'>disco</a> and other styles. <br />"
to
"LCD Soundsystem was the musical project of producer James Murphy, co-founder of dance-punk label DFA Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of alternative dance and post punk, along with elements of disco and other styles."
the only thing is that I've converted all double quotes(") to single quotes and then apply the regex, otherwise I needed to escape them all using "\"
Update:
I also tried escaping all double quotes by using "\" and the result was still the same:
new string I used was:
"LCD Soundsystem was the musical project of producer James Murphy, co-founder of dance-punk label DFA Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of alternative dance and post punk, along with elements of disco and other styles. <br />"
and result:
"LCD Soundsystem was the musical project of producer James Murphy, co-founder of dance-punk label DFA Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of alternative dance and post punk, along with elements of disco and other styles."
Here is CjCoaxs code rewritten for Swift 2.0:
var str = "LCD Soundsystem was the musical project of producer <a href='http://www.last.fm/music/James+Murphy' class='bbcode_artist'>James Murphy</a>, co-founder of <a href='http://www.last.fm/tag/dance-punk' class='bbcode_tag' rel='tag'>dance-punk</a> label <a href='http://www.last.fm/label/DFA' class='bbcode_label'>DFA</a> Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of <a href='http://www.last.fm/tag/alternative%20dance' class='bbcode_tag' rel='tag'>alternative dance</a> and <a href='http://www.last.fm/tag/post%20punk' class='bbcode_tag' rel='tag'>post punk</a>, along with elements of <a href='http://www.last.fm/tag/disco' class='bbcode_tag' rel='tag'>disco</a> and other styles. <br />"
let regex = try! NSRegularExpression(pattern: "<.*?>", options: [.CaseInsensitive])
let range = NSMakeRange(0, input.characters.count)
let htmlLessString :String = regex.stringByReplacingMatchesInString(input, options: [],
range:range ,
withTemplate: "")
print(htmlLessString)
Here is code for Swift 3.0:
do {
let regex = "<[^>]+>"
let expr = try NSRegularExpression(pattern: regex, options: NSRegularExpression.Options.caseInsensitive)
let replacement = expr.stringByReplacingMatches(in: originalString, options: [], range: NSMakeRange(0, comment.characters.count), withTemplate: "")
//replacement is the result
} catch {
// regex was bad!
}
Try SwiftSoup it's easy
do{
let html = "LCD Soundsystem was the musical project of producer James Murphy, co-founder of dance-punk label DFA Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of alternative dance and post punk, along with elements of disco and other styles. <br />"
let doc: Document = try SwiftSoup.parse(html)
return try doc.text()
}catch Exception.Error(let type, let message)
{
print("")
}catch{
print("")
}