简体   繁体   English

jQuery .getJSON返回JSON但无法通过result.key访问

[英]jQuery .getJSON return JSON but can't access by result.key

My call to the REST server using $.getJSON(url).done(function(data) {}); 我使用$.getJSON(url).done(function(data) {});对REST服务器的调用$.getJSON(url).done(function(data) {}); return this 返回这个

[
   "{"   id":1,
   "medname":"Medication No. 1",
   "qty":"2",
   "pDay":"3",
   "beforeAfterMeal":null
}", "{
   "id":3,
   "medname":"Medication No. 2",
   "qty":"1",
   "pDay":"1",
   "beforeAfterMeal":null
}", "{
   "id":4,
   "medname":"Medication 3",
   "qty":"4",
   "pDay":"1",
   "beforeAfterMeal":null
}
]

from console.log in Chrome. 从Chrome中的console.log中。

By using $.each() I can loop through 3 times (thus it knows there are 3 entries in the JSON array), but when I use data[1].id or data[1].medname or any key, it gives me undefined or null ? 通过使用$ .each(),我可以循环3次(因此它知道JSON数组中有3个条目),但是当我使用data[1].iddata[1].medname或任何键时,它会给出我是undefined还是null

My doubt is, does the returned JSON array correct? 我的疑问是,返回的JSON数组正确吗? It is returned from json_encode() (PHP) so I assumed it is correct. 它是从json_encode()(PHP)返回的,所以我认为它是正确的。

Also, I tried $.parseJSON() to no result. 另外,我尝试$.parseJSON()没有结果。

Maybe I am looking at the wrong place? 也许我在找错地方了? A pointer will be of immense help. 指针将提供巨大帮助。

This is the raw return value from the server (after json_encode() ): 这是服务器的原始返回值(在json_encode()之后):

[
  "{\"id\":1,\"medname\":\"Medication No. 1\",\"qty\":\"2\",\"pDay\":\"3\",\"beforeAfterMeal\":null}",
  "{\"id\":3,\"medname\":\"Medication No. 2\",\"qty\":\"1\",\"pDay\":\"1\",\"beforeAfterMeal\":null}",
  "{\"id\":4,\"medname\":\"Medication 3\",\"qty\":\"4\",\"pDay\":\"1\",\"beforeAfterMeal\":null}"
]

Is the above correct? 以上正确吗? If not, it would be useful for me to know which part is actually wrong. 如果不是这样,对我来说知道哪一部分实际上是错误的对我很有用。

UPDATE 更新

I think I am getting where I made the mistake. 我想我正在弄错地方。

Here is what happens in the server, for each of the entries, they are individually encoded using json_encode() , and added to an array. 这是服务器中发生的情况,对于每个条目,它们都使用json_encode()进行单独编码,然后添加到数组中。 In the end, the array is again encoded with json_encode() 最后,再次使用json_encode()对数组进行编码

Here is the var_dump for the array, before I explicitly call json_encode(array); 这是数组的var_dump,在我显式调用json_encode(array);

array (size=3)
  0 => string '{"id":1,"medname":"Medication No. 1","qty":"2","pDay":"3","beforeAfterMeal":null}' (length=81)
  1 => string '{"id":3,"medname":"Medication No. 2","qty":"1","pDay":"1","beforeAfterMeal":null}' (length=81)
  2 => string '{"id":4,"medname":"Medication 3","qty":"4","pDay":"1","beforeAfterMeal":null}' (length=77)

Is this where the mistake is? 这是错误的地方吗? Should I not encode each entries individually first? 我是否应该不首先对每个条目进行单独编码? :/ :/

Ok, to sum it up. 好吧,总结一下。

The error here is that you have ran json_encode on all of the elements in the array and then running json_encode again on the full array. 这里的错误是您对数组中的所有元素都运行了json_encode ,然后在整个数组中再次运行了json_encode You should only run it once, on the full array. 您应该只在整个阵列上运行一次。

Wrong way 错误的方法

$arr[] = json_encode(array('name' => 'hank'));
$arr[] = json_encode(array('name' => 'jofryhs'));
echo json_encode($arr);

Result: ["{\\"name\\":\\"hank\\"}","{\\"name\\":\\"jofryhs\\"}"] 结果: ["{\\"name\\":\\"hank\\"}","{\\"name\\":\\"jofryhs\\"}"]

Correct way 正确的方法

$arr = array(
    array('name' => 'hank'),
    array('name' => 'jofryhs')
);
echo json_encode($arr);

Result: [{"name":"hank"},{"name":"jofryhs"}] 结果: [{"name":"hank"},{"name":"jofryhs"}]

in php code use: 在php代码中使用:

<?php
    $c = array(array("id"=>1,
       "medname"=>"Medication No. 1",
       "qty"=>"2",
       "pDay"=>"3",
       "beforeAfterMeal"=>null),
    array("id"=>3,                  
       "medname"=>"Medication No. 2",
       "qty"=>"1",
       "pDay"=>"1",
       "beforeAfterMeal"=>null));

    echo json_encode($c, JSON_FORCE_OBJECT);
?>

this is the correct json format 这是正确的json格式

[
   {"id":1,
   "medname":"Medication No. 1",
   "qty":"2",
   "pDay":"3",
   "beforeAfterMeal":null
}, {
   "id":3,
   "medname":"Medication No. 2",
   "qty":"1",
   "pDay":"1",
   "beforeAfterMeal":null
}, {
   "id":4,
   "medname":"Medication 3",
   "qty":"4",
   "pDay":"1",
   "beforeAfterMeal":null
}
]

then you can use 那么你可以使用

$.getJSON(url).done(function(data) {
    alert(data[0].id);
});

jsfiddle http://jsfiddle.net/suhailvs0/v8eGg/ jsfiddle http://jsfiddle.net/suhailvs0/v8eGg/

If you got the string value from the server you need to parse it to get the object: 如果从服务器获取了字符串值,则需要对其进行解析以获取对象:

$.getJSON(url).done(function(data) {
    var obj = JSON.parse(data);
    var obj1 = obj[0]; 
    // and so on
});

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

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