简体   繁体   English

使用。$ each访问Json中的嵌套数组

[英]Accessing Nested arrays in Json using .$each

So I'm having a problem accessing my json when its in a nested array. 所以当我在嵌套数组中访问json时,我遇到了问题。 I previously had json set up like this with just one array and my .$each function worked perfectly. 我之前只有一个数组和我的。$每个函数完美地工作了json设置。 However I'm having trouble modifying it for this. 但是我在修改它时遇到了麻烦。 Json: JSON:

 {
 "tcontent": [{

  "Name": "Septicaemia",
  "url":"<a>",
  "image":"<div class='grid' style='background-image:url(img/anatomy/septicaemia.jpg);'>",
  "Variations": [{ 
      "condition":"Community-acquired",
      "organisms":"Staph. aureus",
      "antimicrobial":"Flucloxacillin ",
      "alternative":"(non anaphylaxis): ",
      "comments": "Perform full septic screen."

      }, {

      "Condition":"Community-acquired if intra- abdominal source suspected",
      "Organisms":"Predominantly Gram negatives and anaerobes Enterococci may also feature",
      "Antimicrobial":"Co-amoxiclav 1.2g iv tds",
      "Comments":"Perform full septic screen"

      }, {

      "Condition":"Healthcare-associated",
      "Organisms":"Varies",
      "Antimicrobial":"Piperacillin",
      "Alternative":"Seek advice from Consultant Microbiologist",
      "Comments":"Always"
     }]

   }, {

  "Name": "Infective Endocarditis (IE) (pending blood culture results)",
  "url":"<a>",
  "image":"<div class='grid' style='background-image:url(img/anatomy/endocarditis.jpg);'>"

  }, {

  "Name": "Central Nervous System Infections",
  "url":"<a>",
  "image":"<div class='grid' style='background-image:url(img/anatomy/cns.jpg);'>"

  }, {

  "Name": "Skin and Soft Tissue Infections",
  "url": "<a>",
  "image":"<div class='grid' style='background-image:url(img/anatomy/skin.jpg);'>"

  }, {

  "Name": "Diabetic patients with foot infections",
  "url": "<a>",
  "image":"<div class='grid' style='background-image:url(img/anatomy/foot.jpg);'>"

  }, {

  "Name": "Bone and Joint Infections",
  "url": "<a>",
  "image":"<<div class='grid' style='background-image:url(img/anatomy/bone.jpg);'>"

  }, {

  "Name": "Intravascular Line Infections",
  "url": "<a>",
  "image":"<div class='grid' style='background-image:url(img/anatomy/intravascular.jpg);'>"

  }, {

  "Name": "Urinary Tract Infections",
  "url": "<a>",
  "image":"<div class='grid' style='background-image:url(img/anatomy/urinary.jpg);'>"

  }, {

  "Name": "Respiratory Tract Infections",
  "url": "<a>",
  "image":"<div class='grid' style='background-image:url(img/anatomy/respiratory.jpg);'>"

  }, {

  "Name": "Gastrointestinal Infections",
   "url": "<a>",
   "image":"<div class='grid'        style='backgroundimage:url(img/anatomy/gastrointestinal.jpg);'>"

    }]

   }

Here's my javascript to access it. 这是我的javascript访问它。

$(function (){
var imp = "Json/therapy.json"
 $.getJSON(imp, function(data) {
   var info = "<br>";
   $.each(data.tcontent, function(i, item) {
  if(item.Name=='Septicaemia'){
     var search = item.Variations;
     $.each(item.Variations, function(j, subitem) {
      info += subitem.condition + subitem.organisms + subitem.antimicrobial + subitem.alternative + subitem.comments
       });
 $(info).appendTo(".menu");
 //alert(item)
   };
  });
 });
}); 

I've tried a many variations on the var search but nothing seems to be working. 我已经尝试了var搜索的许多变化,但似乎没有任何工作。 I researched a lot of similar problems to this and I've been stuck on this for too long. 我研究了很多类似的问题,并且我已经坚持了很久。 Any light that can be shed on the situation would be much appreciated! 任何可以解决这种情况的灯都会非常感激!

2 reasons why it doesn't work. 2个原因导致它不起作用。

First of all javascript is case sensitive, Your variations differ. 首先,javascript区分大小写,您的版本不同。

subitem.condition fails on : subitem.condition失败:

  "Condition":"Community-acquired if intra- abdominal source suspected",
  "Organisms":"Predominantly Gram negatives and anaerobes Enterococci may also feature",
  "Antimicrobial":"Co-amoxiclav 1.2g iv tds",
  "Comments":"Perform full septic screen"

So change "Condition" to "condition", etc.etc. 所以将“条件”改为“条件”等等。

second reason is the 第二个原因是

Change $(info).appendTo(".menu"); 更改$(info).appendTo(".menu"); to $(".menu").append(info) ; to $(".menu").append(info) ;

Why? 为什么?

$(".menu").append(info) Will just paste the string in the selected DOM element. $(".menu").append(info)将字符串粘贴到选定的DOM元素中。

But you use 但你用

$(info)... and jquery does all kinds of fancy stuff now. $(info)...和jquery现在做各种奇特的东西。

It tries to either use it as DOM selector, or create a new element. 它尝试将其用作DOM选择器,或者创建一个新元素。

Because your info starts with <br> $(info) tries to create a DOM element and it removes all text. 因为您的信息以<br> $(info)开头,尝试创建DOM元素并删除所有文本。 Leaving just <br> because br cannot contain content. 只留下<br> ,因为BR不能包含的内容。

Try to remove the initial <br> then you will see following error: 尝试删除初始<br>然后您将看到以下错误:

Uncaught Error: Syntax error, unrecognized expression:Community-acquiredStaph. aureusFlucloxacillin...

For example if you would type $("hahaha") , Jquery will try to find the tag <hahaha> , So when you remove the <br> your $(info) is looking for the tag <Community-acquiredStaph. aureusFlucloxacillin...> 例如,如果你输入$("hahaha") ,Jquery将尝试找到标签<hahaha> ,所以当你删除<br>你的$(info)正在寻找标签<Community-acquiredStaph. aureusFlucloxacillin...> <Community-acquiredStaph. aureusFlucloxacillin...> . <Community-acquiredStaph. aureusFlucloxacillin...>

But because your string would then contain weird characters like "-()." 但是因为你的字符串会包含奇怪的字符,如“ - ()”。 It will fail. 它会失败。 Hence the above error. 因此上述错误。

So you can only add html like this: 所以你只能添加这样的html:

$("<span>hahah</span>").appendTo($(".menu"));

Or use selector 或者使用选择器

$("#myDiv").appendTo($(".menu"));

An example when $(info).appendTo(".menu"); $(info).appendTo(".menu");一个例子$(info).appendTo(".menu"); working is: 工作是:

$.each(data.tcontent, function(i, item) {
    if(item.Name=='Septicaemia'){
        var search = item.Variations;
        $.each(item.Variations, function(j, subitem) {
            var info = "<p>" + subitem.condition + subitem.organisms + subitem.antimicrobial + subitem.alternative + subitem.comments + "</p>";
            $(info).appendTo(".menu");
        });

    }
});

Using the following json: 使用以下json:

http://pastebin.com/Bzpix1ai http://pastebin.com/Bzpix1ai

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

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