473,401 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

how can i access my .json file using separate javascript to html

hi, gud day... i'm trying to write a program in javascript that uses .json. i already wrote one that is when i run the program, it will create a tab dynamically, using jquery, from my json data. it's already a running program but my json data is in my javascript code. what im really confused about is how to do it using a separate .json file, .js and .html.

here is my running javascript code with my json in it

Expand|Select|Wrap|Line Numbers
  1. function CreateTab(o) {
  2.   var str = '<ul>';
  3.   for (var i = 0; i < o.length; i++) {        
  4.     str += '<li><a href="#tab' + i + '">' + o[i].title +   '</a></li>';        
  5.     }
  6.  
  7.     str += '</ul>';
  8.     for (var i = 0; i < o.length; i++) {
  9.     str += '<div id="tab' + i + '">' + o[i].desc + '</div>';
  10.     }
  11.   return str;
  12. }
  13.  
  14. $(document).ready(function() {
  15.   var json1 = [    
  16.     {"title": "tab 1", "desc":"This is tab 1"},
  17.     {"title": "tab 2", "desc":"This is tab 2"}
  18.   ];
  19.  
  20. $('#tabs').append(CreateTab(json1, "#tabs", true));    
  21.         });
Feb 15 '11 #1
5 4182
acoder
16,027 Expert Mod 8TB
Since JSON is a subset of JavaScript, you could include it as a JavaScript file:
Expand|Select|Wrap|Line Numbers
  1. <script  type="text/javascript" src="path/to/data.json"></script>
Feb 15 '11 #2
i've tried that one but our instructor wanted us to use jQuery.getjson() only to get our .json file. he said that is what we are going to use if we are to connect soon to the server. thank you so much for the reply
Feb 15 '11 #3
acoder
16,027 Expert Mod 8TB
Oh right. In that case, see the documentation (and the listed examples on that page).
Feb 15 '11 #4
i already tried using that one and yes, it can display the

* Singular sensation
* Beady little eyes
* Little birds pitch by my doorstep

but the problem is, i don't how to do it if i will use

{"title": "tab 1", "desc":"This is tab 1"},
{"title": "tab 2", "desc":"This is tab 2"},
{"title": "tab 3", "desc":"This is tab 3"},

and much more in arrays of objects or object arrays....

i am new in using json file with javascript... so i really need help. thanks a lot.
Feb 16 '11 #5
acoder
16,027 Expert Mod 8TB
Once you have the data, it'd be the same as you're using it now, e.g.
Expand|Select|Wrap|Line Numbers
  1. for (var i = 0; i < o.length; i++) {
  2.      str += '<li><a href="#tab' + i + '">' + o[i].title +   '</a></li>';
  3. }
Feb 16 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Stephane | last post by:
Hi, Is there any way we can access a file using the physical path on a remote server? I need to read and delete content of a remote directory on the LAN. All I got so far is access denied so I...
3
by: Minh | last post by:
I had a MS Access Database file with a password for username: Admin (I set it using MS Access manually) Now I want to use C# code change password of this mdb file. Can you please show me how...
17
by: shineofleo | last post by:
Here is the situation: I wrote a VB programm, which stores all the information in a single Access database file using jet engine. It worked well, however one of my customs reported that there was...
3
by: SM | last post by:
Hello, Im trying to access elements in my XML file using the JavaScript DOM but i'm not sure how. I use AJAX to access the XML and then use the responseXML property to access the XML file data. I...
4
by: manishpo | last post by:
Hi Do any one know how to convert the structred wordocument contents to Access database file using C++ script. Do I have to use word document model for in MFC. if yes then how. Thanks ...
3
by: MLD | last post by:
PLEASE HELP! I would like to include a UL as a menu, styled by an included CSS Style Sheet. The problem I am having is how do I dynamically set the "active" page class using JavaScript to...
4
by: tokcy | last post by:
Hi everyone, I have some .rtf extension file and i want to read that file using php on html page but i am not able to do this. If it is .txt file then there is no problem but it is RTF and DOC...
5
by: LELE7 | last post by:
I'm new to PHP, but I usually program Windows Apps in .NET(c#, VB). So please excuse my unfamiliarity with code and HTML combined. It seems pretty simple to upload a file to a website using the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.