VB code get Json str data.

Kerry Ou 226 Reputation points
2022-10-13T09:11:49.59+00:00

Hi all,
I have a json str from web foreground parameter.

[{"ID":"3957","Name":"Terry","Dept":"WetB","SDate":"2022-10-07T00:00:00","shift":"D07:45","time1":"7:33:07   "}]  

How Can I get "ID" and "shift" from this str ?

I tried this way, but it didn't work. showing error "Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1."

        Dim jstr As String = e.ExtraParams("Values")  
  
        Dim js As JObject  
        js = JObject.Parse(jstr)  
        MsgBox(Js.SelectToken("ID").SelectToken("shift"))  

Any other way to get the data? Please help. Thank you so much.

Developer technologies VB
Developer technologies ASP.NET Other
{count} votes

2 answers

Sort by: Most helpful
  1. LesHay 7,141 Reputation points
    2022-10-13T13:09:21.82+00:00

    Hi
    Tentative offering! I am new to json.
    To get this to work, I had to edit the json string to actually be a string by adding quotes around all entities.
    After that, the code produced the desired result.
    You may well get a better answer from more experienced contributors yet.

    Imports System.Web.Script.Serialization  
    Public Class Form1  
    	Private Sub Form1_Load(senderAsObject, eAsEventArgs) Handles MyBase.Load  
      
    		Dim jstr As String = "{""ID"":""3957"",""Name"":""Terry"",""Dept"":""WetB"",""SDate"":""2022-10-8T00:00:00"",""shift"":""D07:45"",""time1"":""7:33:07   ""}"  
      
    		Dim jss As New JavaScriptSerializer()  
    		Dim dict As Dictionary(Of String, String) = jss.Deserialize(Of Dictionary(Of String, String))(jstr)  
      
    		Dim s1 As String = dict("ID") '3957  
    		Dim s2 As String = dict("time1") '7:33:07     
    		Dim s3 As String = dict("Name") 'Terry  
    		Dim s4 As String = dict("shift") 'D07:45  
    		Dim s5 As Date = dict("SDate") '#10/8/2022 12:00:00 AM#  
      
    	End Sub  
    End Class  
    

  2. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-10-20T09:06:18.027+00:00

    Hi @Kerry Ou ,
    You can refer to the following code.
    I used a txt file as the json datasource.

    Imports System.IO  
    Imports System.Text.Json  
      
    Public Class Form1  
      
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  
            Dim jsonString As String  
            Dim root As Root  
            jsonString = File.ReadAllText("C:\Users\Administrator\Desktop\json.txt")  
            jsonString = jsonString.Remove(0, 1)  
            jsonString = jsonString.Remove(jsonString.Length - 1, 1)  
            root = JsonSerializer.Deserialize(Of Root)(jsonString)  
            Console.WriteLine(root.ID)  
            Console.WriteLine(root.shift)  
        End Sub  
      
    End Class  
      
    Public Class Root  
        Public Property ID As String  
        Public Property Name As String  
        Public Property Dept As String  
        Public Property SDate As DateTime  
        Public Property shift As String  
        Public Property time1 As String  
    End Class  
    

    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.