6

I'm studying javascript these days and I have question. I have variable that contain an url. I would like to save the content of the url pointed by my variable in another variable...

The first variable is something like:

var Link = "http://mysite.com/json/public/search?q=variabile&k=&e=1";

If I open the link I see something like:

{

"count": 1,
"e": 1,
"k": null,
"privateresult": 0,
"q": "gabriel",
"start": 0,
"cards": [
    {
        "__guid__": "cdf8ee96538c3811a6a118c72365a9ec",
        "company": false,
        "__title__": "Gabriel Butoeru",
        "__class__": "entity",
        "services": false,
        "__own__": false,
        "vanity_urls": [
            "gabriel-butoeru"
        ]
    }
]
}

How can I save the json content in another javascript variable?

6
  • You can't unless the server you are accessing supports JSONP Commented Jun 20, 2012 at 9:41
  • Use AJAX to request it, then JSON.parse json.org/js.html Commented Jun 20, 2012 at 9:41
  • the var Link is javascript variable! :S Commented Jun 20, 2012 at 9:42
  • @JellyBelly Captain obvious strikes again :D Commented Jun 20, 2012 at 9:43
  • I don't want to open the json file ... the content should be saved in the javascript variable. The code is just an example. Commented Jun 20, 2012 at 9:48

5 Answers 5

19

You would need a simple AJAX request to get the contents into a variable.

var xhReq = new XMLHttpRequest();
xhReq.open("GET", yourUrl, false);
xhReq.send(null);
var jsonObject = JSON.parse(xhReq.responseText);

Please note that AJAX is bound by same-origin-policy, in case that URL is different this will fail.

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

2 Comments

Likely not from the same server.
since this is a very old post, just wanted to check if there is any new better approach for it? Mainly because I saw this warning when i tried the approach " Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check xhr.spec.whatwg.org."
4

You can use like this

  var savings_data = JSON.stringify(your_json_object);

Comments

1

You can save json as object in this way

Reference : Json

var obj = {};

 obj.name = "test";
 obj.no   = "1234";

Comments

1

I think this might help you, using jQuery... :)

$.ajax({
    beforeSend: function() { DO HERE WHATEVER }, //Show spinner

            complete: function() { DO HERE WHATEVER  }, //Hide spinner

            type: 'POST',
            url: Link,
            dataType: 'JSON',
            success: function(data){
                                var data = data;
                               OR IF YOU WANT SEPARATE THE VALUES...
                                var count = data.count;
                                var e = data.e;
                                var k = data.k;
                                ...
             }                               
          });      

Comments

0

This example considers the state of the request and will allow you to access the data from the JSON format using the dot operator.

var request = new XMLHttpRequest();
request.open("GET", "mysite.com/json/public/search?q=variabile&k=&e=1", true); 
request.setRequestHeader("Content-type", "application/json"); 
request.send(); 
request.onreadystatechange = function(){
     if(request.ready == 4 && request.readyState == 200){
          var response = request.responseText; 
          var obj = JSON.parse(response);
          alert(obj.count); // should return the value of count (i.e Count = 1) 
          alert(obj.e); // should return the value of e (i.e. e = 1) 
          var count = obj.count;  //store the result of count into your variable
          var e = obj.e; //store the result of e into your variable
          //...and so on 
     }
} 

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.