javascript - how to extract data from JSON when JSON object is name is not atomic -
the following simple json object created using xstream. valid javascript object. actualy want ask how access first persons information id ,username, password etc.but when view file in browser displayed webpage not showing "21". expect 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 view file in browser displayed webpage not showing 21. expect page should display 21.
you can copy , paste source code , try it. not displaying 21. how can access these values.
your problem use of "."
in identifier "beans.person"
meaning must quote it, so:
obj.records[0]["beans.person"][0].id
and avoid using document.write
unless have reason , know doing.
<div id="result"></div> 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.getelementbyid("result").textcontent = obj.records[0]["beans.person"][0].id;
on jsfiddle
Comments
Post a Comment