简体   繁体   English

在VB.Net WebMethod中反序列化JSON无法正常工作

[英]Deserialize JSON in VB.Net WebMethod isn't working

I'm trying to deserialize a JSON object when I call a WebMethod. 我在调用WebMethod时试图反序列化JSON对象。 The deserialization successfully creates the array of object, but the values are "Nothing". 反序列化成功创建了对象数组,但是值是“ Nothing”。 What am I doing wrong? 我究竟做错了什么?

My Object class: 我的对象类:

public class Data
{
    public Attribute[] Attributes{ get; set; }
}

public class Attribute
{
    public string Category { get; set; }
    public string Value { get; set; }

}

Here is my WebMethod: 这是我的WebMethod:

<System.Web.Services.WebMethod()>
Public Shared Function A_Method(ByVal Attributes As Data)
    'do things
End Function

And here is my JSON object that is being passed to the WebMethod: 这是传递给WebMethod的JSON对象:

{"Attributes":[
   {
      "Attribute":{
         "Category":"Category1",
         "Value":"Value1"
      }
   },
   {
      "Attribute":{
         "Category":"Category2",
         "Value":"Value2"
      }
   },
   {
      "Attribute":{
         "Category":"Category3",
         "Value":"Value3"
      }
   },
   {
      "Attribute":{
         "Category":"Category4",
         "Value":"Value4"
      }
   },
   {
      "Attribute":{
         "Category":"Category5",
         "Value":"Value5"
      }
   },
   {
      "Attribute":{
         "Category":"Category6",
         "Value":"Value6"
      }
   },
   {
      "Attribute":{
         "Category":"Category7",
         "Value":"Value7"
      }
   }
]
}

My problem is that I get the array of 7 Attributes with the Category and Value labels, but the values are "Nothing". 我的问题是,我得到了包含“类别”和“值”标签的7个属性的数组,但是值是“ Nothing”。 What am I doing wrong? 我究竟做错了什么?

Your object model does not match the shown JSON which actually maps to the following 您的对象模型与实际映射到以下内容的显示的JSON不匹配

public class Data {        
    public AttributeElement[] Attributes { get; set; }
}

public class AttributeElement {        
    public Attribute Attribute { get; set; }
}

public class Attribute {        
    public string Category { get; set; }        
    public string Value { get; set; }
}

Note how the elements in the array have an Attribute property. 请注意数组中的元素如何具有Attribute属性。

<System.Web.Services.WebMethod()>
Public Shared Function A_Method(ByVal data As Data)
    'do things
    Dim someAttribute = data.Attributes(0).Attribute
    Dim category = someAttribute.Category
    Dim value = someAttribute.Value
    '...
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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