简体   繁体   English

遍历json数组字符串

[英]Loop through json array string

Using the following code, I am getting the json array string in the div (named remoteFeed ) 使用以下代码,我在div中获得了json数组字符串(名为remoteFeed
Say it shows: 说它显示:

{"id":"25","name":"Basil","country":"USA"}

My question is how can I get the json array elements? 我的问题是如何获取json数组元素?

Pl note that even though it shows the value in the div -- the alert (see below) shows blank!!! 请注意,即使它显示了div中的值,警报(如下所示)也显示为空白!

alert(data);  //-------------------Shows blank

NB: Being a newbie I was trying to get the value first. 注意:作为一个新手,我试图首先获得价值。 But in this case we have to convert it to json object. 但是在这种情况下,我们必须将其转换为json对象。 So if you know a better way, so that I get the values (id, name..) on the client side that is more advisable 因此,如果您知道更好的方法,以便在客户端获得更可取的值(id,name ..)

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript">


(function ($) {
$(document).ready(function(){
  $('#remoteFeed').load('http://testservice/json/');

  var data = remoteFeed.innerHTML;
  alert(data);  //-------------------Shows blank

  /*
              var jsonObj = JSON.stringify(eval("(" + data + ")"));
              for(var i in jsonObj)
            {
            var id = data[i].country_code;
            alert(id);
            }

  */
});
})(jQuery);


 var data = remoteFeed.innerHTML;
  alert(data);

</script>

</head>
<body>

      <div id="remoteFeed"></div>

</body>

Why the alert() is blank 为什么alert()为空白

You're getting a blank value in the alert() call because you're not waiting for the load() request to complete. 由于您没有等待load()请求完成,因此您在alert()调用中获得了空白值。 Instead of immediately alerting the value, consider passing a callback function to load() . 与其立即提醒该值,不如考虑将回调函数传递给load() The callback function is executed once the request is completed. 请求完成后,将执行回调函数。

For example: 例如:

$('#remoteFeed').load('http://testservice/json/', function() {
    alert($('#remoteFeed').html());
});

How to treat a JSON string as an object 如何将JSON字符串视为对象

Basically, you can use the native JSON object in modern browsers to parse a JSON string into an object. 基本上,您可以在现代浏览器中使用本机JSON对象将JSON字符串解析为一个对象。

For example: 例如:

var json_object = JSON.parse(json_string);

Alternatively, you'll be far better off using one of jQuery's other AJAX functions , that are more suitable to this task than load() which is meant for loading HTML. 另外,使用jQuery的其他AJAX函数之一会更好得多,该函数比用于加载HTML的load()更适合此任务。

For example: 例如:

$.getJSON('http://testservice/json/', function(json_object) {
    for(var i in json_object) {
       var id = json_object[i].country_code;
       alert(id);
    }
});

In this example the method $.getJSON() performs an AJAX request, assumes the returned data is in JSON format and automatically parses it for you. 在此示例中,方法$.getJSON()执行AJAX请求,并假定返回的数据为JSON格式并自动为您解析。 The callback function is then passed the resulting JSON object. 然后,将向回调函数传递生成的JSON对象。

To get json result data you can use jQuery.getJSON() method 要获取json结果数据,可以使用jQuery.getJSON()方法

refer the jquery site http://api.jquery.com/jquery.getjson/ 请参阅jQuery网站http://api.jquery.com/jquery.getjson/

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

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