473,388 Members | 1,499 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,388 software developers and data experts.

How to get Json as key and value in the Ajax $.getJSON()?

I have this ajax code for getting json from Jobs.json file.
Expand|Select|Wrap|Line Numbers
  1.  $(document).ready(function(){
  2.         $('#btn2').click( callJobs );
  3.         });
  4.  
  5. function callJobs()
  6. {
  7.  
  8.  
  9.      alert("getting results...");
  10.     $.getJSON('Jobs.json', function(JSON){
  11.         $('#result').empty();
  12.  
  13.         $.each(JSON.jobs, function(i, JOB){
  14.             $('#result')
  15.             .append(JOB.Job +'<br />')
  16.             .append(JOB.Priority+'<br />')
  17.             .append(JOB.DueDate+'<br />')
  18.             .append(JOB.Iscompleted+'<hr />');
  19.       });
  20.     });
  21. }
Jobs.json code is below.
Expand|Select|Wrap|Line Numbers
  1. {
  2. "jobs":[
  3.   {
  4.      "Job":"Job1",
  5.      "Priority":"Low",
  6.      "DueDate":"11.03.2013",
  7.      "Iscompleted":"No"
  8.   },
  9.   {
  10.      "Job":"Job2",
  11.      "Priority":"High",
  12.      "DueDate":"11.03.2013",
  13.      "Iscompleted" : "No"
  14.   },
  15.   {
  16.      "Job":"Job3",
  17.      "Priority":"Medium",
  18.      "DueDate":"11.03.2013",
  19.      "Iscompleted":"No"
  20.   }
  21.   ]
  22.   }
Now I want to rewrite $.each function dynamically.That is, it will write the json string as key and value instead of .append() .
Mar 11 '13 #1
3 2339
acoder
16,027 Expert Mod 8TB
See the first example in the documentation to give you an idea:
http://api.jquery.com/jQuery.getJSON/
Mar 13 '13 #2
I tried like this.Which part do you think wrong?
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.         <title>To-Do</title>
  4.         <meta name="description" content="To-Do" charset="utf-8">
  5.         </meta>
  6.  
  7.         <script language="javascript" type="text/javascript">
  8. function mr(type,URL) {
  9.  
  10.                 var hr = new XMLHttpRequest();
  11.  
  12.                 //SET UP VARS CORRECTLY FOR GET/POST
  13.                 var content = document.getElementById("content").value;
  14.                 var vars = "content=" +content;
  15.  
  16.                 if (type == 'GET')
  17.                     URL = URL + '?' + vars;
  18.  
  19.                 //SET EVENTHANDLERS (THERE ARE ALOT MORE)
  20.                 hr.onreadystatechange = function() {
  21.                     if (hr.readyState == 4 && hr.status == 200) {
  22.                         var return_data = hr.responseText;
  23.                         document.getElementById("result").innerHTML = return_data;
  24.                     }
  25.  
  26.                 }
  27.                 //OPEN THE REQUEST
  28.                 hr.open(type, URL, true);
  29.  
  30.                 //SET HEADERS
  31.                 hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  32.  
  33.                 switch(type) {
  34.                     case 'GET':
  35.                         hr.send();
  36.                         break;
  37.                     case 'POST':
  38.                         hr.send(vars);
  39.                         break;
  40.                 }
  41.  
  42.                 document.getElementById("result").innerHTML = "Processing...";
  43.  
  44.             }
  45. </script>
  46.         <style type="text/css">
  47.             button {
  48.                 cursor: pointer
  49.             }
  50.             div {
  51.                 color: #666;
  52.                 font: normal 13px "Trebuchet MS";
  53.                 width: 350px;
  54.                 padding: 10px
  55.             }
  56.         </style>
  57.     </head>
  58.  
  59.     <body>
  60.         <div>
  61.             Add Item:
  62.             <input type="text" name="name" id="content">
  63.             <br>
  64.             <button onclick="javascript:mr('POST','To-Do.php');" type="button" id="btn1">
  65.                 Submit
  66.             </button>
  67.             <br>
  68.             <button onclick="javascript:mr('GET','get.php');" type="button" id="btn2" >
  69.                 List Jobs
  70.             </button>
  71.             <div id="result"></div>
  72.         </div>
  73.     </body>
  74. </html>
  75.  
  76.  
Mar 13 '13 #3
acoder
16,027 Expert Mod 8TB
This seems to be for your other question?
Mar 13 '13 #4

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

Similar topics

54
by: VK | last post by:
Mission statement: A mechanics to get text stream into browser from any Web location without reloading the current page. 1) This mechanics has to support *at the very least* IE 5.5 and higher...
2
by: giloosh | last post by:
whats the best way to pass a json string to the server. if my jsonstring = {a:'1',b:'sds',c:'sdg'} could i send that to the server passing it as 1 variable like so: url =...
9
Claus Mygind
by: Claus Mygind | last post by:
I am having trouble escaping the & in a JSON.stringfy() ajax call. I don't even know if I am stating the problem correctly here. In my app I have linked json2.js from http://www.JSON.org/json2.js...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.