The following is a simple JSON object created using Xstream. Is it a valid JavaScript object. Actualy I want to ask how to access first persons information like id ,username, password etc.But when I view this file in browser the displayed webpage is not showing "21". I expect that page should display 21.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script> var obj = {"records":[ {"beans.Person":[ {"id":21,"name":"Name21","username":"Username21","password":"password21","age":41,"sex":true}, {"id":22,"name":"Name22","username":"Username22","password":"password22","age":42,"sex":true}, {"id":23,"name":"Name23","username":"Username23","password":"password23","age":43,"sex":true}, {"id":24,"name":"Name24","username":"Username24","password":"password24","age":44,"sex":true}, {"id":25,"name":"Name25","username":"Username25","password":"password25","age":45,"sex":true}, {"id":26,"name":"Name26","username":"Username26","password":"password26","age":46,"sex":true}, {"id":27,"name":"Name27","username":"Username27","password":"password27","age":47,"sex":true}, {"id":28,"name":"Name28","username":"Username28","password":"password28","age":48,"sex":true}, {"id":29,"name":"Name29","username":"Username29","password":"password29","age":49,"sex":true}, {"id":30,"name":"Name30","username":"Username30","password":"password30","age":50,"sex":true} ] } ] } document.write(obj.records[0].beans.Person[0].id); </script> </head> <body> </body> </html> But when I view this file in browser the displayed webpage is not showing 21. I expect that page should display 21.
You can just copy and paste the source code and try it. It is not displaying 21. How can i access these values.
JSON.parse(jsonString)to convert the json string to javascript object, and use index to iterate through the contents!