发出访问反序列化的json数据的问题

时间:2018-02-07 12:07:45

标签: json vb.net

我是webhooks和json的新手但是一直在搜索这个网站,谷歌的其余部分试图弄清楚如何处理我正在进行的项目的sendgrid json帖子。我有代码在功能上工作但是为我的数据返回任何/空字符串或错误(对于唯一的args)。

我正在使用newtsonsoft.json,目前只是发布到aspx页面并尝试输出数据以完成该过程。我有一个固定的样本json帖子,我正在发送。

以下所有信息 - 非常感谢有人建议我在哪里搞砸了,因为我一直在寻找样品和解决方案而且卡住了!

代码

Public Class sgData

    Public Property sgEmail() As String
    Public Property sgTimestamp() As String
    Public Property sgSMTPID() As String
    Public Property sgEvent() As String
    Public Property sgCategory() As String
    Public Property sgEventID() As String
    Public Property sgMsgID() As String
    Public Property sgUserAgent() As String
    Public Property sgIP() As String
    Public Property sgURL() As String
    Public Property sgASMGroup() As String
    Public Property sgUniqueArgs As uArgs

End Class

Public Class uArgs

    Public Property CB() As String
    Public Property emailType() As String
    Public Property prodGroup() As String

End Class

Public Class xhandler

    Inherits System.Web.UI.Page
    Implements System.Web.IHttpHandler

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim whReader As New StreamReader(HttpContext.Current.Request.InputStream)
        Dim strjson = whReader.ReadToEnd()
        Dim sgResponse = JsonConvert.DeserializeObject(Of List(Of sgData))(strjson)
        For Each item In sgResponse
            HttpContext.Current.Response.Write("-> event=" & item.sgEvent & Chr(13))    ' is empty/nothing
            HttpContext.Current.Response.Write("-> CB=" & item.sgUniqueArgs.CB & Chr(13))    ' throws an error = Object reference not set to an instance of an object
        Next

    End Sub

End Class

json的内容......

[
  {
    "email": "example@test.com",
    "timestamp": 1517928354,
    "smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
    "event": "processed",
    "category": "cat facts",
    "sg_event_id": "uMaJLSrLHH96LFwwpCju5w==",
    "sg_message_id": "14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0",
    "unique_args": {
        "CB": "111",
        "emailType": "invite",
        "prodGroup": "REFEX"
    }
  },
  {
    "email": "example@test.com",
    "timestamp": 1517928354,
    "smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
    "event": "deferred",
    "category": "cat facts",
    "sg_event_id": "3pSLStZrkwJ52nPCBa6z-Q==",
    "sg_message_id": "14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0",
    "response": "400 try again later",
    "attempt": "5",
    "unique_args": {
        "CB": "222",
        "emailType": "invite",
        "prodGroup": "REFEX"
    }
  }
]

2 个答案:

答案 0 :(得分:0)

只需使用像Json.NET这样的JSON库来解析你的字符串。 也许this可以帮到你。

答案 1 :(得分:0)

确定排序了。只是将它直接反序列化为一个对象,然后将unique_args反序列化为一个字典,以完全忽略我创建的类。

下面的工作代码段:

Dim sgResponse = JsonConvert.DeserializeObject(strjson)
For Each item In sgResponse
    sgEventType = item("event").ToString
    candEmail = item("email").ToString
    Dim uniqueArgs = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(item("unique_args").ToString)
    CB = uniqueArgs("CB")
    emailType = uniqueArgs("emailType")
    prodGroup = uniqueArgs("prodGroup")
    HttpContext.Current.Response.Write("-> event=" & sgEventType & ", email=" & candEmail & ", CB=" & CB & ", emailType=" & emailType & ", prodGroup=" & prodGroup & Chr(13))
Next

它输出以下内容:

-> event=processed, email=example@test.com, CB=111, emailType=invite, prodGroup=REFEX
-> event=deferred, email=example@test.com, CB=222, emailType=invite, prodGroup=REFEX
是的,由于某种原因,我不能早点解决它:-D

相关问题