javascript 当 JSON 对象是名称时如何从 JSON 中提取数据不是原子的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16117662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to extract data from JSON when JSON object is name is not atomic
提问by stackOverflow
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.
以下是使用 Xstream 创建的简单 JSON 对象。它是一个有效的 JavaScript 对象。实际上,我想问一下如何访问第一人称信息,例如 id、用户名、密码等。但是当我在浏览器中查看此文件时,显示的网页未显示“21”。我希望该页面应显示 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.
但是当我在浏览器中查看这个文件时,显示的网页没有显示21。我希望该页面应显示 21。
You can just copy and paste the source code and try it. It is not displaying 21. How can i access these values.
您可以复制并粘贴源代码并尝试一下。它不显示21。我如何访问这些值。
回答by Xotic750
Your problem is the use of the "."
in the identifier "beans.Person"
meaning that you must quote it, like so:
您的问题是"."
在标识符中使用了"beans.Person"
意味着您必须引用它,如下所示:
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.
document.write
除非您有充分的理由并知道自己在做什么,否则请避免使用。
<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
回答by NINCOMPOOP
Use JSON.parse()
to get a javascript object from JSON string. You can get more information here.
用于JSON.parse()
从 JSON 字符串获取 javascript 对象。您可以在此处获取更多信息。
EDIT:
编辑:
var objectID = obj.records[0]['beans.Person'][0]['id'];
A nice read, Use Square Bracket Notation.
读得很好,使用方括号表示法。
回答by Muneeb Nasir
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
检查控制台,我很确定你有错误。它把豆子和人两个不同的对象
回答by Jabulani Vilakazi
This is a sample of your object in Back End Code C#
这是后端代码 C# 中的对象示例
public Class Learner
{
public string Name {get; set;}
public string Surname {get; set;}
}
Then in JavaScript when reading the object from JsonResult
然后在 JavaScript 中从 JsonResult 读取对象时
var leanerName = obj[0].Name;
var leanerSurname = obj[0].Surname;
Hope this helps
希望这可以帮助