我在访问值时遇到困难,因为其键的名称中包含正斜杠。
这里是Import.io返回的JSON的摘录:
rowwise
这是我用来访问数据的jQuery代码:
cat
由于斜杠,以下一点给我带来了问题:
"results":[
{
"superawesome_link":"https://medium.com/@sprawsmdoo",
"graf_image":"https://cdn-images-1.medium.com/fit/t/800/240/1*YIWxXylsoUQIDfQK353YuA.png",
"avatar_link":"https://medium.com/@sprawsmdoo",
"darken_link/_text":"Jan 28",
"readmore_number":10.0,
"readmore_number/_source":"10",
"readingtime_value_numbers/_source":"7",
"readmore_link":"https://medium.com/@sprawsmdoo/pricing-design-services-bca1c6955b47?source=latest---------1",
"readingtime_value_numbers":7.0,
"readmore_link/_text":"Read more…",
"darken_link":"https://medium.com/@sprawsmdoo/pricing-design-services-bca1c6955b47?source=latest---------1",
"postarticle_link/_text":"Pricing design servicesWhat we’re doing, how we’re doing it, and what we’re struggling with when it…",
"readingtime_value":"7 min read",
"postarticle_link":"https://medium.com/@sprawsmdoo/pricing-design-services-bca1c6955b47?source=latest---------1",
"superawesome_link/_text":"Superawesome"
},
...
错误消息是"未捕获的ReferenceError:文本未定义"。
如何引用var mediumJSON = "URLHERE";
$.ajax({
url: mediumJSON,
crossDomain: true,
dataType: 'json',
success: function(response) {
$.each(response.results, function(index, value) {
var newPost = $("#postTemplate").clone();
newPost.appendTo('.posts');
newPost.removeAttr('id');
newPost.find('.post-title a').attr('href', value.readmore_link).text(value.postarticle_link/text);
newPost.find('.post-length').text(value.readingtime_value);
newPost.show();
return index < 4;
});
},
error: function(jqXHR, textStatus, errorThrown) {
$('.posts').html('<article class="post"><div class="quiet">There was an error fetching posts, you can read our blog <a href="https://medium.com/@sprawsmdoo">directly on Medium</a>.</div></article>');
}
});
以获取其价值?
答案 0 :(得分:1)
您需要使用Bracket notation,它允许访问包含特殊字符的属性。
var data = {
"readmore_number":10.0,
"readmore_number/_source":"10",
"superawesome_link/_text":"Superawesome"
}
alert(data['superawesome_link/_text'])
&#13;
答案 1 :(得分:1)
好像你使用了import.io的“Magic”。您可以简单地使用他们的App并构建一个新的Extractor。这样你就可以指出&amp;单击所需的数据,更改数据类型(链接或文本),并为结果数据列指定所需的名称(不带斜杠)。