[SOLVED]reading json content in JS which is defined in link

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

[SOLVED]reading json content in JS which is defined in link

Post by novice4eva »

Hi friends,
I could really use your help on this. I have a json file with data like:

Code: Select all

//header set to "application/json"
{items : [{'10100':['huc6':10100, 'name':'St. John', 'historic_d':28, 'abslolute_loss':0, 'percent_loss':0.0000, 'fillColor':'#1ae8ff']},{......
And I want to read this content from javascript. I could have put things in js as var myJson = [the json content] but I would want to learn things via "link" way. Please do share your wisdom a bit :D


Regards,
Dee
Last edited by novice4eva on Thu Oct 20, 2011 2:38 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: reading json content in JS which is defined in link tag

Post by pickle »

What do you mean the "link" way? Do you mean to read this data from a file you access via AJAX?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: reading json content in JS which is defined in link tag

Post by novice4eva »

Hi pickle,
Thanks for taking interest into this and I am really sorry for getting back to this - LATE(5 days to be exact!). I meant something like this:

Code: Select all

<html>
<head>
<link href="myJsonData.json" type="application/json" rel="exhibit/data" />
...
Libraries like simile exhibit read json data using this method. But I am not able to dissect how?
and myJsonData.json would have content as posted in my first thread.

Regards,
Dee
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: reading json content in JS which is defined in link tag

Post by pickle »

So you just want to be able to access the information in myJsonData.json & work with it in Javascript? I honestly don't know why you would put it in a <link> tag. That wouldn't bring the JSON data into your Javascript environment. You'd have to access the tag with Javascript & make another call to the URL specified by the "href" attribute, resulting in the data being downloaded twice.

Putting it in a <script> tag would make way more sense.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

[SOLVED]Re: reading json content in JS which is defined in l

Post by novice4eva »

Hummm now that's interesting..shoot you are right they are making an ajax call because I always saw a get request being made in the firebug console for the json file BUT damn, it never crossed my mind. Pickle thanks a lot - I think I get it how they(simile exhibit) are doing it now. I thought they were somehow loading the content via the link - and felt like wow that's neat. Thank you so much again. I wish I had a smiley to show you my appreciation for "bringing me into light" :D
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: reading json content in JS which is defined in link tag

Post by pickle »

Heh - glad to help.

If you are putting [SOLVED] in the subject title, edit your original post - changing you most recent post doesn't update the title shown in the forum.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply