在ASP的服务器端使用JSON。净和c#

时间:2021-10-20 16:14:40

This is a follow-up to my question here. I'm trying to understand how to serialize some JSON. I am using the JSON.stringify() method found in json2.js to convert a JSON array into a string value I can work with on the server-side. My JSON originally looks like this:

这是我的问题的后续。我正在尝试理解如何序列化一些JSON。我正在使用json2. stringify()方法。将JSON数组转换为可以在服务器端处理的字符串值。我的JSON最初是这样的:

var myItems = {
    "data": [
      {"id":1, "type":2, "name":"book"},
      {"id":2, "type":3, "name":"dvd"},
      {"id":3, "type":4, "name":"cd"}
    ]
};

After I use JSON.stringify, I have noticed that the value on the server looks like the following:

在我使用JSON。stringify,我注意到服务器上的值如下所示:

{"data":[{"id":1,"type":2,"name":"book"},{"id":2,"type":"3","name":"dvd"},{"id":3,"type":4,"name":"cd"}]}

In an effort to serialize this JSON into C# objects I can work with, I have written the following code:

为了将JSON序列化为c#对象,我编写了以下代码:

public MyItems GetMyItems() 
{
  MyItems items = new MyItems();

  string json = serializedJsonInHiddenHtmlElement.Value;
  if (json.Length > 0)
  {
    items = Deserialize<MyItems>(json);
  }
  return items;
}

public static T Deserialize<T>(string json)
{
  using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
  {
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
    return (T)serializer.ReadObject(ms);
  }
}

The classes associated with my types are defined as follows:

与我的类型相关的类定义如下:

[DataContract(Name="myItems")]
internal class MyItems
{
    [DataMember(Name = "data")]
    public string[] Data { get; set; }
}

[DataContract(Name="myItem")]
internal class MyItem
{
    [DataMember(Name = "id")]
    public string ID { get; set; }

    [DataMember(Name = "type")]
    public string Type { get; set; }

    [DataMember(Name = "name")]
    public string Name { get; set; }    
}

When I execute this code, the line that says return (T)serializer.ReadObject(ms); I get an error that says the following:

执行此代码时,表示return (T)serializer.ReadObject(ms)的行;我得到的错误是:

There was an error deserializing the object of type AppNamespace.MyItems. End element 'item' from namespace '' expected. Found element 'id' from namespace ''.

What am I doing wrong? I can't seem to get past this. Can somebody please point me in the right direction? Thank you!

我做错了什么?我似乎过不了这个关。谁能给我指出正确的方向吗?谢谢你!

2 个解决方案

#1


1  

What is "Deserialize(" in your code.. from which namespace are you using the deserialize method ?

什么是“反序列化”(在您的代码中)。使用反序列化方法的名称空间是什么?

If this is an asp.net project, if asp.net 3.5+, you should be able to use microsoft ajax extensions which has json deserialization inbuilt.

如果这是一个asp.net项目,如果是asp.net 3.5+,您应该能够使用内置json反序列化的microsoft ajax扩展。

#2


1  

You could first try to create a MyItems object on the server and serialize it. To see how the output json looks like. I haven't used DataContractJsonSerializer but I think that the wcf attributes make it interpret the json incorrectly.

您可以首先尝试在服务器上创建MyItems对象并对其进行序列化。看看输出json是什么样子。我没有使用DataContractJsonSerializer,但我认为wcf属性使它无法正确地解释json。

Also, shouldn't :

此外,不应该:

public string[] Data { get; set; }

be

public MyItem[] Data { get; set; }

?

吗?

#1


1  

What is "Deserialize(" in your code.. from which namespace are you using the deserialize method ?

什么是“反序列化”(在您的代码中)。使用反序列化方法的名称空间是什么?

If this is an asp.net project, if asp.net 3.5+, you should be able to use microsoft ajax extensions which has json deserialization inbuilt.

如果这是一个asp.net项目,如果是asp.net 3.5+,您应该能够使用内置json反序列化的microsoft ajax扩展。

#2


1  

You could first try to create a MyItems object on the server and serialize it. To see how the output json looks like. I haven't used DataContractJsonSerializer but I think that the wcf attributes make it interpret the json incorrectly.

您可以首先尝试在服务器上创建MyItems对象并对其进行序列化。看看输出json是什么样子。我没有使用DataContractJsonSerializer,但我认为wcf属性使它无法正确地解释json。

Also, shouldn't :

此外,不应该:

public string[] Data { get; set; }

be

public MyItem[] Data { get; set; }

?

吗?

相关文章