简体   繁体   English

JavaScript数组周围的引号(可能非常容易修复)

[英]Quotes Around JavaScript Arrays (Probably Very Easy Fix)

I'm having a problem with quotes in my arrays, and it's probably a super easy fix, but I just can't figure out how on earth I'm supposed to fix it. 我的数组中的引号有问题,这可能是一个非常简单的修复程序,但我只是想不通我应该如何修复它。

I'm using JQPlot to make charts of my social media information, as in Facebook likes, Twitter followers, etc. Initially when I load up the page I show everything from the past two weeks. 我正在使用JQPlot制作我的社交媒体信息的图表,例如在Facebook点 ,Twitter追随者等中。最初,当我加载页面时,我会显示过去两周的所有内容。

I then added two inputs for date, using CalenderDateSelect , a Prototype library. 然后,我使用原型库CalenderDateSelect添加了两个日期输入。 I use jQuery for everything else except for this, so all my jQuery selectors are: 我将jQuery用于除此以外的所有其他功能,因此我所有的jQuery选择器都是:

j$('element').functon();

I bring in my original javascript arrays with the help of PHP. 我借助PHP引入了原始的javascript数组。 They look like the following: 它们如下所示:

<script>

likes=[
  <?
    $i = 0;
    while($like = mysql_fetch_assoc($fb_data)) {
        $d_day = date("d-M-y", $like['date']);
        if ($i > 0) {
            echo ',';
        }
        echo "['".$d_day."', ".$like['likes']."]";
        $i++;
    }
  ?>];

<? mysql_data_seek($fb_data, 0); ?>//this resets $fb_data so I can use it again

dislikes=[
  <?
    $i = 0;
    while($like = mysql_fetch_assoc($fb_data)) {
        $d_day = date("d-M-y", $like['date']);
        if ($i > 0) {
            echo ',';
        }
        echo "['".$d_day."', ".$like['dislikes']."]";
        $i++;
    }
  ?>];

<? mysql_data_seek($fb_data, 0); ?>

impressions=[
  <?
    $i = 0;
    while($like = mysql_fetch_assoc($fb_data)) {
        $d_day = date("d-M-y", $like['date']);
        if ($i > 0) {
            echo ',';
        }
        echo "['".$d_day."', ".$like['impressions']."]";
        $i++;
    }
  ?>];

</script>

I then put them into a function: 然后,我将它们放入一个函数中:

socialMedia(likes, dislikes, impressions,false);

This function does the following (simplified for readability) 此函数执行以下操作(为便于阅读而简化)

function socialMedia(likes, dislikes, impressions, bool) {

console.log(likes);
console.log(dislikes);
console.log(impressions);

var plot1 = j$.jqplot('chart1', [likes, dislikes, impressions], {
    title:'Facebook Stats' });

if (bool) {
    plot1.destroy();
    j$.jqplot('chart1', [likes, dislikes, impressions], {
    title:'Facebook Stats' }).replot();
}   

}

The console log here makes the following (it's correct): 控制台日志在此处进行以下操作(正确):

[["19-Nov-13", 163], ["20-Nov-13", 163], ["21-Nov-13", 164], ["22-Nov-13", 165], ["23-Nov-13", 168], ["24-Nov-13", 181], ["25-Nov-13", 205], ["26-Nov-13", 226], ["27-Nov-13", 244], ["28-Nov-13", 250], ["29-Nov-13", 269], ["30-Nov-13", 306], ["01-Dec-13", 313], ["02-Dec-13", 315]]

[["19-Nov-13", 0], ["20-Nov-13", 0], ["21-Nov-13", 0], ["22-Nov-13", 0], ["23-Nov-13", 1], ["24-Nov-13", 1], ["25-Nov-13", 0], ["26-Nov-13", 1], ["27-Nov-13", 0], ["28-Nov-13", 1], ["29-Nov-13", 0], ["30-Nov-13", 0], ["01-Dec-13", 0], ["02-Dec-13", 0]]

[["19-Nov-13", 68], ["20-Nov-13", 116], ["21-Nov-13", 63], ["22-Nov-13", 15896], ["23-Nov-13", 79617], ["24-Nov-13", 42948], ["25-Nov-13", 22290], ["26-Nov-13", 1369], ["27-Nov-13", 928], ["28-Nov-13", 921], ["29-Nov-13", 631], ["30-Nov-13", 700], ["01-Dec-13", 743], ["02-Dec-13", 300]]

Note the quotes around the day-month-year. 请注意报价单日-年-年左右。

So far everything works correctly. 到目前为止,一切正常。

I then go and change the end-date (e) and start-date (s) inputs and call this function when I do so: 然后,我去更改结束日期(e)和开始日期(s)输入,并在执行此操作时调用此函数:

function getNewSocialData(e, s) {
    j$.ajax({
         type: 'GET',
         url: 'ajax/social_media.php?e='+e+'&s='+s,
         success:function(json){

            data = json;
            data = json.split("T");

            socialMedia(data[0],data[1],data[2],true);
         },
         error: function (xhr, ajaxOptions, thrownError) {
            alert('There appears to be a problem with the information you submitted. Please try again or contact us.');
          }
    });
}

In social_media.php I do the following: 在social_media.php中,我执行以下操作:

$end_date = date("Y-m-d 00:00", strtotime($_GET['e']));
$start_date = date("Y-m-d 00:00", strtotime($_GET['s']));

$fb_data = getFacebookData(strtotime($start_date), strtotime($end_date));

$inside_likes = array();
$inside_dislikes = array();
$inside_impressions = array();

$i = 0;
while($like = mysql_fetch_assoc($fb_data)) {
    $d_day = date("d-M-y", $like['date']);

    $string = $i != 0 ? ',' : '';

    $inside_likes[] = $string.'"'.$d_day.'",'.$like['likes'];
    $inside_dislikes[] = $string.'"'.$d_day.'",'.$like['dislikes'];
    $inside_impressions[] = $string.'"'.$d_day.'",'.$like['impressions'];

    $i++;
}   

$data['likes'][] = $inside_likes;
$data['dislikes'][] = $inside_dislikes;
$data['impressions'][] = $inside_impressions;

foreach ($data as $data_part) {
   foreach ($data_part as $part) {
       foreach($part as $info) {
           echo $info;   
       }
   echo 'T'; //we split on this 
   }
}

We then take this data and, as you saw int the ajax call, we run it through the socialMedia() function and console.log the data. 然后,我们将获取这些数据,并且正如您在ajax调用中看到的那样,我们通过socialMedia()函数运行它,并console.log该数据。 This time my data is all like the following: 这次我的数据如下:

"20-Nov-13",163,"21-Nov-13",164,"22-Nov-13",165,"23-Nov-13",168,"24-Nov-13",181,"25-Nov-13",205,"26-Nov-13",226,"27-Nov-13",244,"28-Nov-13",250,"29-Nov-13",269,"30-Nov-13",306,"01-Dec-13",313,"02-Dec-13",315

"20-Nov-13",0,"21-Nov-13",0,"22-Nov-13",0,"23-Nov-13",1,"24-Nov-13",1,"25-Nov-13",0,"26-Nov-13",1,"27-Nov-13",0,"28-Nov-13",1,"29-Nov-13",0,"30-Nov-13",0,"01-Dec-13",0,"02-Dec-13",0

"20-Nov-13",116,"21-Nov-13",63,"22-Nov-13",15896,"23-Nov-13",79617,"24-Nov-13",42948,"25-Nov-13",22290,"26-Nov-13",1369,"27-Nov-13",928,"28-Nov-13",921,"29-Nov-13",631,"30-Nov-13",700,"01-Dec-13",743,"02-Dec-13",300

And, as you can see, these are not arrays, they're strings, so JQPlot throws the error "uncaught exception: No Data". 而且,如您所见,这些不是数组, 而是字符串,因此JQPlot引发错误“未捕获的异常:无数据”。 We then modify the success condition to put them into arrays: 然后,我们修改成功条件以将它们放入数组:

success:function(json){
    var likes = [];
    var dislikes = [];
    var impressions = [];
    var followers = [];
    var following = [];
    var tweets = [];

data = json;
data = json.split("T");

likes.push(data[0]);
dislikes.push(data[1]);
impressions.push(data[2]);
followers.push(data[3]);
following.push(data[4]);
tweets.push(data[5]);

socialMedia(likes,dislikes,impressions,true);
    }....

And it console's the following: 它的控制台如下:

[""20-Nov-13",163,"21-Nov...13",313,"02-Dec-13",315"]
[""20-Nov-13",0,"21-Nov-1...Dec-13",0,"02-Dec-13",0"]
[""20-Nov-13",116,"21-Nov...13",743,"02-Dec-13",300"]

Which has all of my data there, but inside quotes, causing JQPlot to being unable to parse it, and defaults my chart down to December 31st, 1969. 其中包含我的所有数据,但在引号内,导致JQPlot无法对其进行解析,并将我的图表默认拖延至1969年12月31日。

So, my questions: How do I push in the data in such a way so that the quotes don't go around it? 因此,我的问题是:如何以一种不加引号的方式推入数据? I feel I may have a string and an array mixed up here somehow. 我觉得我可能在这里以某种方式将字符串和数组混合在一起。 Can somebody point me in the right direction? 有人可以指出我正确的方向吗?

Thanks a million! 太感谢了!

EDIT: 编辑:

Thank you everyone for your help. 感谢大家的帮助。 I'm getting closer. 我越来越近了。 I changed my PHP to run like so: 我将PHP更改为如下运行:

while($like = mysql_fetch_assoc($fb_data)) {
$d_day = date("d-M-y", $like['date']);

$inside_likes[] = array('"'.$d_day.'",'.$like['likes']);
$inside_dislikes[] = array('"'.$d_day.'",'.$like['dislikes']);
$inside_impressions[] = array('"'.$d_day.'",'.$like['impressions']);
}   

$data['likes'] = $inside_likes;
$data['dislikes'] = $inside_dislikes;
$data['impressions'] = $inside_impressions;

echo json_encode($data);
exit;

And my AJAX call now does the following: 现在,我的AJAX调用执行以下操作:

j$.ajax({
         type: 'GET',
         url: 'ajax/social_media.php?e='+e+'&s='+s,
     dataType : "json",
         success:function(json){
            var likes = json['likes'];
            var dislikes = json['dislikes'];
            var impressions = json['impressions'];

    socialMedia(likes, dislikes, impressions, true);
         },
        ...
    });

But now, when I console.log it, I'm getting this, note the double quotes. 但是现在,当我进行console.log记录时,我得到了,请注意双引号。

[[""19-Nov-13",163"], [""20-Nov-13",163"], [""21-Nov-13",164"], [""22-Nov-13",165"], [""23-Nov-13",168"], [""24-Nov-13",181"], [""25-Nov-13",205"], [""26-Nov-13",226"], [""27-Nov-13",244"], [""28-Nov-13",250"], [""29-Nov-13",269"], [""30-Nov-13",306"], [""01-Dec-13",313"], [""02-Dec-13",315"]]

When I should be getting this: 当我应该得到这个时:

[["19-Nov-13", 163], ["20-Nov-13", 163], ["21-Nov-13", 164], ["22-Nov-13", 165], ["23-Nov-13", 168], ["24-Nov-13", 181], ["25-Nov-13", 205], ["26-Nov-13", 226], ["27-Nov-13", 244], ["28-Nov-13", 250], ["29-Nov-13", 269], ["30-Nov-13", 306], ["01-Dec-13", 313], ["02-Dec-13", 315]]

Does anybody have any suggestions? 有人有什么建议吗? Thank you! 谢谢!

SOLUTION: 解:

With help from below, the answer to get the arrays in the string and integer format was to do the following: 在下面的帮助下,获取字符串和整数格式的数组的答案是执行以下操作:

while($like = mysql_fetch_assoc($fb_data)) {
$d_day = date("d-M-y", $like['date']);

$inside_likes[] = array($d_day, intval($like['likes']));
$inside_dislikes[] = array($d_day, intval($like['dislikes']));
$inside_impressions[] = array($d_day, intval($like['impressions']));
}   

$data['likes'] = $inside_likes;
$data['dislikes'] = $inside_dislikes;
$data['impressions'] = $inside_impressions;

echo json_encode($data);

In social_media.php , you seem to be pushing strings instead of arrays. social_media.php ,您似乎在推送字符串而不是数组。

This seems wrong: 这似乎是错误的:

$inside_likes[] = $string.'"'.$d_day.'",'.$like['likes'];

Try this: 尝试这个:

$inside_likes[] = array($string.'"'.$d_day, $like['likes']);

It also looks like you're building out your own JSON in a script. 看起来您正在脚本中构建自己的JSON。 That's not a good idea. 那不是一个好主意。 I'd look into formatting JSON using json_encode() . 我会考虑使用json_encode()格式化JSON。

Update: 更新:

Each array should contain two elements: the date string and the number of likes. 每个数组应包含两个元素:日期字符串和点赞次数。 What you're currently doing is concatenating both into a single string, which is not what you want. 您当前正在做的是将两个字符串都串联在一起,这不是您想要的。

Try this: 尝试这个:

$inside_impressions[] = array($d_day, $like['impressions']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM