1

I have url like this which is a js file example like this link http://www.mysite.com/json.js and 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}

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?

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?

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

1
  • 1
    $.parseJSON()? Commented May 4, 2012 at 6:06

4 Answers 4

3

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
});
Sign up to request clarification or add additional context in comments.

1 Comment

Its not working, it wont even alert anything inside $.get function
1

Make sure your JSON is valid -> http://www.jsonlint.com

To go through every entry in jSON try this:

$.getJSON("url/to/json/file",function(data){
    $.each(data,function(index,element){
        console.log(element.items.url);
    });
});

To get one entry try this (without $.each):

$.getJSON("url/to/json/file",function(data){
    console.log(data.items.url);
});

Make sure it's not a CrossDomain issue! JSON parsing between domains doesn't work in some cases. Make sure it's not one of them. To work with CrossDomain use JSONP. The difference between JSON and JSONP it's that JSONP can support callback and works over crossdomain.

Hope it helps

2 Comments

the JSON validator says the link i used is a valid JSON, however it is a Cross Domain link. What is the right function for using JSONP? i search it on google but the documents are so limited. thanks
there is post here that can help you! You can still use JSON! stackoverflow.com/questions/10030572/…
0

place that in a variable suppose,

myJSON = $.parseJSON(YOUR_JSON);

then,

myJSON.items[0].url

Comments

0

jQuery.parseJSON

var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );

Also you can use jQuery.getJSON/

1 Comment

What's the difference between this and the example on parseJSON documentation? You might just give the link to the documentation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.