问题描述
我有这样的URL,这是一个js文件示例,例如此链接 http://www.mysite. com/json.js 并且该链接将放入json格式的值,如下所示:
I have url like this which is a js file example like this link http://www.mysite.com/json.jsand that link will out put a json formatted values like below:
{
"note":null,
"attributes":null,
"total_price":9980,
"total_weight":0,
"item_count":4,
"items":[{
"id":214597138,
"title":"Acai +",
"price":2495,
"line_price":9980,
"quantity":4,
"sku":"",
"grams":0,
"vendor":"Delta Labs",
"variant_id":214597138,
"url":"/products/acai",
"image":"http://cdn.shopify.com/s/files/1/0156/4994/products/acai_bottle.png?385",
"handle":"acai",
"requires_shipping":true
}],
"requires_shipping":true}
现在我想抓住那个链接,然后在我的jquery上,我将如何在json输出中使用该链接以获取诸如标题,URL,价格,sku等之类的值?
now i wanted to grab that link, then on my jquery how am i going to use that link with json output in order to get the values like the title, url, price, sku, and etc?
我在Google上搜索了是否有一些可以处理此问题的函数,然后我找到了jQuery.getJSON,但是我不太了解,或者它是否适用于我要完成的工作?
i searched on google if there are some functions that can handle this and i found jQuery.getJSON, but i don't really understand that much or is it applicable to my to what i am trying to accomplish?
顺便说一句, http://www.mysite.com/json.js ,它的json值是动态的,因此它是我想要抓取的链接,并输出json内容
By the way, http://www.mysite.com/json.js, its json value is dynamic, therefore it is the link i wanted to grab, and output the json content
推荐答案
例如,在ajax请求中,这就是我的方法.
Here is how I would do it, in an ajax request for example.
$.get('http://www.mysite.com/json.js', function(result) {
var json = $.parseJSON(result);
alert(json.title); //Do whatever you want here
});
这篇关于使用Jquery从JSON格式提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!