getjson jquery 解析数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2727546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:09:54  来源:igfitidea点击:

getjson jquery parsing an array

jqueryajaxjsonarraysgetjson

提问by Sphvn

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.

TLDR:想要从数组中获取每个标题并将其插入到 div 中,而无需使用 Jquery - getJSON 知道 div 中的内容。

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.

编辑:数据来自一个软件,该软件每隔几秒输出一次带有新数据的 JSON 字符串,因此我需要在“d”中提取数据数组,如下例所示。所以我应该为每个人获得“标题”和“034324324”等。

Edit2Updated code to match mine exactly..

Edit2更新代码以完全匹配我的..

I have JSON array lets say:

我有 JSON 数组可以说:

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

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

我想将其解析为每个“标题”以将其插入到 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:

我在其他地方使用 Getjson 但在我知道我在做什么请求的情况下。例如:

$('#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 :

我目前使用的 JSON 看起来像:

{"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

我只是不断添加更多。调试萤火虫或 IE 的调试器 (Aargh) 它将命中

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?

- GET 请求正常触发并返回正确的 JSON 字符串。但是现在它不会填充 $.each,或者即使我特别要求某个字符串。 注意到上面的 Firebug 还告诉我它没有格式化为 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:

将原始 JSON 字符串更改为更简单的内容。(一周中的天数)因为一周中的天数将加载。所以我正在到达某个地方。但我需要能够调用更复杂的数组。例如(使用天数)每一天都有一个特定的时间,我也需要显示,每一天都应该在一个新行上尝试添加“ + '< br />' 但这也不起作用. 显示在一个数组中,例如:

{
  "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.

我需要为所有人拉取 Dayname & time。

采纳答案by Dave G

$.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.appendto add all entries as you've specified above

在此代码中,您最终将使用 ID 为“testfield”的项目的 HTML 一遍又一遍地替换为值……您可能想尝试使用jQuery.append添加您在上面指定的所有条目

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

回答by Dave G

Hopefully this example will point you in the right direction

希望这个例子能为你指明正确的方向



<html>
<head>
  <title>Stackoverflow</title>
  // ** POINT THIS TO A VALID jquery.js LOCATION
  <script src="jquery.js" type="text/javascript"></script>
  <script>
    var days = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ];
    // *****************************************************
    // This is your data from getJSON if correctly formatted
    var data = '{"Days":[ {"Sunday":"10.00"}, {"Monday":"12.00"}, {"Tuesday":"09.00"}, {"Wednesday":"10.00"}, {"Thursday":"02.00"}, {"Friday":"05.00"}, {"Saturday":"08.00"}]}';
    runUpdate = function() {
      // *******************************************************
      // Clear the div of all data by replacing it
      $('#testfield').replaceWith('<div id="testfield"></div>');
      var dataObject= $.parseJSON( data );
      $.each( dataObject.Days, function (index, value) {
        // ********************************************
        // Append new elements to the newly created div
        $('#testfield').append("<p>"+days[index]+": "+value[ days[index] ]+"</p>");
      });
    };

    runReset = function() {
      $('#testfield').replaceWith('<div id="testfield">This is original text.</div>');
    };
  </script>
</head>
<body>
  <div id="testfield">
    This is original text.
  </div>
  <div>
    <input type="button" value="Run Update" onclick="runUpdate();" />
    <input type="button" value="Reset" onclick="runReset();" />
  </div>
</body>
</html>

回答by GlenCrawford

There is an error in your JSON data: you're not using colons after "d" in the last three items. Is this an example of real data that your application is using, or example data to demonstrate your problem?

您的 JSON 数据中存在错误:您没有在最后三项中的“d”后使用冒号。这是您的应用程序正在使用的真实数据示例,还是用于演示您的问题的示例数据?