Struggling with system.text.json

Marc Hillman 21 Reputation points
2022-09-28T02:11:14.957+00:00

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 VB.net, but an answer in any language should be fine.

  1. No result {
    "results":[ ]
    }
  2. Success result {
    "results":[
    {
    "layerId":31,
    "layerName":"Islands",
    "displayFieldName":"name",
    "value":"STEPHENS ISLAND",
    "attributes":{
    "OBJECTID":"5767",
    "featuretype":"Island",
    "name":"STEPHENS ISLAND",
    "state":"QLD",
    "featurereliability":"1/1/2004",
    "attributereliability":"9/14/2001",
    "planimetricaccuracy":"100",
    "source":"GEOSCIENCE AUSTRALIA",
    "ufi":"BC52900008",
    "creationdate":"5/9/2006",
    "retirementdate":"Null",
    "pid":"3491389",
    "symbol":"0",
    "SHAPE":"Polygon",
    "st_area(shape)":"0.000012",
    "st_perimeter(shape)":"0.016835"
    }
    }
    ]
    }
VB
VB
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
    2022-09-28T07:50:40.043+00:00

    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)  
            Console.WriteLine(root.results(0).attributes.name)  
        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  
        <JsonPropertyName("st_area(shape)")>  
        Public Property StAreaShape As String  
        <JsonPropertyName("st_perimeter(shape)")>  
        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