简体   繁体   English

我可以使用javascript生成JSON文件吗?

[英]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". 我想在域“example1.com”上创建一个页面,并在另一个域“example2.com/json.json”上获取/解析JSON文件。 Can the json file be generated with javascript (on example2.com)? 可以使用javascript(在example2.com上)生成json文件吗? I think this can be done with php, but I want to do it with javascript. 我认为这可以用PHP完成,但我想用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? 如果无法使用javascript生成json文件,是否可以从javascript文件中获取/解析对象? EX: "example1.com" to "example2.com/js.js" EX:“example1.com”到“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. 编辑:好的,所以不可能从javascript文件中获取/解析对象,因为它是客户端。 So my only option is to generate a JSON file. 所以我唯一的选择是生成一个JSON文件。 Is it possible to do that with Javascript? 用Javascript可以做到这一点吗? I know it's probably not the best way, but I want to do it in JS, not PHP. 我知道这可能不是最好的方式,但我想用JS而不是PHP来做。

json is just a javascript object in a string format so javascript can make json data: json只是一个字符串格式的javascript对象,所以javascript可以生成json数据:

   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. javascript不允许ajax调用到其他站点(跨站点脚本),但这是一个安全风险。 you could look into jsonP which is a work-around this rule. 你可以查看jsonP这是一个解决这条规则的方法。

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). 此外,您不能调用其他网站或您自己的服务器运行javascript返回的东西,因为javascript只在客户端浏览器中运行(除非您使用像node.js这样的javascript服务器)。 A server has to be listening somewhere for a request. 服务器必须在某处侦听请求。

To parse a JSON object with Javascript, I would use jQuery. 要用Javascript解析JSON对象,我会使用jQuery。

http://api.jquery.com/jQuery.parseJSON/ 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. 就像anthonybell所说,大多数浏览器不允许跨站点脚本,所以你需要调查jsonP或在同一个域上工作。

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: 另外,要生成JSON,您可以使用javascript创建一个对象,然后循环通过它并输出它或者只输出JSON格式的数据,您可以在这里阅读:
http://www.w3schools.com/json/ http://www.w3schools.com/json/

You can use JSONP - essentially wrap your JSON in a callback function on domain2.com like so: 您可以使用JSONP - 基本上将您的JSON包装在domain2.com上的回调函数中,如下所示:

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: 然后你可以像这样从domain1.com调用它:

(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). 您无法用JavaScript替换PHP(除非您有NodeJS或其他东西)。

Generating your JSON using PHP will return JSON to the client. 使用PHP生成JSON将返回JSON到客户端。

Having Javascript generate your JSON will return Javascript to the client. 让Javascript生成您的JSON将返回Javascript到客户端。 If you're doing that theres no point even usng JSON. 如果你这样做,即使使用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. 如果要生成JSON文档(从服务器上的数据源)而不是静态JSON文件,则需要某种服务器端编程。

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. 如果您想使用JavaScript,您需要安装和配置NodeJS(可能还有一些其他服务,如Web服务器,或使用NodeJS创建服务器)并编写生成和提供JSON的脚本。

Creating the scripts to generate the JSON (and creating the basic server to host it) is not all that difficult. 创建脚本以生成JSON(并创建基本服务器来托管它)并不是那么困难。 Installing and configuring Node can be more difficult. 安装和配置Node可能会更加困难。

If you already have access to PHP (and webserver) on your server, use that. 如果您已经可以访问服务器上的PHP(和Web服务器),请使用它。

If your output file can be static, (and your running windows) you could possibly use JScript to generate your static file. 如果您的输出文件可以是静态的(和您正在运行的窗口),您可以使用JScript生成静态文件。 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. 因此,您每隔一段时间就会用新生成的JSON替换静态文件。 Probably not what you want though! 可能不是你想要的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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