如何访问字符串数据的字段 [英] How to access to fields of an string of data
问题描述
你好
我的控制器中有功能,我以字符串格式从数据库中获取数据。结果是:
[{NationalCode:12458,LicenceCode:4582aS,电话:45236987}] >
我把它传递给我的JavaScript文件。如何访问此字符串的字段。
我的意思是访问NationalCode或LicenceCode或Phone值?
提前致谢
我的尝试:
1)
Hello
I have function in my Controller and I have fetch data from data base in string format. the result is :
[{"NationalCode":"12458","LicenceCode":"4582aS","Phone":"45236987"}]
that i pass it to my JavaScript file. How can access to the fields of this string.
I mean access to "NationalCode" or "LicenceCode" or "Phone" values ?
Thanks in Advance
What I have tried:
1)
var source = JSON.parse(response);
alert(source.NationalCode); // returns undefined
2)
2)
var source = JSON.parse(response);
alert(source["NationalCode"]); // returns undefined
3)
3)
alert(response); // returns the whole string
4)
4)
var source = JSON.parse(response);
alert(source); // returns [object object]
响应是从控制器函数返回的结果
response is the result that has returned from controller function
推荐答案
你好,
Re:[{NationalCode:12458,LicenceCode:4582aS,电话:45236987}]
您可以使用以下方式直接访问它:
查看:
成功:功能(回复){
var groupVal = response.NationalCode;
}
让我解释一下:
控制器:
Hello,
Re: [{"NationalCode":"12458","LicenceCode":"4582aS","Phone":"45236987"}]
You can direct access it using:
View:
success: function (response) {
var groupVal = response.NationalCode;
}
Let me explain you:
Controller:
public JsonResult GetAdminSettingByID(int settingID)
{
var adminSettingDetails = bjClsAdminService.GetAdminSettingByID(settingID);
return Json(adminSettingDetails, JsonRequestBehavior.AllowGet);
}
使用模型类创建json并直接在这里传递类对象。
Using model class for create json and pass the class object directly here.
这篇关于如何访问字符串数据的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!