问题:
我从JSON创建了一个类,我从外部公司提供了一个用于向其系统发送订单的公司
课程的一部分:
Public Class SalesOrder
Public Class Field
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property FieldType() As String
Get
Return m_FieldType
End Get
Set(value As String)
m_FieldType = value
End Set
End Property
Private m_FieldType As String
End Class
End Class
案例:
假设我通过使用带有JSON的txt文件并从那里解析类来创建SalesOrder类的新实例,现在我想访问Field类中的Name属性,以便我可以编辑它。如何从SalesOrder类的新实例执行此操作?每当我尝试访问它时,它会告诉我它是一种类型,不能用作表达式
示例:
Dim sOrder As New SalesOrder
sOrder = JsonConvert.DeserializeObject(Of SalesOrder)(File.ReadAllText("C:\Users\Something\Documents\Visual Studio 2010\Projects\SomethingElse\WebApplication1\OrderDefinitionJSON.txt"))
Dim FieldName As String = SOrder.Field.Name
这将导致sOrder.Field
发生异常修改
JSON
这是我试图读出的JSON的一部分:
{
"BookDate": "2016-12-18T10:39:05.276Z",
"BusinessObject": "SO1",
"Table": {
"Definition": {
"Name": "SO_HDR",
"Fields": [{
"Name": "RowId",
"FieldType": "C"
}]
}
}
}
谢谢!
答案 0 :(得分:1)
您的SalesOrder类不包含Field类的等级。如果要设置它,则必须向SalesOrder类添加属性:
Public Class SalesOrder
Public SalesField as Field
End Class
Public Class Field
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property FieldType() As String
Get
Return m_FieldType
End Get
Set(value As String)
m_FieldType = value
End Set
End Property
Private m_FieldType As String
End Class
现在,如果您正确反序列化并且SalesField不是什么,您可以:
Dim sOrder As New SalesOrder
sOrder = JsonConvert.DeserializeObject(Of SalesOrder)(File.ReadAllText("C:\Users\Something\Documents\Visual Studio 2010\Projects\SomethingElse\WebApplication1\OrderDefinitionJSON.txt"))
Dim FieldName As String = SOrder.Field.Name