在asp.net中序列化JSON ?

时间:2021-10-24 16:36:55

Hi am using JSON for the firsttime in asp.net

在asp.net中,我第一次使用JSON。

I have my WebMethod like this

我有这样的WebMethod

               [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

但是,当我尝试使用Ajax调用,并附加到表时,如果我尝试绑定结果,如果我尝试绑定结果,它会给出未被删除的令牌U。

Finally i found problem is with serialization,

最后我发现问题在于序列化,

my Ajax call is

我的Ajax调用

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

3 个解决方案

#1


0  

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.

注意,我已经从数据更改为结果。

#2


0  

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

我不知道getvessel想要什么,但是试试:

GetVessels(data.d);

i.e. pass the actual data rather than the entire response object.

即传递实际数据而不是整个响应对象。

#3


0  

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

首先,验证GetVessles方法期望的是哪种JSON或数组结构。

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

您可以通过直接提供VoyageVessel_Get方法的JSON响应或者手工构造它来实现。即。

 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.

看看你的get容器是否适用于这个对象。如果它没有找到它需要的结构(只有你能做到),然后构造它。

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

第二,您不能直接访问:ASP中的success。净web服务。

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 i.e.

如果您的对象包含datetime字段,请尝试在序列化中指定DateFormathandling。

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

#1


0  

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.

注意,我已经从数据更改为结果。

#2


0  

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

我不知道getvessel想要什么,但是试试:

GetVessels(data.d);

i.e. pass the actual data rather than the entire response object.

即传递实际数据而不是整个响应对象。

#3


0  

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

首先,验证GetVessles方法期望的是哪种JSON或数组结构。

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

您可以通过直接提供VoyageVessel_Get方法的JSON响应或者手工构造它来实现。即。

 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.

看看你的get容器是否适用于这个对象。如果它没有找到它需要的结构(只有你能做到),然后构造它。

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

第二,您不能直接访问:ASP中的success。净web服务。

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 i.e.

如果您的对象包含datetime字段,请尝试在序列化中指定DateFormathandling。

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