Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I am testing whether i could read a item in a .json file into javascript JSON object and display the contents. I need to store the BIDs in the variable R1 array and display it

Code is as follows

<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<script type="application/javascript">
function loadJSON()
{
   var data_file = "data1.json";
   var http_request = new XMLHttpRequest();
   try{
      // Opera 8.0+, Firefox, Chrome, Safari
      http_request = new XMLHttpRequest();
   }catch (e){
      // Internet Explorer Browsers
      try{
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      }catch (e) {
         try{
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         }catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
         }
      }
   }
   http_request.onreadystatechange  = function(){
      if (http_request.readyState == 4  )
      {
        // Javascript function JSON.parse to parse JSON data
        var jsonObj = JSON.parse(http_request.responseText);


        var R1 = new Array();    

for(var i= 0 ; i< jsonObj.length; i++){
   R1.push(jsonObj[i].BID);
   document.write(R1);
}



      }
   }
   http_request.open("GET", data_file, true);
   http_request.send();
}




</script>

</body>
</html>

AND my data1.json is as follows

[ { "BID" : "4569749", }, { "BID" : "466759", }, { "BID" : "4561149", }, ]
share|improve this question
1  
Why do you tag the question with jQuery when you aren't using it? – Johan Jan 10 '14 at 8:15
    
what you want exactly – Karthick Kumar Jan 10 '14 at 8:15
    
possible duplicate of Parse local JSON file with jQuery and Javascript – Johan Jan 10 '14 at 8:16
    
i could like to check whether the code written is correctly accepting the BIDs and display the contents of array R1 in browser. – user3138452 Jan 10 '14 at 8:17

1 Answer 1

Yes we can load Json objects, As I did in one of my JSP project. here is the code so that you can easily understand.It is calling a servlet which prepare JSON file from the DB.


        $('document').ready(function(){
            $.ajax({
                type: 'POST', 
                url: 'getCities',
                success: function(data) {
                    var response = JSON.parse(data);
                    var products = response['city'];
                    var product_html = '';
                    $(products).each(function(index, value){
                        product_html += ""+value['name']+"";
                    });
                    product_html += "";
                    $("#citylist").html(product_html);
                }
            }); 
        }); 

here 'getCities' is a servlet which prepare JSON file from Data fetched from Database. It is actually populating the dropdownlist related to particular counties.

One more thing I believe the json file is incorrect. Please check the format with some json validator.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.