Results 1 to 10 of 10

Thread: JsonProperty auslesen

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2022
    Posts
    28

    Lightbulb JsonProperty auslesen

    Hallo zusammen,

    ich habe folgenden json String
    PHP Code:
    {
    "Message""{
    "
    Hostname": "[EU/GERTest Server",
    "
    MaxPlayers": 2,
    "
    Players": 0,
    "
    Queued": 0,
    "
    Joining": 0,
    "
    EntityCount": 85486,
    "
    GameTime": "06/14/2024 23:03:17",
    "
    Uptime": 1145,
    "
    Map": "Procedural Map",
    "
    Framerate": 237.0,
    "
    Memory": 1412,
    "
    Collections": 137,
    "
    NetworkIn": 0,
    "
    NetworkOut": 0,
    "
    Restarting": false,
    "
    SaveCreatedTime": "01/06/2022 23:17:08"
    }"
    ,
    "Identifier"1,
    "Type""Generic",
    "Stacktrace"""

    Wie kann ich z.b. MaxPlayers ausgeben?

    Meine klasse sieht bis jetzt so aus.

    Code:
    Imports Newtonsoft.Json
     
    Namespace .WebSockets
        Public Class Packet
            Public Sub New(message As Object, identifier As Integer, ByVal Optional _
              name As String = "Test")
                Me.Message = message
                Me.Identifier = identifier
                Me.Name = name
                MaxPlayers = MaxPlayers
     
            End Sub
     
     
     
            <JsonProperty("Identifier")>
            Public Property Identifier As Integer
     
            <JsonProperty("Message")>
            Public Property Message As Object
     
            <JsonProperty("Name")>
            Public Property Name As String
     
            <JsonProperty("MaxPlayers")>
            Public Property MaxPlayers As Integer
     
        End Class
    End Namespace
    Ich komm einfach nicht mehr weiter

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    37,327

    Re: JsonProperty auslesen

    This is an English speaking forum. Here's the translation:


    Quote Originally Posted by StikxX View Post
    Hi, everyone,

    I have the following json string
    PHP Code:
    {
    "Message""{
    "
    Hostname": "[EU/GERTest Server",
    "
    MaxPlayers": 2,
    "
    Players": 0,
    "
    Queued": 0,
    "
    Joining": 0,
    "
    EntityCount": 85486,
    "
    GameTime": "06/14/2024 23:03:17",
    "
    Uptime": 1145,
    "
    Map": "Procedural Map",
    "
    Framerate": 237.0,
    "
    Memory": 1412,
    "
    Collections": 137,
    "
    NetworkIn": 0,
    "
    NetworkOut": 0,
    "
    Restarting": false,
    "
    SaveCreatedTime": "01/06/2022 23:17:08"
    }"
    ,
    "Identifier"1,
    "Type""Generic",
    "Stacktrace"""

    WHow can I e.g. Spend MaxPlayers?

    So far my class looks like this.

    Code:
    Imports Newtonsoft.Json
     
    Namespace .WebSockets
        Public Class Packet
            Public Sub New(message As Object, identifier As Integer, ByVal Optional _
              name As String = "Test")
                Me.Message = message
                Me.Identifier = identifier
                Me.Name = name
                MaxPlayers = MaxPlayers
     
            End Sub
     
     
     
            <JsonProperty("Identifier")>
            Public Property Identifier As Integer
     
            <JsonProperty("Message")>
            Public Property Message As Object
     
            <JsonProperty("Name")>
            Public Property Name As String
     
            <JsonProperty("MaxPlayers")>
            Public Property MaxPlayers As Integer
     
        End Class
    End Namespace

    I just can't get any further
    My usual boring signature: Nothing

  3. #3
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,867

    Re: JsonProperty auslesen

    dein Json ist nicht Korrekt
    your Json isn't Valid

    check here https://jsonlint.com/
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2022
    Posts
    28

    Re: JsonProperty auslesen

    But I get the json back just like that.
    I can't do anything about it. :/

  5. #5
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,773

    Re: JsonProperty auslesen

    The first line of date
    Code:
    "Message": "{
    should be
    Code:
    "Message": {
    notice your data as an extra " character.

    If another system is generating this JSON then the other system is bugged, it isn't generating valid json- if you have a way to report a bug to the owners then I would suggest doing so.

    Other than that I suppose you could try and remove the extra character from the results and then process it, however that is going to get messy if there are more examples of the json being invalid.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2022
    Posts
    28

    Re: JsonProperty auslesen

    Unfortunately I can't change the json. I get them from the game Rust.

    and how can remove the extra character from the results :/

  7. #7
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,090

    Re: JsonProperty auslesen

    There is also a closing curly bracket with a quotation marker
    Code:
    "Message": "{
    "Hostname": "[EU/GER] Test Server",
    "MaxPlayers": 2,
    "Players": 0,
    "Queued": 0,
    "Joining": 0,
    "EntityCount": 85486,
    "GameTime": "06/14/2024 23:03:17",
    "Uptime": 1145,
    "Map": "Procedural Map",
    "Framerate": 237.0,
    "Memory": 1412,
    "Collections": 137,
    "NetworkIn": 0,
    "NetworkOut": 0,
    "Restarting": false,
    "SaveCreatedTime": "01/06/2022 23:17:08"
    }",
    When you get your input data you could replace all "{ with { and }" with }

  8. #8
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,867

    Re: JsonProperty auslesen

    I found this on my HD, the Function ConvertJsonToDatatable isn't my Code
    and I can't remember where I got it from but it seem's to Work.
    It is a Workaround with a lot of Replace, if you want to use it...well that's up to you.

    I took the Crap Json from Post#1 without any changes
    Code:
    Imports System.Text.RegularExpressions
    
    Public Class Form3
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim input As String = IO.File.ReadAllText("D:\TestFolder\StilXX.txt")
            Dim dt As DataTable = ConvertJsonToDatatable(input)
            DataGridView1.DataSource = dt
        End Sub
    
        Protected Function ConvertJsonToDatatable(ByVal jsonString As String) As DataTable
            Dim dt As New DataTable()
            'strip out bad characters
            Dim jsonParts() As String = Regex.Split(jsonString.Replace("[", "").Replace("]", ""), "},{")
            'hold column names
            Dim dtColumns As New List(Of String)()
            'get columns
            For Each jp As String In jsonParts
                'only loop thru once to get column names
                Dim propData() As String = Regex.Split(jp.Replace("{", "").Replace("}", ""), ",")
                For Each rowData As String In propData
                    Try
                        Dim idx As Integer = rowData.IndexOf(":")
                        Dim n As String = rowData.Substring(0, idx - 1)
                        Dim v As String = rowData.Substring(idx + 1)
                        If Not dtColumns.Contains(n) Then
                            dtColumns.Add(n.Replace("""", ""))
                        End If
                    Catch ex As Exception
                        Throw New Exception(String.Format("Error Parsing Column Name : {0}", rowData))
                    End Try
                Next rowData
                Exit For ' TODO: might not be correct. Was : Exit For
            Next jp
            'build dt
            For Each c As String In dtColumns
                dt.Columns.Add(c)
            Next c
            'get table data
            For Each jp As String In jsonParts
                Dim propData() As String = Regex.Split(jp.Replace("{", "").Replace("}", ""), ",")
                Dim nr As DataRow = dt.NewRow()
                For Each rowData As String In propData
                    Try
                        Dim idx As Integer = rowData.IndexOf(":")
                        Dim n As String = rowData.Substring(0, idx - 1).Replace("""", "")
                        Dim v As String = rowData.Substring(idx + 1).Replace("""", "")
                        nr(n) = v
                    Catch ex As Exception
                        Continue For
                    End Try
                Next rowData
                dt.Rows.Add(nr)
            Next jp
            Return dt
        End Function
    End Class
    here a Pic with the results
    Name:  StilX.jpg
Views: 49
Size:  14.7 KB
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2022
    Posts
    28

    Re: JsonProperty auslesen

    Oh nice, thanks ChrisE

    i got it
    Code:
               Dim jstring As String = e.Data
                jstring = jstring.Replace("\r\n", Environment.NewLine)
                jstring = jstring.Replace("\", "")
                jstring = jstring.Replace("""{", "{")
                jstring = jstring.Replace("}""", "}")
                MessageBox.Show(jstring)
                Dim o As JObject = JObject.Parse(jstring)
                Dim maxPlayers As String = CStr(o.SelectToken("Message.MaxPlayers"))
                MessageBox.Show(maxPlayers)
    thanks all for you help.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jan 2022
    Posts
    28

    Re: JsonProperty auslesen

    hm but how can I display this in a label on Form1? :/

    Additional Information: Invoke or BeginInvoke cannot be called on a control until the window handle has been created.

    Code:
            Private Shared Sub WebSocket_OnMessage(ByVal sender As Object, ByVal e As MessageEventArgs)
                Dim jstring As String = e.Data
                jstring = jstring.Replace("\r\n", Environment.NewLine)
                jstring = jstring.Replace("\", "")
                jstring = jstring.Replace("""{", "{")
                jstring = jstring.Replace("}""", "}")
                'MessageBox.Show(jstring)
                Dim o As JObject = JObject.Parse(jstring)
                Dim maxPlayers As String = CStr(o.SelectToken("Message.Hostname"))
                MessageBox.Show(maxPlayers)
    
                Form1.Label1.Invoke(Sub() Form1.Label1.Text = maxPlayers)
            End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width