将json数据传递给javascript? [英] pass json data to javascript?

查看:107
本文介绍了将json数据传递给javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在javascript中将每日json数据传递给(var data),当页面加载并将历史json数据传递给javascript中的(var data2)。我不太确定,我该怎么做呢。请指教,我如何在javascript中获取(var data)函数来读取javascript数据和javascript上的(var data2)来读取历史json数据。



I am trying to pass daily json data to (var data) in javascript, when the page loads and pass historical json data to (var data2) in javascript. I am little unsure, how do I go about doing this. Please advice, in how can I get (var data) function in javascript to read daily json data and (var data2) on javascript to read historical json data.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Daily Data
            JavaScriptSerializer jss = new JavaScriptSerializer();
            ClientScript.RegisterStartupScript(this.GetType(), "TestInitPageScript",
                //string.Format("<script type=\"text/javascript\">google.load('visualization','1.0',{{'packages':['corechart','controls']}});google.setOnLoadCallback(function(){'drawVisualization'({0},'{1}','{2}');});</script>",
           string.Format("<script type=\"text/javascript\">google.load('visualization','1.0',{{'packages':['corechart','controls']}});google.setOnLoadCallback(function(){{drawVisualization({0},'{1}','{2}','{3}');}});</script>",
            jss.Serialize(GetData()),
        "Name Example",
        "Name",
        "Type Example",
        "Type Example",
         "Type,"));

        }
            else 

            {
               // Historical Data
                JavaScriptSerializer jtt = new JavaScriptSerializer();
        ClientScript.RegisterStartupScript(this.GetType(), "TestInitPageScript",
            //string.Format("<script type=\"text/javascript\">google.load('visualization','1.0',{{'packages':['corechart','controls']}});google.setOnLoadCallback(function(){'drawVisualization'({0},'{1}','{2}');});</script>",
       string.Format("<script type=\"text/javascript\">google.load('visualization','1.0',{{'packages':['corechart','controls']}});google.setOnLoadCallback(function(){{drawVisualization({0},'{1}','{2}','{3}');}});</script>",
        jtt.Serialize(GetData2()),
    "Name Example",
    "Name",
    "Type Example",
    "Type Example",
     "Type,"));

            }
        }







function drawVisualization(dataValues, chartTitle, columnNames, categoryCaption) {
            if (dataValues.length < 1)
                return;

            var data = new google.visualization.DataTable();
            data.addColumn('string', columnNames.split(',')[0], 'name');
            data.addColumn('number', columnNames.split(',')[1], 'price');
            data.addColumn('string', columnNames.split(',')[2], 'type');
            data.addColumn('datetime', columnNames.split(',')[3], 'date');

            for (var i = 0; i < dataValues.length; i++) {

                var date = new Date(parseInt(dataValues[i].Date.substr(6), 10));

                data.addRow([dataValues[i].ColumnName, dataValues[i].Value, dataValues[i].Type, date]);
            }


            var data2 = new google.visualization.DataTable();
            data2.addColumn('string', columnNames.split(',')[0], 'name');
            data2.addColumn('number', columnNames.split(',')[1], 'price');
            data2.addColumn('string', columnNames.split(',')[2], 'type');
            data2.addColumn('datetime', columnNames.split(',')[3], 'date');

            for (var i = 0; i < dataValues.length; i++) {

                var date = new Date(parseInt(dataValues[i].Date.substr(6), 10));

                data2.addRow([dataValues[i].ColumnName, dataValues[i].Value, dataValues[i].Type, date]);
            }





非常感谢您的帮助和时间。



Many thanks for your help and time.

推荐答案

如果你曾经有兴趣学习一个非常基本的东西,你就不需要问这个问题了:什么是JSON?例如,在这里阅读:http://en.wikipedia.org/wiki/JSON [ ^ ]。



什么,还不清楚?然后是提示:JSON字符串只是Javascript代码的一个片段。 它已经是Javascript。我是否还要继续?



好​​的,以防万一:JSON字符串包含某个对象的定义(实际上,使用某些Javascript语法编写的对象图。这意味着:如果您获取一些JSON字符串并将其传递给Javascript函数 eval (这是提供给脚本的Javascript解释器),它将为您提供对象本身。例如,这里解释了这一点: http://www.json.org/js.html [ ^ ]。



就是这样。



-SA
You would not need asking this question if you once got interest in learning one very basic thing: what is JSON? For example, read about it here: http://en.wikipedia.org/wiki/JSON[^].

What, still unclear? Then here is the hint: JSON string is just a fragment of Javascript code. It is already Javascript. Do I even have to continue?

Okay, just in case: JSON string contains a definition of some object (object graph, actually) written with the use of certain Javascript syntax. It means: if you take some JSON string and pass it to the Javascript function eval (which is the Javascript interpreter provided to the scripts), it will get you the object itself. This is explained, for example, here: http://www.json.org/js.html[^].

That's all.

—SA


这篇关于将json数据传递给javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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