我有一个JSON字符串。有一个这样的部分
"Result": {
"AdditionalInfo": {
"SubCategoryID": "978",
"SellerPartNumber": "04-VO7T-14PP",
"ManufacturerPartNumberOrISBN": "04-VO7T-14PP",
"UPC": null
},
"ErrorList": {
"ErrorDescription": [
{
"#cdata-section": "Error(s). Item not created."
},
{
"#cdata-section": "Error:[Item does not exist. Please create a new list.]"
}
]
}
}
但是,有时该部分也是一个数组。 像
"Result": [
{
"AdditionalInfo": {
"SubCategoryID": "1512",
"SellerPartNumber": "TY-SZT1-358V",
"ManufacturerPartNumberOrISBN": "TY-SZT1-358V",
"UPC": null
},
"ErrorList": {
"ErrorDescription": [
{
"cdata-section": "Error(s). Item not created."
},
{
"cdata-section": "CufflinkMaterial - Property: [CufflinkMaterial] value error. The submitted value: [MPS.Domain.SubCategoryProperty] is not recognized for this property."
},
{
"cdata-section": "CufflinkType - Property: [CufflinkType] value error. The submitted value: [MPS.Domain.SubCategoryProperty] is not recognized for this property."
}
]
}
},
{
"AdditionalInfo": {
"SubCategoryID": "1512",
"SellerPartNumber": "UF-T05C-T6XG",
"ManufacturerPartNumberOrISBN": "UF-T05C-T6XG",
"UPC": null
},
"ErrorList": {
"ErrorDescription": [
{
"cdata-section": "Error(s). Item not created."
},
{
"cdata-section": "CufflinkMaterial - Property: [CufflinkMaterial] value error. The submitted value: [MPS.Domain.SubCategoryProperty] is not recognized for this property."
},
{
"cdata-section": "CufflinkType - Property: [CufflinkType] value error. The submitted value: [MPS.Domain.SubCategoryProperty] is not recognized for this property."
}
]
}
},
{
"AdditionalInfo": {
"SubCategoryID": "1512",
"SellerPartNumber": "5B-1137-WT3O",
"ManufacturerPartNumberOrISBN": "5B-1137-WT3O",
"UPC": null
},
"ErrorList": {
"ErrorDescription": [
{
"cdata-section": "Error(s). Item not created."
},
{
"cdata-section": "CufflinkMaterial - Property: [CufflinkMaterial] value error. The submitted value: [MPS.Domain.SubCategoryProperty] is not recognized for this property."
},
{
"cdata-section": "CufflinkType - Property: [CufflinkType] value error. The submitted value: [MPS.Domain.SubCategoryProperty] is not recognized for this property."
}
]
}
}
]
是否有一种通用方法可以在不定义不同对象的情况下对两个JSON进行Deserlize?所以,我定义一个Result对象数组,当有单个实体时,它会在对象数组中创建一个索引,当有多个时,它可能会在数组中创建多个索引。
最终目标是使用对象对象并使用它来解析单个Result对象或多个。
这可能吗? 谢谢 Sameers
答案 0 :(得分:0)
感谢Brian Rogers
答案在你的链接中。
How to handle both a single item and an array for the same property using JSON.net
我使用此代码将其转换为VB.NET。
Class SingleOrArrayConverter(Of T)
Inherits Newtonsoft.Json.JsonConverter
Public Overrides Function CanConvert(ByVal objectType As Type) As Boolean
'Return (objectType = GetType(List(Of T)))
Return objectType.Equals(GetType(Generic.List(Of T)))
End Function
Public Overrides Function ReadJson(ByVal reader As Newtonsoft.Json.JsonReader, ByVal objectType As Type, ByVal existingValue As Object, ByVal serializer As Newtonsoft.Json.JsonSerializer) As Object
Dim token As Newtonsoft.Json.Linq.JToken = Newtonsoft.Json.Linq.JToken.Load(reader)
If token.Type = Newtonsoft.Json.Linq.JTokenType.Array Then
Return token.ToObject(Of Generic.List(Of T))()
End If
Dim list As New Generic.List(Of T)
list.Add(token.ToObject(Of T)())
Return list
End Function
Public Overrides ReadOnly Property CanWrite() As Boolean
Get
Return False
End Get
End Property
Public Overrides Sub WriteJson(ByVal writer As Newtonsoft.Json.JsonWriter, ByVal value As Object, ByVal serializer As Newtonsoft.Json.JsonSerializer)
Throw New NotImplementedException()
End Sub
End Class
并将此属性应用于我的数据定义。
<Newtonsoft.Json.JsonProperty("Result")> _
<Newtonsoft.Json.JsonConverter(GetType(Utilities.JSONUtilities.SingleOrArrayConverter(Of FeedResultType)))> _
Public Property Result() As Generic.List(Of FeedResultType)
Get
Return m_Result
End Get
Set(ByVal value As Generic.List(Of FeedResultType))
m_Result = value
End Set
End Property
现在一切都很好。 谢谢 Sameers
是的,我使用的是NewtonSoft库