[英]How to parse a JSON object ? (From server-side to client-side…javascript)
I have a jquery interacting with a server side Web Method. 我有一个与服务器端Web方法交互的jquery。 The Web method accepts a string 'memID' from the jquery and executes SQL queries based on it.
Web方法从jquery接受字符串'memID'并基于它执行SQL查询。 Then I create a class:-
然后我创建一个类:
public class Member
{
// Objects of Member. //
public string id { get; set; }
public string HPCode { get; set; }
public string OPfromDate { get; set; }
public string OPthruDate { get; set; }
public Member(string ID, List<string> hpCode, List<string> opfromDate, List<string> opthruDate)
{
id = ID;
for (int j = 0; j < hpCode.Count; j++){ HPCode = HPCode + (hpCode)[j] + '*' };
for (int j = 0; j < opfromDate.Count; j++){OPfromDate = OPfromDate + (opfromDate)[j] + '*' };
for (int j = 0; j < opthruDate.Count; j++){OPthruDate = OPthruDate+ (opthruDate)[j] + '*' };
}
}
This class is used to return the results of my SQL query to the client side:- 此类用于将我的SQL查询结果返回给客户端:-
return JsonConvert.SerializeObject(member);
I used breakpoints and checked on the client side, indeed it is getting the return values. 我使用断点并在客户端进行检查,的确是在获取返回值。 However I am not sure about what is the best way to parse these values and store them in variables on my javascript side so i can use them for client-side functionalities.
但是我不确定解析这些值并将它们存储在我的JavaScript端的变量中的最佳方法是什么,因此我可以将它们用于客户端功能。
// Ajax function that sends member ID from client side to server side without a post back.
function ajax()
{
$.ajax
({
url: 'Default.aspx/MyMethod',
type: 'POST',
dataType: "json",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ memID: mem_ID }),
success: onSuccess,
fail : onFail
});
}
// If client-to-server communication is successful.
function onSuccess(data)
{
confirm(data.d);
var returnedstring = (data.d);
var jsondata = $.parseJSON(data.d);
}
Now how do I parse the 'data' ?? 现在我该如何解析“数据”? is there an eval function?
有评估功能吗? or parse function?
或解析功能?
Does anybody have any web examples? 有人有网络示例吗? before this I was only passing back 1 value to my client side, so it was easy to get, now i am confused with multiple values.
在此之前,我只将1值传递回我的客户端,因此很容易获得,现在我对多个值感到困惑。
UPDATE:- 更新:-
I tried doing this on my javascript side:- 我尝试在JavaScript端执行此操作:-
var returnedstring = (data.d);
var member = data.d.id;
var Hp = data.d.HPCode;
however when I use breakpoints and hover over them with my mouse, i get member and HP as undefined, however the returnedstring has all the correct values.. ... any idea? 但是,当我使用断点并用鼠标悬停在它们上时,我得到的成员和HP未定义,但是返回的字符串具有所有正确的值。
SOLUTION (I couldn't figure out the other way suggested in the answers, but this works for me) :- 解决方案(我想不出答案中建议的另一种方法,但这对我有用):-
function onSuccess(data)
{
// stores entire return in one string.
var returnedstring = (data.d);
// parses json returned data
var jsondata = $.parseJSON(data.d);
var member = jsondata.id;
var HpCode = jsondata.HPCode;
}
Because you're using dataType: "json"
, data
is already parsed out of the JSON string. 因为您使用的是
dataType: "json"
,所以已经从JSON字符串中解析出了data
。 That's why you're able to access data.d
in Javascript. 因此,您可以使用Javascript访问
data.d
To access the members, you can do something like this: 要访问成员,您可以执行以下操作:
console.log(data.d.HPCode);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.