在jquery中访问json值时出现问题

时间:2020-11-02 18:44:47

标签: jquery json

这是我的JSON字符串:

{"success":true,"docSearch":
 [
  {"fName":"Sam",
   "lName":"Mehra",
   "email":"samanyumehra20000@gmail.com",
   "mobile":"7859857230",
   "userImage":"IMG/patient.png",
   "address":"412/A Ganesh Nagar 2 gali no.1 Laxmi Nagar Near Ajay cycles, Mona Pg",
   "special":"Allergist, Dentist, Anesthesiologist, Cardiologist",
   "deg":"MBBS, BDS, BAMS, BUMS","experience":"3","fees":"3"},
   {"fName":"Samanyu",
    "lName":"Mehra",
    "email":"samanyumehra20000@gmail.com",
    "mobile":"7859857230",
    "userImage":"IMG/regis.jpg",
    "address":"412/A Ganesh Nagar 2 gali no.1 Laxmi Nagar Near Ajay cycles, Mona Pg",
    "special":"Allergist, Dentist, Anesthesiologist, Cardiologist, Dermatologist",
    "deg":"MBBS, BDS, BAMS, BUMS, BHMS, BYNS",
    "experience":"3",
    "fees":"3"
    }
  ]
}

我想访问jQuery中的值。到目前为止,我做到了:

success: function(data,textStatus,jqXHR){
            if(data.success){
                var img=document.createElement('img');
                $(data).each(function(index,item){
                $("#ajaxResponse").html("");
                $("#ajaxResponse").append("<b>First Name:</b> " + data.docSearch[index].fName+"<br>");
                $("#ajaxResponse").append("<b>Last Name:</b> " + data.docSearch[index].lName+"<br>");
                $("#ajaxResponse").append("<b>Email:</b> " + data.docSearch[index].email+"<br>");
                $("#ajaxResponse").append("<b>Experience:</b> " + data.docSearch[index].experience+"<br>");
                $("#ajaxResponse").append("<b>Fees:</b> " + data.docSearch[index].fees+"<br>");
                img.src=data.docSearch[index].userImage;
                $("#ajaxResponse").append(img);
                });
            } 

如果阅读本文的任何人都知道如何访问此类JSON字符串,请帮帮我。 注意:此JSON字符串也可以包含1个值,例如:

    {"success":true,"docSearch":
 [
  {"fName":"Sam",
   "lName":"Mehra",
   "email":"samanyumehra20000@gmail.com",
   "mobile":"7859857230",
   "userImage":"IMG/patient.png",
   "address":"412/A Ganesh Nagar 2 gali no.1 Laxmi Nagar Near Ajay cycles, Mona Pg",
   "special":"Allergist, Dentist, Anesthesiologist, Cardiologist",
   "deg":"MBBS, BDS, BAMS, BUMS","experience":"3","fees":"3"}
]
    }

编辑1:

    success: function(data,textStatus,jqXHR){
        //doc Name was correct so we have some information to display   
        if(data.success){
            var img=document.createElement('img');
            var br=document.createElement('br');
            $("#ajaxResponse").html("");
            $(data.docSearch).each(function(index,item){
            $("#ajaxResponse").append("<b>First Name:</b> " + item.fName+"<br>");
            $("#ajaxResponse").append("<b>Last Name:</b> " + item.lName+"<br>");
            $("#ajaxResponse").append("<b>Email:</b> " + item.email+"<br>");
            $("#ajaxResponse").append("<b>Usernames:</b> " + item.username+"<br>");
            $("#ajaxResponse").append("<b>Experience:</b> " + item.experience+"<br>");
            $("#ajaxResponse").append("<b>Fees:</b> " + item.fees+"<br>");
            img.src=item.userImage;
            $("#ajaxResponse").append(img);
            $("#ajaxResponse").append(br);
            });
        } 
             //display error message
             else {
                 $("#ajaxResponse").html("<div><b>No Record Found!!</b></div>");
             }
        },

我添加了<'br'>标记,以便在要显示的两个内容之间都有一个换行符,但是没有换行符,并且也没有显示2张图像。对于第一次迭代,它只读取它,然后像这样保留它;对于第二次迭代,它显示图像。我要两个数据都显示图像。请帮助!!!

编辑2: 我的问题得到解决,方法是将var img=document.createElement('img');保留在循环中,这样无论何时为图像设置路径,它都将显示图像。

1 个答案:

答案 0 :(得分:2)

您使用了foreach循环来设置错误的变量。将data.docSearch的foreach循环用于data的。

var data = {"success":true,"docSearch":
 [
  {"fName":"Sam",
   "lName":"Mehra",
   "email":"samanyumehra20000@gmail.com",
   "mobile":"7859857230",
   "userImage":"IMG/patient.png",
   "address":"412/A Ganesh Nagar 2 gali no.1 Laxmi Nagar Near Ajay cycles, Mona Pg",
   "special":"Allergist, Dentist, Anesthesiologist, Cardiologist",
   "deg":"MBBS, BDS, BAMS, BUMS","experience":"3","fees":"3"},
   {"fName":"Samanyu",
    "lName":"Mehra",
    "email":"samanyumehra20000@gmail.com",
    "mobile":"7859857230",
    "userImage":"IMG/regis.jpg",
    "address":"412/A Ganesh Nagar 2 gali no.1 Laxmi Nagar Near Ajay cycles, Mona Pg",
    "special":"Allergist, Dentist, Anesthesiologist, Cardiologist, Dermatologist",
    "deg":"MBBS, BDS, BAMS, BUMS, BHMS, BYNS",
    "experience":"3",
    "fees":"3"
    },{"fName":"Sandeep",
   "lName":"Mehra",
   "email":"samanyumehra20000@gmail.com",
   "mobile":"7859857230",
   "userImage":"IMG/patient.png",
   "address":"412/A Ganesh Nagar 2 gali no.1 Laxmi Nagar Near Ajay cycles, Mona Pg",
   "special":"Allergist, Dentist, Anesthesiologist, Cardiologist",
   "deg":"MBBS, BDS, BAMS, BUMS","experience":"3","fees":"3"},
   {"fName":"Samanyu",
    "lName":"Mehra",
    "email":"samanyumehra20000@gmail.com",
    "mobile":"7859857230",
    "userImage":"IMG/regis.jpg",
    "address":"412/A Ganesh Nagar 2 gali no.1 Laxmi Nagar Near Ajay cycles, Mona Pg",
    "special":"Allergist, Dentist, Anesthesiologist, Cardiologist, Dermatologist",
    "deg":"MBBS, BDS, BAMS, BUMS, BHMS, BYNS",
    "experience":"3",
    "fees":"3"
    }
  ]
}


$(data.docSearch).each(function(index,item){
                console.log(item.fName);
                });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>