javascript - Scraping HTML page for contents of div and passing it to a variable -
i need scrape web page json string rests inside div
"json-data" id
. json string not on same page jquery code itself, first need page (using .get?), find div (using something) , parse json :(
i need parse contents of div
json object , pass variable can use later in code.
i have use jquery v1.8.3, suppose .get()
, .load()
work. i'm not sure how grab json , put in variable.
here's example using jquery 1.8.3, asked. quite simple!
i put example json string in #json-data
element, read element's contents jquery's .html()
, parsed string json.parse()
.
$.get( "ajax/test.html", function( data ) { var myjson = json.parse( $(data).find('#json-data').html() ); console.log('glossary.title:', myjson.glossary.title) });
<!-- html ajax/test.html page --> <div id="json-data"> { "glossary": { "title": "example glossary", "glossdiv": { "title": "s", "glosslist": { "glossentry": { "id": "sgml", "sortas": "sgml", "glossterm": "standard generalized markup language", "acronym": "sgml", "abbrev": "iso 8879:1986", "glossdef": { "para": "a meta-markup language, used create markup languages such docbook.", "glossseealso": ["gml", "xml"] }, "glosssee": "markup" } } } } } </div>
Comments
Post a Comment