我可以使用javascript生成JSON文件吗? [英] Can I generate a JSON file with javascript?

查看:123
本文介绍了我可以使用javascript生成JSON文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在域example1.com上创建一个页面,并在另一个域example2.com/json.json上获取/解析一个JSON文件。可以使用javascript(在example2.com上)生成json文件吗?我认为这可以用PHP完成,但我想用javascript做。如果无法使用javascript生成json文件,是否可以从javascript文件中获取/解析对象? EX:example1.com到example2.com/js.js

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"

编辑:好的,所以无法从javascript获取/解析对象文件,因为它是客户端。所以我唯一的选择是生成一个JSON文件。用Javascript可以做到这一点吗?我知道这可能不是最好的方法,但我想用JS而不是PHP来做。

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使用Javascript的对象,我会使用jQuery。


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



和anthonybell一样,大多数浏览器都不允许跨站点脚本,所以你需要查看jsonP或者在同一个域上工作。



另外要生成JSON,您可以使用javascript创建一个对象然后循环然后输出它或者只输出JSON格式的数据,您可以在这里阅读:

http://www.w3schools.com/json/

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文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆