mahmoudismail mahmoudismail - 5 months ago 9
JSON Question

How i can read custom field from object (wordpress)

How i can read

custom_fields
object from this response in javascript ?

{
"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/"
}

Answer

Used $.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>