获得JSON从其他网站,变成数组或CSV [英] get JSON from another site and turn into array or csv

查看:455
本文介绍了获得JSON从其他网站,变成数组或CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把在1这个跨域JSON的钥匙插入我的网站上JS数组的值。

I'm trying to turn the value at the key of "1" of this cross-domain JSON into a js array on my site.

我试图用 $。的getJSON(),但我遇到了跨域出身的错误。我尝试AJAX,并得到了跨域出身的错误。

I tried using $.getJSON(), but I encountered a cross-domain origin error. I tried AJAX and got a cross-domain origin error.

有没有什么办法可以解决这个获取和用户的JSON?

Is there any way I can get around this and user the JSON?

下面是我尝试使用 $的getJSON()

var trends = '';
var json = 'http://hawttrends.appspot.com/api/terms/';
$.getJSON(json, function(trends){
    console.log(trends["1"]);
});

下面是我的AJAX的尝试:

Here is my AJAX attempt:

    $.ajax({
    type:'GET',
    dataType:'jsonp',
    data:{},
    url:'http://hawttrends.appspot.com/api/terms/',
    error:function(jqXHR, textStatus, errorThrown){
        console.log(jqXHR);
    },
    success:function(msg){
        if (msg) {
          var myArray = [];
          $.each(msg, function(i, item) {
             //do whatever you want for each row in json
             myArray.push(item);
          });
        }
    }
});

如果做到这一点的唯一方法是在服务器上。我怎样才能解析JSON并打开的键1的值在Go(Golang)在CSV文件中的元素。

If the only way to do this is on a server. How can I parse the JSON and turn the values of the key "1" to elements in a CSV file in Go ( Golang ).

推荐答案

像:

var json = 'http://hawttrends.appspot.com/api/terms/';
$.getJSON(json, function(trends){
  $.ajax({
    type:'GET',
    url:json,
    dataType:'JSONP',
    data: trends,
    success: function(msg){
      // do stuff with the msg
    }
  });
});

由于趋势返回从 $的数据。的getJSON 您稍后运行AJAX。实在是没有理由这样做,如果你有服务器的访问。

Since trends returns the data from $.getJSON you run your AJAX later. There is really no reason to do this, if you have server access.

使用PHP:

<?php
$dataArray = json_decode(file_get_contents('http://hawttrends.appspot.com/api/terms/'));
// $dataArray has all data
?>

这篇关于获得JSON从其他网站,变成数组或CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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