简体   繁体   English

如何在Javascript中访问JSON数组

[英]How to access JSON array in Javascript

How do i get "LSZ09". 我如何获得“ LSZ09”。

var el1=data[0];
alert(el1);

This gives me "a" from array, as position 1 gives me r, 2 r, 3 a, 4y ,.. 这给了我数组的“ a”,因为位置1给了我r,2 r,3 a,4y...。

The array is received through a " echo json_encode($punten); " 该数组通过“ echo json_encode($ punten);”接收

Also when i try 当我尝试

var jsonDataArray = JSON.parse(data);

I get a syntax error: 我收到语法错误:

SyntaxError: JSON.parse: unexpected character

Code: 码:

$.ajax({ url: 'getPunten.php',
                        data: {statnam: jSelectedStation[0]},
                        type: 'get',
                        success: function(data) {

Received from php script, with last line being: echo json_encode($punten); 从php脚本接收,最后一行是:echo json_encode($ punten);

[{"STATDEV":"LSZ09 ","0":"LSZ09 ","DEVPKT":"1","1":"1","PKTTYP":"S","2":"S","KARTNR":"0","3":"0","BITNRK":"1","4":"1","BITSTATUS":"0","5":"0","TYPE":"I ","6":"I "},{"STATDEV":"LSZ10 ","0":"LSZ10 ","DEVPKT":"1","1":"1","PKTTYP":"S","2":"S","KARTNR":"0","3":"0","BITNRK":"2","4":"2","BITSTATUS":"0","5":"0","TYPE":"I ","6":"I "}

php: PHP:

$db = new PDO ("xxxx");
$qry="SELECT r.refnam, r.zustnr FROM refdev r INNER JOIN (SELECT refnam, COUNT(*) cnt FROM refdev rc GROUP BY refnam) rc ON rc.refnam = r.refnam LEFT OUTER JOIN texte t ON r.sigtnr = t.textnr WHERE rc.cnt = $aantal AND t.tstring LIKE '%$tekst%' ORDER BY r.refnam, r.zustnr";
$filterQry = $db->query($qry);
$filtered = $filterQry->fetchAll();
echo json_encode($filtered);

If that php you've shown is the one you're using,no, it won't work. 如果您显示的php是您正在使用的php,否,它将无法正常工作。

You see, the text that gets passed to the javascript includes the results of both var_dump and echo . 你看,那被传递给JavaScript文本包括结果var_dumpecho

Consider the following php code: 考虑下面的PHP代码:

<?php
    $filtered = array(1,2,3);
    var_dump($filtered);
    echo json_encode($filtered);    
?>

What do you think the result of this is? 您认为这是什么结果? Hint: it's not what you've been expecting. 提示:这不是您所期望的。 You seem to be expecting it to be 您似乎期望它会

 [1,2,3]

Well, it is - at least in part. 好吧-至少有一部分。 That's the output of the last line only. 那只是最后一行的输出。 But we've not considered the middle line var_dump($filtered); 但是我们没有考虑中间线var_dump($filtered); - the code that will output something similar to the code you've labelled with Received from php script, with last line being: echo json_encode($punten); -该代码将输出与您用Received from php script, with last line being: echo json_encode($punten);到的代码相似的代码Received from php script, with last line being: echo json_encode($punten);

The output of my snippet is actually: 我的代码段的输出实际上是:

array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } [1,2,3] array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } [1,2,3]

That's why you're getting the first 5 elements containing 'a' 'r' 'r' 'a' 'y'!! 这就是为什么要获取包含“ a”,“ r”,“ r”,“ a”,“ y”的前5个元素的原因! Its also why the call to JSON.parse(data); 这也是为什么调用JSON.parse(data); fails - "arrray(3)" isn't valid json. 失败- "arrray(3)"不是有效的json。

Hint: when you navigate to the php script with a browser, what you see there is what will be retrieved with an ajax call. 提示:当您使用浏览器导航到php脚本时,看到的内容将通过ajax调用进行检索。

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

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