可能重复:
循环通过Json对象
我有一个PHP函数,data.php
它从外部服务器URL获取JSON数据,如:
<?php
$url = "https://dev.externalserver.net/directory";
$content = file_get_contents($url);
echo json_encode($content);
?>
Run Code Online (Sandbox Code Playgroud)
检索到的JSON数组如下所示:
[
{ "name": "Not configured",
"mac_address": "1111c11c1111",
"online": "false",
"rate": "Not configured" },
{ "name": "Not configured",
"mac_address": "0000c00c0000",
"online": "false",
"rate": "Not configured" }
]
Run Code Online (Sandbox Code Playgroud)
我现在正在尝试编写一个对该PHP函数的AJAX调用,遍历JSON数组,并以非JSON格式在浏览器中显示它.我的AJAX代码如下所示:
$.ajax({ url: 'data.php',
type: 'POST',
dataType: 'json',
success: function(output) {
$.each(output, function() {
$.each(this, function(key, value){
alert(key + " --> " + value);
});
});
}
});
Run Code Online (Sandbox Code Playgroud)
我的问题是,代码当前显示的警告框,显示像这样的阵列内的单个字符:0 --> [
,0 -->
,0 --> …
我有JSON看起来像这样
{
"keyword1": {
"identifier1": 16
},
"keyword2": {
"identifier2": 16
}
}
Run Code Online (Sandbox Code Playgroud)
我需要遍历关键字以获取标识符(不确定我是否在这里使用正确的术语).看起来很简单,但由于关键字都被命名为不同,我不知道如何处理.
可能重复:
访问对象的第一个属性
我有一个像这样的javascript对象:
var list = {
item1: "a",
item2: "b",
item3: "c",
item4: "d"
};
Run Code Online (Sandbox Code Playgroud)
在JS中使用反射,我可以说list ["item1"]以编程方式获取或设置每个成员,但我不想依赖成员的名称(对象可以扩展).所以我想得到这个对象的第一个成员.
如果我编写以下代码,则返回undefined.谁知道如何做到这一点?
var first = list[0]; // this returns undefined
Run Code Online (Sandbox Code Playgroud) 下面是一个简单的samlple,我想使用for/in循环显示对象属性
var Object = { x:1, y:2, z:3 };
for (property in Object) {
console.log(Object.property);
};
Run Code Online (Sandbox Code Playgroud)
它显示未定义.
但如果使用console.log(Object[property]);
它有效,并显示1 2 3
为什么我不能用于Object.property
显示in/in循环?
我正在使用node.js来接收一个帖子请求,请求体在打印后使用以下内容获得此内容console.log()
:
{
'object 1':
{
deviceType: 'iPad Retina',
guid: 'DF1121F9-FE66-4772-BE74-42936F1357FF',
is_deleted: '0',
last_modified: '1970-12-19T06:01:17.171',
name: 'test1',
projectDescription: '',
sync_status: '1',
userName: 'testUser'
},
'object 0':
{
deviceType: 'iPad Retina',
guid: '18460A72-2190-4375-9F4F-5324B2FCCE0F',
is_deleted: '0',
last_modified: '1970-12-19T06:01:17.171',
name: 'test2',
projectDescription: '',
sync_status: '1',
userName: 'testUser'
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用以下node.js代码获取请求:
var restify = require('restify'),
mongoose = require('mongoose');
var connect = require('connect');
var bodyParser = require('body-parser');
/*server declaration
...
...
*/
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: true }));
server.post('/project', function (req, res, next) {
console.log(req.body);//the …
Run Code Online (Sandbox Code Playgroud) 我想在JavaScript中解析JSON字符串.响应是这样的
var response = '{"1":10,"2":10}';
Run Code Online (Sandbox Code Playgroud)
如何从这个json中获取每个键和值?
我这样做 -
var obj = $.parseJSON(responseData);
console.log(obj.count);
Run Code Online (Sandbox Code Playgroud)
但我得到undefined
了obj.count
.
我有一个类似于以下的数据结构:
var object = {
name: "a",
number: "1",
mission: ['a','b','c']
}
Run Code Online (Sandbox Code Playgroud)
现在我想将它们更新到数据库中,所以我需要遍历它们。
我试过:
object.forEach(function(value, key, map){
console.log('value: ' + value + ', key: ' + key + ', map:' + map);
});
Run Code Online (Sandbox Code Playgroud)
然后控制台报错:object.forEach is not a function
可能重复:
循环通过Json对象
{
"data": [
{
"name": "Jen",
"id": "1"
},
{
"name": "Steve",
"id": "8"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我正在与之交互的服务器响应上述内容.
我正在尝试enter code here
为For..in语句循环遍历它.
这就是我想要做的:
for (var item in response.data) {
console.log(item.name);
}
Run Code Online (Sandbox Code Playgroud)
这不起作用.什么地方出了错?
谢谢
在阅读评论后,我认为可以使用以下内容:
for (var item in response.data) {
console.log(response.data[item].name);
}
Run Code Online (Sandbox Code Playgroud)
我能够得到一份名单......
有人可以解析它的工作原理吗?
我正在使用格式如下的 GeoJSON 数据集:
{
"type": "Feature",
"properties": {
"startcong": "109",
"district": "7",
"statename": "Pennsylvania",
"member": {
"112": {
"21168": {
"party": "Republican",
"name": "Meehan, Pat",
"district": "7"
}
},
"109": {
"15447": {
"party": "Republican",
"name": "Weldon, Curt", "district": "7"}
},
"110": {
"20744": {
"party": "Democrat",
"name": "Sestak, Joe",
"district": "7"
}
},
"111": {
"20744": {
"party": "Democrat",
"name": "Sestak, Joe",
"district": "7"
}
}
},
"endcong":
"112",
"id": "042109112007"
}
}
Run Code Online (Sandbox Code Playgroud)
我正在努力解决如何访问这些嵌套对象的问题。例如,我可以使用feature.properties.member[112][21168]
来访问该party
属性。然而:
如何迭代类属性列表并获取每个属性的值(仅属性而不是函数)
class Person{
name:string;
age:number;
address:Address;
getObjectProperties(){
let json = {};
// I need to get the name, age and address in this JSON and return it
// how to do this dynamically, rather than getting one by one
// like json["name"] = this.name;
return json;
}
}
Run Code Online (Sandbox Code Playgroud)
请帮忙。
javascript ×6
json ×4
arrays ×2
ajax ×1
for-loop ×1
geojson ×1
iteration ×1
jq ×1
jquery ×1
key ×1
node.js ×1
object ×1
php ×1
reflection ×1
typescript ×1