Solved

ASP.NET/VB: JavaScriptSerializer - Avoiding Errors When JSON Empty

Posted on 2013-12-24
1
719 Views
Last Modified: 2013-12-24
This works:
Imports System.Web.Script.Serialization
Public Class JSONstuff
    Inherits System.Web.UI.Page
    Dim obj As ResultRows
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim data As String = "{""total_rows"":427,""offset"":0,""rows"":[ {""id"":""123xyz""} ]}"
        Dim jsd As New JavaScriptSerializer
        obj = jsd.Deserialize(Of ResultRows)(data)
        Response.Write("id: " & obj.rows(0).id)
    End Sub
End Class

Public Class ResultRows
    Public rows() As ResultRow
End Class

Public Class ResultRow
    Public id As String
End Class

Open in new window

But this returns an error:

Server Error in '/' Application.
Index was outside the bounds of the array.
Imports System.Web.Script.Serialization
Public Class JSONstuff
    Inherits System.Web.UI.Page
    Dim obj As ResultRows
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim data As String = "{""total_rows"":427,""offset"":0,""rows"":[]}"
        Dim jsd As New JavaScriptSerializer
        obj = jsd.Deserialize(Of ResultRows)(data)
        Response.Write("id: " & obj.rows(0).id)
    End Sub
End Class

Public Class ResultRows
    Public rows() As ResultRow
End Class

Public Class ResultRow
    Public id As String
End Class

Open in new window

Instead of returning an error, how can I get it to return an empty value for the id?
0
Comment
Question by:hankknight
1 Comment
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Response.Write("id: " & IF(obj IsNot Nothing AndAlso obj.Rows.Length> 0, obj.rows(0).id, String.Empty))

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now