我如何从json响应中读取特定数据字段

时间:2015-08-03 09:07:01

标签: jquery ajax json asp.net-mvc-5 jquery-ajaxq

我有$ .ajax函数的json响应,我也可以提醒整个响应,但是如何在“recordFound”函数下响应特定字段,例如First Name。

JSON响应

{"StudentID":12,"StudentNumber_UWLID":21209510,"Title":"mr","FirstName":"Adam","MiddleName":null,"LastName":"Test"}

Ajax功能

 $.ajax({
            url: '@Url.Action("GetStudentRecordByID", "StudentProfile")',
            type: "POST",
            dataType: "JSON",
            data: { _GivenStudentUWLID: StudentUWLID },
            cache: false
        }).done(function (data, textStatus, jqXHR) {

            if (data.RecordStatus == "NotAvailable")
            {
                $(this).MyMessageDialog({
                    _messageBlockID: "_StatusMessage",
                    _messageContent: "<div class='warningMessage'> <h4>Given Student Cannot Be Enter As Additional Tenant.</h4> <br/> Student Need to Have their Profile Completed On Student Village Portal Before Can Be Added As Additional Tenant Within The Tendency Form! <br/><br/> Or Enter Correct Student UWL ID "+"</div>",
                    _messageBlockWidth: "400px"
                });
            }
            else if(data.RecordStatus=="recordFound")
            {
                alert(data.Response);???????

                 $("#AdditionalTenent").find(".listedStudentTitle").val("dddadd");
            }

        }).fail(function (jqXHR, textStatus, errorThrown) {

            alert("error");
            });

        });

    });

ASP.NET-MVC控制器发送JSON

 if (_entity != null)
            {
                var studentObj = new StudentLimitedInfo
                {
                    StudentNumber_UWLID = _entity.StudentNumber_UWLID,
                    StudentID = _entity.StudentID,
                    Title = _entity.Title,
                    FirstName = _entity.FirstName,
                    MiddleName = _entity.MiddleName,
                    LastName = _entity.LastName
                };

                var jsonObj = new JavaScriptSerializer().Serialize(studentObj);

                return Json(new { Response = jsonObj, RecordStatus = "recordFound" }, JsonRequestBehavior.AllowGet);
            }

1 个答案:

答案 0 :(得分:0)

我发现答案如下;

var paraed = JSON.parse(data.Response);

alert(paraed.Title + "   "+paraed.FirstName);