jQuery 我可以用 javascript 生成一个 JSON 文件吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15915385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I generate a JSON file with javascript?
提问by
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"
我想在域“example1.com”上创建一个页面,并在另一个域“example2.com/json.json”上获取/解析一个 JSON 文件。可以用javascript(在example2.com上)生成json文件吗?我认为这可以用 php 完成,但我想用 javascript 来完成。如果无法使用 javascript 生成 json 文件,是否可以从 javascript 文件中获取/解析对象?例如:“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. 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.
编辑:好的,所以不可能从 javascript 文件中获取/解析一个对象,因为它是客户端。所以我唯一的选择是生成一个 JSON 文件。可以用 Javascript 做到这一点吗?我知道这可能不是最好的方法,但我想用 JS 来做,而不是用 PHP。
采纳答案by John
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/
要使用 Javascript 解析 JSON 对象,我将使用 jQuery。
http://api.jquery.com/jQuery.parseJSON/
就像 anthonybell 说的,大多数浏览器不允许跨站点脚本,所以你需要查看 jsonP 或在同一个域上工作。
同样要生成 JSON,您可以使用 javascript 创建一个对象,然后循环遍历它并输出它,或者仅以 JSON 格式输出您的数据,您可以在此处阅读:http:
//www.w3schools.com/json/
回答by anthonybell
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. you could look into jsonP which is a work-around this rule.
尽管存在安全风险,但 javascript 不允许跨站点(跨站点脚本)进行 ajax 调用。您可以查看 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). A server has to be listening somewhere for a request.
此外,您不能调用另一个网站或您自己的服务器来运行 javascript 以返回某些内容,因为 javascript 仅在客户端浏览器中运行(除非您使用像 node.js 这样的 javascript 服务器)。服务器必须在某处侦听请求。
回答by Sam Deering
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);
回答by NickSlash
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. If you're doing that theres no point even usng JSON.
让 Javascript 生成您的 JSON 会将 Javascript 返回给客户端。如果您这样做,即使使用 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. Installing and configuring Node can be more difficult.
创建脚本以生成 JSON(并创建基本服务器来托管它)并不是那么困难。安装和配置 Node 可能会更困难。
If you already have access to PHP (and webserver) on your server, use that.
如果您已经可以访问服务器上的 PHP(和网络服务器),请使用它。
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!
如果您的输出文件可以是静态的(以及您正在运行的窗口),您可能会使用 JScript 来生成您的静态文件。这将需要您实际运行脚本(或可能创建一个计划任务来为您运行它)。所以你会经常用新生成的 JSON 替换你的静态文件。可能不是你想要的!