jQuery的的getJSON分析数组 [英] getjson jquery parsing an array

查看:155
本文介绍了jQuery的的getJSON分析数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

得到了简化阵列工作见下图

复杂的阵列上跟进解析看<一href="http://stackoverflow.com/questions/2734989/parsing-a-complicated-array-with-getjson-jquery">here.


TLDR :想要得到的每个标题来自一​​个数组并将其插入一个div不知道什么是使用jQuery的DIV中 - 的getJSON

修改:将数据从一个软件即输出JSON字符串每对夫妇的新的数据秒到来,所以我需要提取数据的阵列中的D,如下面的例子。所以我应该得到标题和放大器; 034324324等每一个。

EDIT2 更新code精确匹配我的..

我有JSON数组可以说:

  {
天:
[星期天星期一星期二星期三星期四星期五星期六]
}
 

和我想要解析它为每个头衔将其插入到一个DIV。

线沿线的尝试

 函数的getData(){
31 $ .ajaxSetup({缓存:假}); //&LT;! - 停止IE浏览器从缓存中的数据//  - &GT;
32
33 $ .getJSON(testurl,功能(数据){
34 $每个(data.days,功能(I,项目){
35 $​​('#testfield)追加。('&LT; P&GT;'+项目+'&LT; / P&GT;');
36
37});
38});
 

无济于事。

我在其他地方,但在我知道什么要求我作出的情况下使用的getJSON。 例如:

  $('#姓名)HTML(data.Projectname)。
 

这是所有工作完全正常,但在这种情况下,我需要的细节出数组的不知道是什么我敢肯定,数组里面有简单的东西,我很想念这里还是做错了^^。

的JSON我当前已经在与如下:

  {项目名:ertyofhj,Projectid:aqwertyuqw,Projecttitle:ertyofhj}
 

该数组只是一个方式来看待出来拉出数据,而不知道什么是数组中。

我只是不停地增加更多。 调试萤火虫或IE浏览器的调试器(AARGH),它会按

  33 $ .getJSON(testurl,功能(数据){
 

然后

  38});
 

完全跳过

  34 $每个(data.days,功能(I,项目){
 

-The GET请求触发罚款,并返回正确的JSON字符串。但现在它不会填补$。每次,或者即使我专门请教了一定的字符串。 在上述注萤火虫的还告诉我,这是不是JSON格式?

改变了原来的JSON字符串到简单的东西。作为是星期几将加载(本周日)。所以我得到的地方。但我需要能够调用更复杂的阵列。例如(使用天),每天都有一个特定的时间就可以了,我需要显示以及同时每天要在新的一行尝试添加一个+&LT; BR />,但不工作无论是在一个阵列中显示诸如:

  {天:
[星期天:10.00,星期一:12.00,星期二:09.00,星期三:10.00,星期四:02.00,星期五:05.00,星期六:08.00]
}
 

和我需要拉DAYNAME和放大器;时间所有。

解决方案

  $。的getJSON(URL,
    功能(数据){
      $每个(data.items,功能(I,项目){
      $('#testfield)HTML('&LT; P&GT;+ item.d.title +'&LT; / P&GT;');
      });
    });
 

在此code你要结束了testfield的价值一遍又一遍的ID替换项目的HTML ...你可能要尝试使用的 jQuery.append 添加为你上面

  $。的getJSON(URL,
    功能(数据){
      $每个(data.items,功能(I,项目){
      $('#testfield')追加('&其中p为H.;'+ item.d.title +'&所述; / P&GT;');
      });
    });
 

Got the simplified array working see below

Following up on the complicated array to parse see here.


TLDR: Want to get each heading from an array and insert it into a div without knowing what is inside the div using Jquery - getJSON.

Edit: The data is coming from a piece of software that is outputting the JSON string every couple of seconds with new data, so I need to pull the array of data within "d" as in the example below. So i should get "Title" & "034324324"etc for each.

Edit2 Updated code to match mine exactly..

I have JSON array lets say:

{
"Days":
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
}

And I want to parse it as for each "title" to insert it into a div.

Trying along the lines of

function getdata() {
31 $.ajaxSetup ({ cache: false}); //<!-- Stops IE from caching data //-->
32 
33 $.getJSON(testurl, function(data) {
34 $.each(data.days, function(i,item){
35 $('#testfield').append('<p>' + item + '</p>');
36
37 });
38 }); 

to no avail.

I am using Getjson in other places but in cases where I know what request I am making. e.g:

$('#name').html(data.Projectname);

Which is all working perfectly fine but in this case I need to get the details out of the array without knowing what is inside the array I'm sure there is something simple that I'm missing here or doing wrong ^^.

The JSON I have currently been working with looks like :

{"Projectname":"ertyofhj","Projectid":"aqwertyuqw","Projecttitle":"ertyofhj"}

The array was just a way to look out pulling out the data without knowing what was inside the array.

I just keep adding more. Debugging firebug or IE's debugger(Aargh) it will hit

33 $.getJSON(testurl, function(data) {

then

38 });

completely skipping

34 $.each(data.days, function(i,item){

-The GET request fires fine and returns the correct JSON string. But now it will not fill the $.each, or even if I specifically ask for a certain string. On note of above Firebug is also telling me that it isn't formatted as JSON?

Changed the original JSON string to something simpler.(Days of the week) as is days of the week will load. So I am getting somewhere. But I need to be able to call a more complex array. For example (using the days) each day has a specific time on it that I need to display as well also each day should be on a new line tried adding in a " + '< br />' but that doesn't work either. Displayed in an array such as:

{"Days":
["Sunday":"10.00", "Monday":"12.00", "Tuesday":"09.00", "Wednesday":"10.00", "Thursday":"02.00", "Friday":"05.00", "Saturday":"08.00"]
}

And I need to pull Dayname & time for all.

解决方案

$.getJSON(url,
    function(data){
      $.each(data.items, function(i,item){
      $('#testfield').html('<p>' + item.d.title + '</p>');
      });
    });

In this code you're going to end up replacing the HTML of the item with ID of 'testfield' with the value over and over ... you might want to try using jQuery.append to add all entries as you've specified above

$.getJSON(url,
    function(data){
      $.each(data.items, function(i,item){
      $('#testfield').append('<p>' + item.d.title + '</p>');
      });
    });

这篇关于jQuery的的getJSON分析数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆