从json结果中提取data-tag

时间:2017-03-09 15:02:18

标签: javascript json

这是我的API请求的结果:

// API callback
showAuth({
    "version": "1.0",
    "encoding": "UTF-8",
    "entry": {
        "content": {
            "type": "html",
            "$t": "\u003Cdiv id=\"data\" class=\"democlass\" data-author=\"Mr. Jhon Doe\" data-cat=\"Technology\" data-url=\"http:\/\/www.qbtemplates.com\/\"\u003E\u003C\/div\u003E \u003Cimg alt=\"Treasury : Minimal Responsive News \u0026 Magazine Blogger Template\" src=\"https:\/\/4.bp.blogspot.com\/-Kv_-Jg5krkA\/V8PFfwe1ikI\/AAAAAAAAAU0\/4J4Dlc0lOJIW2Qbe45TDTMVo4_Ij8MQhACLcB\/s1600\/Treasury---http___treasury-soratemplates.blogspot.co.jpg\" title=\"Treasury : Minimal Responsive News \u0026 Magazine Blogger Template\" \/\u003E\u003Cbr \/\u003E \u003C!-- \u003Cul class=\"item-cta\"\u003E    \u003Cli class=\"demo\"\u003E        \u003Ca data-demo=\"true\" href=\"http:\/\/treasury-soratemplates.blogspot.com\/\" rel=\"nofollow\" target=\"_blank\"\u003EPreview\u003C\/a\u003E    \u003C\/li\u003E    \u003Cli class=\"download\"\u003E        \u003Ca data-download=\"true\" href=\"http:\/\/qbtemplates.16mb.com\/files\/Treasury%20Free%20Version.zip\" rel=\"nofollow\" target=\"_blank\"\u003EDownload\u003C\/a\u003E    \u003C\/li\u003E\u003C\/ul\u003E--\u003E \u003Cbr \/\u003E\u003Cdiv style=\"text-align: justify;\"\u003ETreasury is all purpose Blogging blogger theme which can be used for all your blogs like travel, food, life, programming, everyday, fashion and everything you want to blog about. On top of this, great and friendly support makes your website setup experience completely smooth! Treasury Magazine design is excellent for a news, newspaper, magazine, article and editorial publishing or review and rating site. It uses the best clean SEO practices, and on top of that, it’s fast, simple, and easy to use. \u003C\/div\u003E"
        },
        "author": [{
            "name": {
                "$t": "Mikey"
            },
            "uri": {
                "$t": "http:\/\/www.blogger.com\/profile\/05775556662956xxxxxx"
            },
            "email": {
                "$t": "noreply@blogger.com"
            },
            "gd$image": {
                "rel": "http://schemas.google.com/g/2005#thumbnail",
                "width": "16",
                "height": "16",
                "src": "http:\/\/img1.blogblog.com\/img\/b16-rounded.gif"
            }
        }],
    }
});

我只是想把一些数据提取到变量

function showAuth(user) {
    var b = user.entry.author[0];
    c = b.name.$t;
    d = b.gd$image.src.replace(/\/s[0-9]+(-*c*)\//, '/s60$1/');
    e = user.entry.content.$t[0];
    f = e.data - author;
    document.write('<img class="auvtar" alt="' + c + '" src="' + d + '" title="' + c + '"/>' + f + '')
}

变量cd会产生正确的数据,但变量f不会产生任何数据(我的意思是如果我启用变量,整个代码都不起作用{ {1}})。

我想从f中将data-catdata-authordata-url提取到分开的变量中,例如entry.contentfg

2 个答案:

答案 0 :(得分:1)

首先,删除

中的[0]
  • e = user.entry.content。$ t [0];

然后将其转换为html元素;

  • g = $(e)[0]; //索引0是div,如果你可以替换它 想要找到一个特定的元素

最后你可以将它用作jQuery对象并提取数据值:

  • h = $(g).data('author')

  • i = $(g).data('cat')

  • j = $(g).data('url')

干杯!

答案 1 :(得分:0)

e.dataundefined

您将e设为user.entry.content.$t[0]

这意味着e$t中的第一个字符,它似乎是以"\u003Cdiv id=\"data\" class=\"d开头的长字符串

相关问题