0

I have a JSON request result for a blogger post.

// API callback
posts(
{
    "version": "1.0",
    "encoding": "UTF-8",
    "entry":
    {
        "title":
        {
            "type": "text",
            "$t": "Vimeo Embed Video Post"
        },
        "content":
        {
            "type": "html",
            "$t": "<span data-format=\"video-post\"><\/span><iframe allowfullscreen=\"\" frameborder=\"0\" height=\"281\" mozallowfullscreen=\"\" src=\"\/\/player.vimeo.com\/video\/107469289\" webkitallowfullscreen=\"\" width=\"500\"><\/iframe> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod<br \/>tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,<br \/>quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo<br \/>consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse<br \/>cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non<br \/>proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        },
    }
});

Inside the "content" you can see there is a <span data-format="video-post"></span>. I want to get value from data-format atrribute with javascript.

2
  • Do you want to get mentioned value inside posts function? Commented Oct 2, 2014 at 10:48
  • Yes, I want to get value from <span data-format="..."></span>, and yes it is inside the posts function. Commented Oct 2, 2014 at 10:50

2 Answers 2

2

Update

Try this...

function posts(data) {
   var formatData = $(data.entry.content['$t']).data('format');
}

Update #2

In case if your data object, or any of it's properties/subproperties might be not defined, here is safer version:

function posts(data) {
   var formatData = data 
      && data.entry 
      && data.entry.content 
      && data.entry.content['$t'] 
      && $(data.entry.content['$t']).data('format') || '';
   if (formatData!='') { //If has value
      // ... have something to do about it
   } 
}

JSFiddle

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

6 Comments

Yes, now it looks good for me, +1. And just to mention: content['$t'] is correct and appropriate way to get value by key $t, but content.$t seems to work aswell.
Well, can we add a if function? since the <span data-format="..."></span> not automatically generated by the blogger so it will not available for every JSON request. I mean the script will only get the value when data-format available in JSON, if not, the result should be return blank or null.
What exactly does not work? Please check JSFiddle example: jsfiddle.net/jevgenig/qL3o8d9r
variable "formatData" is defined inside function "posts" and you are trying to use it outside. Just move document.getElementById("demo").innerHTML = formatData; into the body of function, as last row, like that: jsfiddle.net/jevgenig/78thgfu9/1
It's because in that example, function posts was executed two times, with valid and invalid formatData values. Invalid was the last one. I've removed it here: jsfiddle.net/jevgenig/78thgfu9/2 and it works now
|
0

Try this...

$(variable.entry.content['$t']).data('format');

where variable is a parameter of your post method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.