Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get key from JSON file using jQuery

I have one JSON file sample

{a:'spc432',
 b:'lll432'
}

I am trying to get JSON file from client side. and html also written at client side only.

After that I want to find out each ID from JSON file dynamically. How can we get Ids with knowing JSON file Ids..?

I tried below coding, but it is not working. Could you please check?

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.5.js"></script>
  <script>
        $(document).ready(function(){
            alert("hi");
            $.get("json_text.txt", function(data) 
                    { alert("length = "+data.length); 
                                          for( i = 0; i< data.length; i++)
                                             alert("key name "+data[i].id);
                                         }
                  );
                    
        });
    </script>
</head>
<body>
</body>
</html>
like image 613
pradeep cs Avatar asked Feb 07 '11 11:02

pradeep cs


1 Answers

To get the key names you can do:

for(var key in data){ alert('key name: ' + key + ' value: ' + data[key]); }
like image 78
Liam Galvin Avatar answered Sep 23 '22 19:09

Liam Galvin