简体   繁体   中英

How to parse a JSON object ? (From server-side to client-side…javascript)

I have a jquery interacting with a server side Web Method. The Web method accepts a string 'memID' from the jquery and executes SQL queries based on it. 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:-

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.

// 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.

UPDATE:-

I tried doing this on my javascript side:-

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?

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. That's why you're able to access data.d in Javascript.

To access the members, you can do something like this:

console.log(data.d.HPCode);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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