【问题标题】:Javascript array formatting error from php [duplicate]来自php的Javascript数组格式错误[重复]
【发布时间】:2013-08-21 14:16:58
【问题描述】:

我有一个数组,比如:

[['jkjhkfhkjh jkj jkjhk', '54.324705', '-2.749629', '189', 1, 1, 0, 0, 'Test two', '2 ', '10+', 'http://xx.co.uk/xx/?post_type=listings&p=189', '<img width="160" height="85" src="http://www.xx.com/manage/wp-content/uploads/wheelbase.jpg" class="attachment-thumbnail wp-post-image" alt="wheelbase" title="wheelbase">', '189'],['fghfghfgh  fghfg hf dfh dfh', '54.323174', '-2.744554', '188', 1, 1, 0, 0, 'Test', '2 ', '10+', 'http://xx/xx/?post_type=listings&p=188', '<img width="160" height="85" src="http://www.xx.com/manage/wp-content/uploads/wheelbase.jpg" class="attachment-thumbnail wp-post-image" alt="wheelbase" title="wheelbase">', '188']];

我使用php获取数据:

echo "[";
  for ($i=0; $i < count($json); $i++) { 
    echo "['" . $json[$i]["content"] . "', '". $json[$i]["lat"] . "', '" . $json[$i]["long"] . "', '" . $json[$i]["id"] . "', 1, 1, 0, 0, '" . $json[$i]["title"] . "', '2 ', '10+', '" . $json[$i]["link"] . "', '<img width=\"160\" height=\"85\" src=\"http://www.xx.com/manage/wp-content/uploads/wheelbase.jpg\" class=\"attachment-thumbnail wp-post-image\" alt=\"wheelbase\" title=\"wheelbase\" />', '" . $json[$i]["id"] . "'],";
  }   
  echo "]";

(我调用了一个变量$json,忽略我调用它的事实,它不是json)

所以我将这些内容回显到将被隐藏的 div 中。然后在 javascript 中获取它,我试试这个:

var locations = $('#listingsarray').html();

似乎可以很好地转换为控制台,但它是以文本而不是数组的形式出现的。我怎样才能把它变成一个数组?

【问题讨论】:

  • 您如何检索 JSON?
  • HTML 不支持数组,所以当然是文本?
  • @JasonMcCreary 我只是以 javascript 数组的格式回显了一些数据
  • @adeneo 是的,我不确定如何翻译。我试过 .text() 但同样,它不是一个数组。如何将它作为数组带入我的 js 中?
  • 显示您正在回显的行。

标签: php javascript jquery arrays


【解决方案1】:

使用JSON.parse 将字符串解析为 JSON,但请记住它是无效的。你最好这样做:

echo json_encode(array_map("array_values",$json));

这假定键的顺序是“内容”、“纬度”、“长”……并且没有其他键。如果不是这种情况,您需要遍历数组以确保一切正常,然后使用json_encode

【讨论】:

    【解决方案2】:

    首先,您需要构建一个有效的 json 数组表示,您使用的技术会导致语法无效。这应该对你更好。

    $data = array();
    for ($i=0; $i < count($json); $i++) { 
      $data[] =  array(
                      $json[$i]["content"],
                      $json[$i]["lat"],
                      $json[$i]["long"],
                      $json[$i]["id"],
                      1,
                      1,
                      0,
                      0,
                      $json[$i]["title"],
                      '2',
                      '10+',
                      $json[$i]["link"],
                      '<img width=\"160\" height=\"85\" src=\"http://www.xx.com/manage/wp-content/uploads/wheelbase.jpg\" class=\"attachment-thumbnail wp-post-image\" alt=\"wheelbase\" title=\"wheelbase\" />',
                      $json[$i]["id"]
                  );
    }
    $jsonString = json_encode($data);
    

    如果你想把它放到一个 json 上下文中,你可以简单地这样做

    <script>var jsonArray = <?= $jsonString ?>;</script>
    

    【讨论】:

    • 感谢您提供此信息。我不认为它仍然解析为一个数组,在控制台中它只是一个没有方括号[] 的单维数组,所以它就像value,value,value 等等(显然用实际值替换值预期。但我认为它正在到达那里!
    • @JoshBoothe 确保您在回声周围加上引号,例如&lt;script&gt;var jsonArray = '&lt;?= $jsonString ?&gt;';&lt;/script&gt;,它应该是&lt;script&gt;var jsonArray = &lt;?= $jsonString ?&gt;;&lt;/script&gt;
    • @JoshBoothe 并仔细检查您是如何构建 $data 变量的......您应该构建一个数组数组。
    【解决方案3】:

    如果您在页面加载时输出此内容,请尝试以下操作:

    echo "<script>var locations = [";
    for ($i=0; $i < count($json); $i++) { 
      //...
    }   
    echo "]</script>";
    

    然后您的 javascript 将可以直接访问 locations 变量和数组,而无需翻译或 evaling 它。

    虽然我肯定会考虑使用 json_encode,因为这些库在处理边缘情况方面要好得多,而且代码也更简洁。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 2012-06-29
      • 1970-01-01
      • 2017-06-23
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多