Warum liest er nur das ende der Json Datei ?

  • C#

Es gibt 39 Antworten in diesem Thema. Der letzte Beitrag () ist von mrMo.

    Warum liest er nur das ende der Json Datei ?

    Hallo liebes Forum !

    Ich habe da ein kleines Problem mit dem Auslesen meiner JSon Datei, und ich hoffe das mir jemand dabei helfen kann.

    Ich habe es geschafft, das das Programm auf die Json-Datei zugreift.
    Auch habe ich es geschafft, das ich etwas daraus in ein Label schreiben kann.
    Hier ist jedoch das Problem, das das Programm nur die letzten Einträge ausliest und sobald ich was von der Mitte oder vom Anfang lesen möchte ist das Label leer? Und ja es steht auch was drinnen :D

    Hier ist der Code:

    C#-Quellcode

    1. Imports System.Net
    2. Imports System.IO
    3. Imports System.Linq
    4. Imports Newtonsoft.Json
    5. Imports Newtonsoft.Json.Linq
    6. Public Class Form1
    7. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    8. Dim request As HttpWebRequest
    9. Dim response As HttpWebResponse = Nothing
    10. Dim reader As StreamReader
    11. Try
    12. request = DirectCast(WebRequest.Create("HIER IST MEINE ADRESSE ZUR URL"), HttpWebRequest)
    13. response = DirectCast(request.GetResponse(), HttpWebResponse)
    14. reader = New StreamReader(response.GetResponseStream())
    15. Dim rawresp As String
    16. rawresp = reader.ReadToEnd
    17. Dim jResult As JObject = JObject.Parse(rawresp)
    18. Dim result As List(Of JToken) = jResult.Children().ToList()
    19. For Each item As JProperty In result
    20. item.CreateReader()
    21. Label1.Text = item.Value("TEST3") ' Das wird ausgelesen
    22. Label2.Text = item.Value("TEST1") ' Das wird NICHT ausgelesen | Macht auch nichts wenn ich es am anfang schreibe
    23. Next
    24. Catch ex As Exception
    25. MsgBox(ex.ToString)
    26. Finally
    27. If Not response Is Nothing Then response.Close()
    28. End Try
    29. End Sub
    30. End Class



    Ich hoffe das mir jemand weiterhelfen kann/will.

    Ps: Das ist nur zum Testen :D also nicht das Fertige Programm, deswegen habe ich noch das Label mit Label stehen :D
    Heyho,

    ich kann hier nur daran appellieren das Try-Catch mal weg zu machen.
    Fehler tut die IDE dir super melden beim Debuggen da braucht man den nicht extra per Try-Catch irgendwo in einer
    Messagebox ausgeben. Hier dazu Lektüre: TryCatch ist ein heißes Eisen

    Und dann mal nen Haltepunkt setzten und schauen was in der rawresp Variable drin steht wie SpaceyX schon sagte.
    Grüße , xChRoNiKx

    Nützliche Links:
    Visual Studio Empfohlene Einstellungen | Try-Catch heißes Eisen
    Hast das Try-Catch mal weggemacht? Wenn Dir das auch keinen HInweis gibt, dann poste mal bitte den Link, damit man sich das mal selber anschauen kann.
    Die Unendlichkeit ist weit. Vor allem gegen Ende. ?(
    Manche Menschen sind gar nicht dumm. Sie haben nur Pech beim Denken. 8o
    In dieser Datei befinden sich keine Keys "TEST1" oder "TEST3", kann gar nicht funktionieren.

    Wenn die verlinkte Datei die Daten enthält, um die es für Dich geht, dann lass Dir hier jsonutils.com/ die Klassen für das JSON-Object generieren. Dann kannst Du einfach die Daten auf die Klassen mappen.

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.IO
    2. Imports Newtonsoft.Json
    3. Module Module1
    4. Sub Main()
    5. Dim json As String = File.ReadAllText("test.json")
    6. Dim result As Example = JsonConvert.DeserializeObject(Of Example)(json)
    7. End Sub
    8. End Module
    9. Public Class Game
    10. Public Property connected As Boolean
    11. Public Property gameName As Object
    12. Public Property paused As Boolean
    13. Public Property time As DateTime
    14. Public Property timeScale As Double
    15. Public Property nextRestStopTime As DateTime
    16. Public Property version As String
    17. Public Property telemetryPluginVersion As String
    18. End Class
    19. Public Class Placement
    20. Public Property x As Double
    21. Public Property y As Double
    22. Public Property z As Double
    23. Public Property heading As Double
    24. Public Property pitch As Double
    25. Public Property roll As Double
    26. End Class
    27. Public Class Acceleration
    28. Public Property x As Double
    29. Public Property y As Double
    30. Public Property z As Double
    31. End Class
    32. Public Class Head
    33. Public Property x As Double
    34. Public Property y As Double
    35. Public Property z As Double
    36. End Class
    37. Public Class Cabin
    38. Public Property x As Double
    39. Public Property y As Double
    40. Public Property z As Double
    41. End Class
    42. Public Class Hook
    43. Public Property x As Double
    44. Public Property y As Double
    45. Public Property z As Double
    46. End Class
    47. Public Class Truck
    48. Public Property id As String
    49. Public Property make As String
    50. Public Property model As String
    51. Public Property speed As Double
    52. Public Property cruiseControlSpeed As Double
    53. Public Property cruiseControlOn As Boolean
    54. Public Property odometer As Double
    55. Public Property gear As Integer
    56. Public Property displayedGear As Integer
    57. Public Property forwardGears As Integer
    58. Public Property reverseGears As Integer
    59. Public Property shifterType As String
    60. Public Property engineRpm As Double
    61. Public Property engineRpmMax As Double
    62. Public Property fuel As Double
    63. Public Property fuelCapacity As Double
    64. Public Property fuelAverageConsumption As Double
    65. Public Property fuelWarningFactor As Double
    66. Public Property fuelWarningOn As Boolean
    67. Public Property wearEngine As Double
    68. Public Property wearTransmission As Double
    69. Public Property wearCabin As Double
    70. Public Property wearChassis As Double
    71. Public Property wearWheels As Double
    72. Public Property userSteer As Double
    73. Public Property userThrottle As Double
    74. Public Property userBrake As Double
    75. Public Property userClutch As Double
    76. Public Property gameSteer As Double
    77. Public Property gameThrottle As Double
    78. Public Property gameBrake As Double
    79. Public Property gameClutch As Double
    80. Public Property shifterSlot As Integer
    81. Public Property engineOn As Boolean
    82. Public Property electricOn As Boolean
    83. Public Property wipersOn As Boolean
    84. Public Property retarderBrake As Integer
    85. Public Property retarderStepCount As Integer
    86. Public Property parkBrakeOn As Boolean
    87. Public Property motorBrakeOn As Boolean
    88. Public Property brakeTemperature As Double
    89. Public Property adblue As Double
    90. Public Property adblueCapacity As Double
    91. Public Property adblueAverageConsumption As Double
    92. Public Property adblueWarningOn As Boolean
    93. Public Property airPressure As Double
    94. Public Property airPressureWarningOn As Boolean
    95. Public Property airPressureWarningValue As Double
    96. Public Property airPressureEmergencyOn As Boolean
    97. Public Property airPressureEmergencyValue As Double
    98. Public Property oilTemperature As Double
    99. Public Property oilPressure As Double
    100. Public Property oilPressureWarningOn As Boolean
    101. Public Property oilPressureWarningValue As Double
    102. Public Property waterTemperature As Double
    103. Public Property waterTemperatureWarningOn As Boolean
    104. Public Property waterTemperatureWarningValue As Double
    105. Public Property batteryVoltage As Double
    106. Public Property batteryVoltageWarningOn As Boolean
    107. Public Property batteryVoltageWarningValue As Double
    108. Public Property lightsDashboardValue As Double
    109. Public Property lightsDashboardOn As Boolean
    110. Public Property blinkerLeftActive As Boolean
    111. Public Property blinkerRightActive As Boolean
    112. Public Property blinkerLeftOn As Boolean
    113. Public Property blinkerRightOn As Boolean
    114. Public Property lightsParkingOn As Boolean
    115. Public Property lightsBeamLowOn As Boolean
    116. Public Property lightsBeamHighOn As Boolean
    117. Public Property lightsAuxFrontOn As Boolean
    118. Public Property lightsAuxRoofOn As Boolean
    119. Public Property lightsBeaconOn As Boolean
    120. Public Property lightsBrakeOn As Boolean
    121. Public Property lightsReverseOn As Boolean
    122. Public Property placement As Placement
    123. Public Property acceleration As Acceleration
    124. Public Property head As Head
    125. Public Property cabin As Cabin
    126. Public Property hook As Hook
    127. End Class
    128. Public Class Trailer
    129. Public Property attached As Boolean
    130. Public Property id As String
    131. Public Property name As String
    132. Public Property mass As Double
    133. Public Property wear As Double
    134. Public Property placement As Placement
    135. End Class
    136. Public Class Job
    137. Public Property income As Integer
    138. Public Property deadlineTime As DateTime
    139. Public Property remainingTime As DateTime
    140. Public Property sourceCity As String
    141. Public Property sourceCompany As String
    142. Public Property destinationCity As String
    143. Public Property destinationCompany As String
    144. End Class
    145. Public Class Navigation
    146. Public Property estimatedTime As DateTime
    147. Public Property estimatedDistance As Integer
    148. Public Property speedLimit As Integer
    149. End Class
    150. Public Class Example
    151. Public Property game As Game
    152. Public Property truck As Truck
    153. Public Property trailer As Trailer
    154. Public Property job As Job
    155. Public Property navigation As Navigation
    156. End Class
    Die Unendlichkeit ist weit. Vor allem gegen Ende. ?(
    Manche Menschen sind gar nicht dumm. Sie haben nur Pech beim Denken. 8o
    In dieser Datei befinden sich keine Keys "TEST1" oder "TEST3", kann gar nicht funktionieren.


    Weis ich die habe ich nur zum anschauen reingemacht :) stehen natürlich die richtigen drinnen :D


    Aber jetzt stehe ich komplett auf den schlauch ?( 8|
    das ganze soll ja online sein da sich der Inhalt immer ändert.
    Sollte so gehen.

    VB.NET-Quellcode

    1. Imports System.IO
    2. Imports Newtonsoft.Json
    3. Imports Newtonsoft.Json.Linq
    4. Module Module1
    5. Sub Main()
    6. Dim json As String = File.ReadAllText("test.json")
    7. Dim x As JObject = JObject.Parse(json)
    8. Dim y As JToken = x("truck")("make")
    9. Console.WriteLine(y.ToString())
    10. Console.ReadLine()
    11. End Sub


    Ich würde aber die Variante bevorzugen, die ich weiter oben gepostet habe. Auf Klassen mappen macht Vieles einfacher.
    Die Unendlichkeit ist weit. Vor allem gegen Ende. ?(
    Manche Menschen sind gar nicht dumm. Sie haben nur Pech beim Denken. 8o
    Hast Du mal geschaut, ob der 2. Wert überhaupt einen Wert hat? Hast Du mal das TryCatch weggemacht, um zu sehen, ob eine Exception geworfen wird?
    Die Unendlichkeit ist weit. Vor allem gegen Ende. ?(
    Manche Menschen sind gar nicht dumm. Sie haben nur Pech beim Denken. 8o
    Ich habe das TryCatch raus gemacht und es gab keine Exception.

    Einige werte sind true oder fals abfragen. ich habe auch versucht das Game zu starten um alle werte zu bekommen. Aber auch hier das gleiche Problem in ein Label will er es nicht schreiben die MsgBox hingegen wird korrekt ausgefült.
    Okay..

    Also das ist die Json datei.
    Spoiler anzeigen

    {"game":{
    "connected":false,
    "gameName":null,
    "paused":false,
    "time":"0001-01-01T00:00:00Z",
    "timeScale":0.0,
    "nextRestStopTime":"0001-01-01T00:00:00Z",
    "version":"0.0",
    "telemetryPluginVersion":"0"
    },
    "truck":{
    "id":"",
    "make":"",
    "model":"",
    "speed":0.0,
    "cruiseControlSpeed":0.0,
    "cruiseControlOn":false,
    "odometer":0.0,
    "gear":0,
    "displayedGear":0,
    "forwardGears":0,
    "reverseGears":0,
    "shifterType":"",
    "engineRpm":0.0,
    "engineRpmMax":0.0,
    "fuel":0.0,
    "fuelCapacity":0.0,
    "fuelAverageConsumption":0.0,
    "fuelWarningFactor":0.0,
    "fuelWarningOn":false,
    "wearEngine":0.0,
    "wearTransmission":0.0,
    "wearCabin":0.0,
    "wearChassis":0.0,
    "wearWheels":0.0,
    "userSteer":0.0,
    "userThrottle":0.0,
    "userBrake":0.0,
    "userClutch":0.0,
    "gameSteer":0.0,
    "gameThrottle":0.0,
    "gameBrake":0.0,
    "gameClutch":0.0,
    "shifterSlot":0,
    "engineOn":false,
    "electricOn":false,
    "wipersOn":false,
    "retarderBrake":0,
    "retarderStepCount":0,
    "parkBrakeOn":false,
    "motorBrakeOn":false,
    "brakeTemperature":0.0,
    "adblue":0.0,
    "adblueCapacity":0.0,
    "adblueAverageConsumption":0.0,
    "adblueWarningOn":false,
    "airPressure":0.0,
    "airPressureWarningOn":false,
    "airPressureWarningValue":0.0,
    "airPressureEmergencyOn":false,
    "airPressureEmergencyValue":0.0,
    "oilTemperature":0.0,
    "oilPressure":0.0,
    "oilPressureWarningOn":false,
    "oilPressureWarningValue":0.0,
    "waterTemperature":0.0,
    "waterTemperatureWarningOn":false,
    "waterTemperatureWarningValue":0.0,
    "batteryVoltage":0.0,
    "batteryVoltageWarningOn":false,
    "batteryVoltageWarningValue":0.0,
    "lightsDashboardValue":0.0,
    "lightsDashboardOn":false,
    "blinkerLeftActive":false,
    "blinkerRightActive":false,
    "blinkerLeftOn":false,
    "blinkerRightOn":false,
    "lightsParkingOn":false,
    "lightsBeamLowOn":false,
    "lightsBeamHighOn":false,
    "lightsAuxFrontOn":false,
    "lightsAuxRoofOn":false,
    "lightsBeaconOn":false,
    "lightsBrakeOn":false,
    "lightsReverseOn":false,

    "placement":{
    "x":0.0,
    "y":0.0,
    "z":0.0,
    "heading":0.0,
    "pitch":0.0,
    "roll":0.0},

    "acceleration":{
    "x":0.0,
    "y":0.0,
    "z":0.0},

    "head":{
    "x":0.0,
    "y":0.0,
    "z":0.0},

    "cabin":{
    "x":0.0,
    "y":0.0,
    "z":0.0},

    "hook":{
    "x":0.0,
    "y":0.0,
    "z":0.0}
    },

    "trailer":{
    "attached":false,
    "id":"",
    "name":"",
    "mass":0.0,
    "wear":0.0,
    "placement":{
    "x":0.0,
    "y":0.0,
    "z":0.0,
    "heading":0.0,
    "pitch":0.0,
    "roll":0.0}
    },

    "job":{
    "income":0,
    "deadlineTime":"0001-01-01T00:00:00Z",
    "remainingTime":"0001-01-01T00:00:00Z",
    "sourceCity":"",
    "sourceCompany":"",
    "destinationCity":"",
    "destinationCompany":""},

    "navigation":{
    "estimatedTime":"0001-01-01T00:00:00Z",
    "estimatedDistance":0,
    "speedLimit":0}
    }


    Und das ist der Code der ja soweit funktioniert.
    Spoiler anzeigen

    C-Quellcode

    1. Imports System.Net
    2. Imports System.IO
    3. Imports System.Linq
    4. Imports Newtonsoft.Json
    5. Imports Newtonsoft.Json.Linq
    6. Public Class Form1
    7. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    8. Dim request As HttpWebRequest
    9. Dim respnse As HttpWebResponse = Nothing
    10. Dim reader As StreamReader
    11. request = DirectCast(WebRequest.Create("http://127.0.0.1:25555/api/ets2/telemetry"), HttpWebRequest)
    12. respnse = DirectCast(request.GetResponse(), HttpWebResponse)
    13. reader = New StreamReader(respnse.GetResponseStream())
    14. Dim rawresp As String
    15. rawresp = reader.ReadToEnd
    16. Dim jResult As JObject = JObject.Parse(rawresp)
    17. Dim result As List(Of JToken) = jResult.Children().ToList()
    18. For Each item As JProperty In result
    19. item.CreateReader()
    20. TextBox1.Text = item.Value("estimatedTime")
    21. TextBox2.Text = item.Value("version")
    22. TextBox3.Text = item.Value("speedLimit")
    23. Next
    24. End Sub
    25. End Class



    So weit ist ja alles gut. Wie ich schon sagte, bekomme ich keine Exception.
    Das einzige was ist, er liest mir nur die Letzten drei ("estimatedTime":"0001-01-01T00:00:00Z","estimatedDistance":0,"speedLimit":0}) Werte aus wenn ich es in einer Textbox oder Label schreiben will.
    Sobald ich es aber in eine MSG Box schreibe liest er alles aus was ich will....

    Hier noch ein Bild :)
    prntscr.com/mj2jkh

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Jochen2208“ ()

    Beschreib mal, was du denkst, was dieser Part hier macht.

    VB.NET-Quellcode

    1. For Each item As JProperty In result
    2. item.CreateReader()
    3. TextBox1.Text = item.Value("estimatedTime")
    4. TextBox2.Text = item.Value("version")
    5. TextBox3.Text = item.Value("speedLimit")
    6. Next
    "Gib einem Mann einen Fisch und du ernährst ihn für einen Tag. Lehre einen Mann zu fischen und du ernährst ihn für sein Leben."

    Wie debugge ich richtig? => Debuggen, Fehler finden und beseitigen
    Wie man VisualStudio nutzt? => VisualStudio richtig nutzen
    Ich denke das hier folgendes gemacht wird:

    Er liest in einer Schleife die Property aus, die er aus result bekommt und speichert diese dann wiederum in Item ab.
    Danach erstellt "Item" einen Reader der mir die werte ausliest.

    Die Textbox sollte eiigendlich den Inhalt von "Item" wiedergeben

    Bitte nicht schlagen wen es falsch ist :)