Struggling with system.text.json

Marc Hillman 21 Reputation points

I'm struggling with system.text.json. I have been using NewtonSoft for years, but I'm trying to migrate away from it to avoid dependencies on 3rd party libraries.  I'm trying to do something very simple, and I just don't get it. I am calling a web service which returns the result of a GIS query. I have previously successfully decoded these both with XML and NewtonSoft, but despite many attempts, I just don't get it. What I'm trying to do is very simple.

I want to detect case 1 where there are no results returned.
I want to detect case 2 where there are results returned and extract the "attributes"."name" field.

How hard could it be? Well, despite massive googling, I just can't work it out. I have tried both JsonDocument.Parse and JsonNode.Parse, but after the initial parse I just can't navigate to the correct field without type violations. I'm programming in, but an answer in any language should be fine.

  1. No result {
    "results":[ ]
  2. Success result {
    "value":"STEPHENS ISLAND",
    "name":"STEPHENS ISLAND",
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,602 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,131 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 27,241 Reputation points Microsoft Vendor

    Hi @
    I doing some tests based on the document How to serialize and deserialize (marshal and unmarshal) JSON in .NET and it works well.
    Please try the following code.

    Imports System.IO  
    Imports System.Text.Json  
    Imports System.Text.Json.Serialization  
    Public Class Form1  
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            Dim jsonString As String  
            Dim root As Root  
            jsonString = File.ReadAllText("C:\json.txt")  
            root = JsonSerializer.Deserialize(Of Root)(jsonString)  
        End Sub  
    End Class  
    Public Class Attributes  
        Public Property OBJECTID As String  
        Public Property featuretype As String  
        Public Property name As String  
        Public Property state As String  
        Public Property featurereliability As String  
        Public Property attributereliability As String  
        Public Property planimetricaccuracy As String  
        Public Property source As String  
        Public Property ufi As String  
        Public Property creationdate As String  
        Public Property retirementdate As String  
        Public Property pid As String  
        Public Property symbol As String  
        Public Property SHAPE As String  
        Public Property StAreaShape As String  
        Public Property StPerimeterShape As String  
    End Class  
    Public Class Result  
        Public Property layerId As Integer  
        Public Property layerName As String  
        Public Property displayFieldName As String  
        Public Property value As String  
        Public Property attributes As Attributes  
    End Class  
    Public Class Root  
        Public Property results As List(Of Result)  
    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 additional answers

Sort by: Most helpful