1

I'm trying to use Javascript to read stock data from yahoo finance at "http://table.finance.yahoo.com/table.csv?s=000001.sz", which returns a csv file and convert the data into json format as in http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-ohlcv.json&callback=? to be used in highcharts.

I tried using $.ajax get and jquery.get but neither worked. Can somebody tell me how to read the data from the url and convert it into json? Thanks a lot.

4
  • 1
    Is javascript necessary for you? can't you use others. Like python has a built in CSV parser. Else, you need to get the data, parse and convert it to json yourself. I can help you with both though Commented Apr 28, 2015 at 5:17
  • i think you should use either jQuery.ajax and jQuery.get or $.ajax and $.get. Maintain uniformity. $ and jQuery are the same thing. $===jQuery returns true but jquery and jQuery aren't same. If you aren't sure if where you have used jquery instead of jQuery do a case sensitive search in any IDE and replace with jQuery or at the beginning, do var jquery= jQuery so that your code works, but i wouldn't recommend that Commented Apr 28, 2015 at 5:20
  • @Ronnie Thanks a lot. I used jQuery but I don't think that's the problem. I'm just wondering if there's anything in js that has the same function as requests.get() or urllib2.urlopen() Commented Apr 28, 2015 at 5:31
  • This should be quite cumbersome to do with JavaScript if at all possible. I hope you are using PHP. Commented Apr 28, 2015 at 5:51

2 Answers 2

1

This can be easily done with PHP.

<?php
file_put_contents("data.csv", fopen("http://table.finance.yahoo.com/table.csv?s=000001.sz", 'r'));

//reads the CSV file and save value into an associative array
function csv_to_array($filename = '', $delimiter = ',')
{
    if (!file_exists($filename) || !is_readable($filename))
        return FALSE;

    $header = NULL;
    $data   = array();
    if (($handle = fopen($filename, 'r')) !== FALSE) {
        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
            if (!$header)
                $header = $row;
            else
                $data[] = array_combine($header, $row);
        }
        fclose($handle);
    }
    return $data;
}

$arr = csv_to_array('data.csv');

//<pre></pre> tags appear only to prevent white-space collapsing

//prints the associative array
echo "<pre>";
print_r($arr);
echo "</pre>";

//displays the the JSON
echo "<pre>";
echo json_encode($arr, JSON_PRETTY_PRINT);
echo "</pre>";
?>

Now depending on the format of JSON that it acceptable to Highcharts API, you are required to tweak how the array is encoded into JSON. Also avoid using JSON_PRETTY_PRINT if the size of the incoming data is large.

Sign up to request clarification or add additional context in comments.

Comments

0

I guess you are facing Cross Domain issue. Use jsonp calls for Cross Domain requests. Use something like this

 $.ajax({
    url : "http://xx.xx.xx.xx/xxx/xxx",
    type: "GET",
    dataType: "jsonp",
    jsonp : "callback",
    success: function(data) {alert("Success");},
    error: function(data) { alert("Error"); }

    });        
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.