简体   繁体   中英

Serializing to JSON in asp.net?

Hi am using JSON for the firsttime in asp.net

I have my WebMethod like this

               [WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string Vo_Get(string Action,int ID)
{
    Product p= new Product();

    DataSet ds= new DataSet();

    p.Action = Action;
    p.ID= ID;

    ds= VoGet_Get(obj);

    **string jsonVesselDetails = JsonConvert.SerializeObject(ds, Formatting.None);
    return jsonVesselDetails;**
}

here iam getting my result as

       [
  {
    "Pinky": 1,
    "Ponky": "Breakwater Dockk",

  },
  {
    "Pinky": 2,
    "Ponky": "Watson Island Dock",    
  },

But when i tried to call using Ajax and append to table its giving Unexpexted Token U if i try to bind with result and its giving Unexpected token O if i try to bind with result.data

Finally i found problem is with serialization,

my Ajax call is

                  $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Voyage.aspx/Vo_Get",
                data: "{'Action':'Get','ID':'68'}",
                dataType: "json",
                success: function (data) {
                    try {
                        alert("getdata! success" );

                        Get(data);

                    } catch (ex) {
                        alert(ex);
                    }
                },
                error: function (msg) {
                    alert("err: " + error);
                }
            });

and

I don't know what GetVessels is expecting but try:

GetVessels(data.d);

ie pass the actual data rather than the entire response object.

May be you are using the same variable data in success function as well. Try using the following,

success: function (result) {
   try {
        alert("getdata! success" );

        GetVessels(result);

        //Rebuild the table
        //BuildVesselTaggerInfo();

   } catch (ex) {
        alert(ex);
   }
},

note that I have changed from data to result.

First of all verify what kind of JSON or array structure your GetVessles method expects.

you can do it by directly feeding the JSON response of VoyageVessel_Get method or construct it manually. ie

 var data= [
 {
      "TerminalID": 1,
      "TerminalName": "Breakwater Dockk",
      "PortID": 1,
      "PortName": "Swire Pacific Offshore",
      "Column1": "08/03/13",
      "Column2": "16/03/13",
      "ServiceID": 2
  },
  {
      "TerminalID": 2,
      "TerminalName": "Watson Island Dock",
      "PortID": 2,
      "PortName": "Keppel Offshore",
      "Column1": "20/03/13",
      "Column2": "23/03/13",
      "ServiceID": 2
  }];

  GetVessels(data);

see if your GetVessels works for this object. if it doesn't then find out what structure it needs(only you can do that), and construct that.

Second, you cannot access directly the response of :success in ASP.NET Web-Service .

access it like this:

success: function (data) {
           var jsonData= data.d;
           GetVessels(jsonData);
 }

update

if your object consist of datetime field try specifying DateFormathandling in the Serialization ie

JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
            {
                DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
            };
string serializedObject= Newtonsoft.Json
                     .JsonConvert
                     .SerializeObject(dsVoyages, microsoftDateFormatSettings);

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