迭代对象反序列化 JSON
Iterate Object Deserialized JSON
我不知道如何遍历对象来检索 JSON 字符串中发送的错误列表。我在 JSON 中的每个密钥对值处都有一个 For Each 循环,但第三个密钥对是密钥对的对象。我无法使用相同的 For Each 结构访问它们(如果我循环遍历对象但希望尽可能保持一致,我可以访问它们。我的 'Case Is "Errors" case 语句是我想要迭代错误的地方对象,但我不确定如何获取它。这是我的代码。我希望有人能提供帮助。
样本JSON:
{"success":"true","api_reference":3821,"errors":[{"record":"landlord","record_id":"-16::1::LPMB40-2385DDDC","error":"Please
ensure the email field has been
completed","error_code":"33101"},{"record":"landlord","record_id":"-16::1::LPMB40-2385DDDC","error":"Please
ensure the email field is a valid email
address","error_code":"33102"}]}
Dim jss = New JavaScriptSerializer()
Dim data = jss.Deserialize(Of Object)(responseFromServer)
Dim strSuccess = "", strAPIReference = ""
Dim intExpiresIn = 0
Dim ErrorsObject As Object
For Each kvp As KeyValuePair(Of String, Object) In data
Select Case kvp.Key
Case Is = "success"
strSuccess = kvp.Value
Case Is = "api_reference"
strAPIReference = kvp.Value
Case Is = "errors"
ErrorsObject = kvp.Value
For Each errorskvp As KeyValuePair(Of String, Object) In ErrorsObject
Next
End Select
Next
我建议执行此强类型操作,以便您可以将 json 反序列化为真实对象并使用对象属性。这样就更容易读取值和循环错误等:
类:
Public Class JsonResponse
Public Property Success As Boolean
Public Property Api_reference As String
Public Property Errors As IEnumerable(Of JsonError)
End Class
Public Class JsonError
Public Property Record As String
Public Property Record_Id As String
Public Property [Error] As String
Public Property Error_Code As String
End Class
反序列化和使用:
Dim j As New JavaScriptSerializer()
Dim data As JsonResponse = j.Deserialize(Of JsonResponse)(responseFromServer)
If Not data.Success Then
For Each myError As JsonError In data.Errors
Next
End If
我不知道如何遍历对象来检索 JSON 字符串中发送的错误列表。我在 JSON 中的每个密钥对值处都有一个 For Each 循环,但第三个密钥对是密钥对的对象。我无法使用相同的 For Each 结构访问它们(如果我循环遍历对象但希望尽可能保持一致,我可以访问它们。我的 'Case Is "Errors" case 语句是我想要迭代错误的地方对象,但我不确定如何获取它。这是我的代码。我希望有人能提供帮助。
样本JSON:
{"success":"true","api_reference":3821,"errors":[{"record":"landlord","record_id":"-16::1::LPMB40-2385DDDC","error":"Please ensure the email field has been completed","error_code":"33101"},{"record":"landlord","record_id":"-16::1::LPMB40-2385DDDC","error":"Please ensure the email field is a valid email address","error_code":"33102"}]}
Dim jss = New JavaScriptSerializer()
Dim data = jss.Deserialize(Of Object)(responseFromServer)
Dim strSuccess = "", strAPIReference = ""
Dim intExpiresIn = 0
Dim ErrorsObject As Object
For Each kvp As KeyValuePair(Of String, Object) In data
Select Case kvp.Key
Case Is = "success"
strSuccess = kvp.Value
Case Is = "api_reference"
strAPIReference = kvp.Value
Case Is = "errors"
ErrorsObject = kvp.Value
For Each errorskvp As KeyValuePair(Of String, Object) In ErrorsObject
Next
End Select
Next
我建议执行此强类型操作,以便您可以将 json 反序列化为真实对象并使用对象属性。这样就更容易读取值和循环错误等:
类:
Public Class JsonResponse
Public Property Success As Boolean
Public Property Api_reference As String
Public Property Errors As IEnumerable(Of JsonError)
End Class
Public Class JsonError
Public Property Record As String
Public Property Record_Id As String
Public Property [Error] As String
Public Property Error_Code As String
End Class
反序列化和使用:
Dim j As New JavaScriptSerializer()
Dim data As JsonResponse = j.Deserialize(Of JsonResponse)(responseFromServer)
If Not data.Success Then
For Each myError As JsonError In data.Errors
Next
End If