Can not convert {null} to String.

May 26, 2009 at 4:52 PM

Hello,

I'm trying to import a 36meg json file.  I'm using beta 3.

My code returns this initially:

Newtonsoft.Json.Linq.JArray
Newtonsoft.Json.Linq.JObject

However when I try to retrieve the property values I get this error.

Can not convert {null} to String.  on this line: j2 = JsonConvert.DeserializeObject(Of JObject)(p, s)

Here is a sample of the json file.

[{"crossword": {"image": "http://static.c2w.com/uploads/skin/image/27/N27/1231514184.png", "title": "RAMAYANA", "number_of_questions": 10, "base_url": "http://c2w.com/crosswords/25753-RAMAYANA", "id": 25753, "embed": "<object type='application/x-shockwave-flash' data='http://c2w.com/C-25753' width='550' height='500' id='c2w_game'><param name='allowScriptAccess' value='always'/><param name='movie' value='http://c2w.com/C-25753'/><param name='wmode' value='transparent'/><embed src='http://c2w.com/C-25753' wmode='transparent' allowScriptAccess='always' type='application/x-shockwave-flash' width='550' height='500'></embed></object>", "description": "ITS A SIMPLE QUIZ ABOUT ANCIENT RAMAYANA.ITS QUIET EASY,EXCITING", "category": "General", "creator": "venkyraji", "playing_instructions": "", "ends_on": "2020-01-01"}}]

Here is the full source of my code.  I'm sure I'm doing something stupid  :)

 

<%@ Page Language="VB" Debug="true" %>

<%@ Import Namespace="system.io" %>
<%@ Import Namespace="Newtonsoft.Json" %>
<%@ Import Namespace="Newtonsoft.Json.Linq" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Class game
        Private p_image As String
        Public Property image() As String
            Get
                Return p_image
            End Get
            Set(ByVal value As String)
                p_image = value
            End Set
        End Property
       
        Private p_title As String
        Public Property title() As String
            Get
                Return p_title
            End Get
            Set(ByVal value As String)
                p_title = value
            End Set
        End Property
       
        Private p_questions As String
        Public Property questions() As String
            Get
                Return p_questions
            End Get
            Set(ByVal value As String)
                p_questions = value
            End Set
        End Property
       
        Private p_url As String
        Public Property url() As String
            Get
                Return p_url
            End Get
            Set(ByVal value As String)
                p_url = value
            End Set
        End Property
       
        Private p_id As String
        Public Property id() As String
            Get
                Return p_id
            End Get
            Set(ByVal value As String)
                p_id = value
            End Set
        End Property
       
        Private p_embed As String
        Public Property embed() As String
            Get
                Return p_embed
            End Get
            Set(ByVal value As String)
                p_embed = value
            End Set
        End Property
       
        Private p_category As String
        Public Property category() As String
            Get
                Return p_category
            End Get
            Set(ByVal value As String)
                p_category = value
            End Set
        End Property
       
        Private p_creator As String
        Public Property creator() As String
            Get
                Return p_creator
            End Get
            Set(ByVal value As String)
                p_creator = value
            End Set
        End Property
       
        Private p_instructions As String
        Public Property instructions() As String
            Get
                Return p_instructions
            End Get
            Set(ByVal value As String)
                p_instructions = value
            End Set
        End Property
       
        Private p_endson As String
        Public Property endson() As String
            Get
                Return p_endson
            End Get
            Set(ByVal value As String)
                p_endson = value
            End Set
        End Property
       
    End Class
   
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
       
        Dim count As Integer = 0
        Dim sr As StreamReader = File.OpenText(Server.MapPath("dump.json"))

        Dim r As JsonTextReader = New JsonTextReader(sr)
        'r.Read()
       
       
        Dim s As New JsonSerializerSettings
        s.NullValueHandling = NullValueHandling.Ignore
        s.MissingMemberHandling = MissingMemberHandling.Ignore
       
        Dim j As JArray = JsonConvert.DeserializeObject(sr.ReadToEnd)
        Dim j2 As JObject
       
       
        out.Text = j.GetType().ToString
       
        For Each p As JObject In j
            out.Text += "<li>" & p.GetType.ToString
            j2 = JsonConvert.DeserializeObject(Of JObject)(p, s)
            out.Text += "<li>" & j2.GetType.ToString
            For Each p1 As JProperty In j2
                out.Text += "<li>" & p1.Name.ToString
            Next
          
            count += 1
            If count = 1 Then
                Return
            End If
        Next
       
       

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="out" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

 

Coordinator
May 30, 2009 at 1:26 AM

I think I have fixed the error you have encountered in the latest version of the source code. It is available to download from here.