-1

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", }, ]
4
  • 1
    Why do you tag the question with jQuery when you aren't using it? Commented Jan 10, 2014 at 8:15
  • what you want exactly Commented Jan 10, 2014 at 8:15
  • possible duplicate of Parse local JSON file with jQuery and Javascript Commented Jan 10, 2014 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. Commented Jan 10, 2014 at 8:17

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.