1

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.

1
  • Use JSON.parse(jsonString) to convert the json string to javascript object, and use index to iterate through the contents! Commented Apr 20, 2013 at 7:02

4 Answers 4

5

Your problem is the use of the "." in the identifier "beans.Person" meaning that you must quote it, like so:

obj.records[0]["beans.Person"][0].id 

And avoid using document.write unless you have a good reason for it and know what your 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

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

Comments

2

Use JSON.parse() to get a javascript object from JSON string. You can get more information here.

EDIT:

var objectID = obj.records[0]['beans.Person'][0]['id']; 

A nice read, Use Square Bracket Notation.

1 Comment

I am already using that already that above example is a sample I am interested in how to these data. Put simply I am asking for syntax for accessing id,username,password.
1

try this, i'm sure it will work

document.write(obj.records[0]['beans.Person'][0]['id']); 

check console, i'm petty sure you were having errors. It is taking beans and person two different objects

Comments

0

This is a sample of your object in Back End Code C#

public Class Learner { public string Name {get; set;} public string Surname {get; set;} } 

Then in JavaScript when reading the object from JsonResult

 var leanerName = obj[0].Name; var leanerSurname = obj[0].Surname; 

Hope this helps

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.