开发者

JSON data format changed & function building unordered list out of it stopped working?

I pass some data converting it into json & json outputs like;

[{"msg":"1: AVAILABLE: should be publicly available"},{"msg":"1: API USAGE: Uses api 
of twitter to develop messaging service \n PUBLICALLY AVAILABLE: Should be publically 
available"},{"msg":"1: EX_DES_EXP: Help novice users to exchange \n design experiences 
through specifications messages"}] 

I use following script to build an unordered list using this json data:

 <script type="text/javascript" language="javascript">
 jQuery(document).ready(function() { 
 jQuery.getJSON("http://127.0.0.1/conn_msg2_qua.php", function (jsonData) {
 var markup;
 markup = [];
 jQuery.each(jsonData, function (i, j) {
    markup.push("<li>");
    markup.push(j.msg);
    markup.push("</li>");
 });
 jQuery('#msg_q').append(markup.join(""));
 });});
 </script>

  <script> 

Now I change implementation of data that was converted to json as bit & json app开发者_如何学编程ears like

["1 AVAILABLE: should be publicly available","2 API USAGE: Uses api of twitter to  
 develop messaging service \n PUBLICALLY AVAILABLE: Should be publically available","3 
 EX_DES_EXP: Help novice users to exchange \n design experiences through 
 specifications messages"] 

and my function stopped printing it as unordered list. Whats the problem causes this. How do I again make it work.


Replace:

markup.push(j.msg);

with:

markup.push(j);

Update:

I'd also recommend to change the way you inject text into your HTML. Instead of:

markup.push("<li>");
markup.push(j);
markup.push("</li>");
[...]
jQuery('#msg_q').append(markup.join(""));

... use something like:

var li = $("<li></li>");
li.text(j);
markup.push(li);
[...]
jQuery('#msg_q').append(markup);


Since it's just an array of strings, you need just the j from your loop (the string), not the .msg property (which a string doesn't have), like this:

jQuery.each(jsonData, function (i, j) {
  markup.push("<li>");
  markup.push(j);
  markup.push("</li>");
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜