如何在 javascript 中阅读此回复中的custom_fields
个对象?
{
"status": "ok",
"post": {
"id": 25,
"type": "post",
"slug": "price-list",
"url": "http://example.com/2016/02/10/post-title/",
"status": "publish",
"title": "title",
"title_plain": "title",
"content": "",
"excerpt": "",
"date": "2016-02-10 12:23:40",
"modified": "2016-06-01 06:27:39",
"categories": [],
"tags": [],
"author": {
"id": 1,
"slug": "slug",
"name": "name",
"first_name": "first name",
"last_name": "last name",
"nickname": "nickname",
"url": "",
"description": "descp"
},
"comments": [],
"attachments": [],
"comment_count": 0,
"comment_status": "open",
"custom_fields": {
"Regular 95": [
"34200:1"
],
"Super 98": [
"34900:0"
],
"Gasoil": [
"26100:-1"
],
"Diesel": [
"26200:1"
],
"Kerosene": [
"20100:-1"
],
"Fuel / Ton": [
"773.30:0"
]
}
},
"previous_url": "http://example.com/2016/02/10/post-title/",
"next_url": "http://example.com/2016/02/10/post-title/"
}
答案 0 :(得分:0)
已使用$.each
$.each(response.post.custom_fields, function( index, value ) {
console.log( index + ": " + value );
});
$(document).ready(function(){
var response={
"status": "ok",
"post": {
"id": 25,
"type": "post",
"slug": "price-list",
"url": "http://example.com/2016/02/10/post-title/",
"status": "publish",
"title": "title",
"title_plain": "title",
"content": "",
"excerpt": "",
"date": "2016-02-10 12:23:40",
"modified": "2016-06-01 06:27:39",
"categories": [],
"tags": [],
"author": {
"id": 1,
"slug": "slug",
"name": "name",
"first_name": "first name",
"last_name": "last name",
"nickname": "nickname",
"url": "",
"description": "descp"
},
"comments": [],
"attachments": [],
"comment_count": 0,
"comment_status": "open",
"custom_fields": {
"Regular 95": [
"34200:1"
],
"Super 98": [
"34900:0"
],
"Gasoil": [
"26100:-1"
],
"Diesel": [
"26200:1"
],
"Kerosene": [
"20100:-1"
],
"Fuel / Ton": [
"773.30:0"
]
}
},
"previous_url": "http://example.com/2016/02/10/post-title/",
"next_url": "http://example.com/2016/02/10/post-title/"
};
$.each(response.post.custom_fields, function( index, value ) {
console.log( index + ": " + value );
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 1 :(得分:0)
从w3c doc,不同的方法来访问JavaScript属性:
objectName.property // person.age
objectName["property"] // person["age"]
objectName[expression] // x = "age"; person[x]
表达式必须评估为属性名称。
您可以使用嵌套对象链接
objectName.internalObject.property