从另一个网站获取JSON并转换为数组或CSV。

huangapple go评论83阅读模式
英文:

get JSON from another site and turn into array or csv

问题

我正在尝试将跨域JSON中键为"1"的值转换为我的网站上的js数组。

我尝试使用$.getJSON(),但遇到了跨域来源错误。我尝试使用AJAX,也遇到了跨域来源错误。

有没有办法绕过这个问题并使用JSON?

这是我使用$.getJSON()的尝试:

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

这是我使用AJAX的尝试:

$.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) {
             //对JSON中的每一行进行操作
             myArray.push(item);
          });
        }
    }
});

如果唯一的方法是在服务器上进行操作,那么如何解析JSON并将键"1"的值转换为Go(Golang)中CSV文件的元素?

英文:

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

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

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

Here is my attempt using $.getJSON():

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

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);
          });
        }
    }
});

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 ).

答案1

得分: 2

var json = 'http://hawttrends.appspot.com/api/terms/';
$.getJSON(json, function(trends){
$.ajax({
type:'GET',
url:json,
dataType:'JSONP',
data: trends,
success: function(msg){
// 使用msg进行操作
}
});
});
由于trends返回了$.getJSON的数据,你稍后运行了AJAX。如果你有服务器访问权限,这样做没有什么理由。

使用PHP:

英文:

Like:

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
    }
  });
});

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.

Using PHP:

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

huangapple
  • 本文由 发表于 2014年6月4日 06:31:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/24026056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定