简体   繁体   中英

Can I generate a JSON file with javascript?

I want to make a page on domain "example1.com" and get/parse a JSON file on another domain "example2.com/json.json". Can the json file be generated with javascript (on example2.com)? I think this can be done with php, but I want to do it with javascript. If it is not possible to generate a json file with javascript, is it possible to get/parse an object from a javascript file? EX: "example1.com" to "example2.com/js.js"

EDIT: Ok, so it is not possible to get/parse an object from a javascript file, because it is client side. So my only option is to generate a JSON file. Is it possible to do that with Javascript? I know it's probably not the best way, but I want to do it in JS, not PHP.

json is just a javascript object in a string format so javascript can make json data:

   var animal = {name: "cat", sound: "meow"};
   json = JSON.stringify(animal); //json is a JSON string

javascript does not allow ajax calls across to other sites (cross-site scripting) though becuase it is a security risk. you could look into jsonP which is a work-around this rule.

Also you cannot call to another website or your own server to run javascript to return something becuase javascript only runs in the clients browser (unless you are using a javascript server like node.js). A server has to be listening somewhere for a request.

To parse a JSON object with Javascript, I would use jQuery.

http://api.jquery.com/jQuery.parseJSON/

Like anthonybell said most browsers do not allow cross-site scripting so you need to look into jsonP or work on the same domain.

Also to generate JSON you can create an object using javascript then either loop though it and output it or just output your data in JSON format which you can read about here:
http://www.w3schools.com/json/

You can use JSONP - essentially wrap your JSON in a callback function on domain2.com like so:

jsonCallback(
    {
        "sites":
        [
            {
                "siteName": "JQUERY4U",
                "domainName": "http://www.jquery4u.com",
                "description": "#1 jQuery Blog for your Daily News, Plugins, Tuts/Tips & Code Snippets."
            },
            {
                "siteName": "BLOGOOLA",
                "domainName": "http://www.blogoola.com",
                "description": "Expose your blog to millions and increase your audience."
            },
            {
                "siteName": "PHPSCRIPTS4U",
                "domainName": "http://www.phpscripts4u.com",
                "description": "The Blog of Enthusiastic PHP Scripters"
            }
        ]
    }
);

Then you can call it from domain1.com like so:

(function($) {
var url = 'http://www.jquery4u.com/scripts/jquery4u-sites.json?callback=?';

$.ajax({
   type: 'GET',
    url: url,
    async: false,
    jsonpCallback: 'jsonCallback',
    contentType: "application/json",
    dataType: 'jsonp',
    success: function(json) {
       console.dir(json.sites);
    },
    error: function(e) {
       console.log(e.message);
    }
});

})(jQuery);

You cannot replace PHP with JavaScript (unless you've got NodeJS or something).

Generating your JSON using PHP will return JSON to the client.

Having Javascript generate your JSON will return Javascript to the client. If you're doing that theres no point even usng JSON.

If you want to have a JSON Document generated (from data sources on your server) instead of a static JSON file you will need some sort of server-side programming.

If you want to use JavaScript you will need to install and configure NodeJS (and possibly some other services like a web-server, or create a server with NodeJS) and write scripts that generate and serve your JSON.

Creating the scripts to generate the JSON (and creating the basic server to host it) is not all that difficult. Installing and configuring Node can be more difficult.

If you already have access to PHP (and webserver) on your server, use that.

If your output file can be static, (and your running windows) you could possibly use JScript to generate your static file. This would require you to physically run the script (or possibly create a scheduled task to run it for you). So you'd be replacing your static file with newly generated JSON every so often. Probably not what you want though!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM