0

I could use some assistance. I am trying to deserialize a Json file and then add it to a list so I can drop it into a table within a C# application. I get part of my code to produce, but not the nested areas.

I want to count the total number of properties (shown as "features" in the json). I then want to list out the "attributes" for each property.

Any help would be awesome. -Rob

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Net;


public class Program
{
    public static void Main()
    {
          WebClient client = new WebClient();
          string json = client.DownloadString("https://gis.nccde.org/agsserver/rest/services/CustomMaps/Ownership/MapServer/0/query?where=UPPER(SUBDIV)%20like%20'%25ENCLAVE%20AT%20ODESSA%25'&outFields=*&outSR=4326&f=json");            

        var myModel = JsonConvert.DeserializeObject<ParcelViewModel>(json);

        Console.WriteLine("Top Level ID: " + myModel.displayFieldName);

        Console.WriteLine("Property Count: " + myModel.features.Count);

        int i = 0;
        foreach (var fac in myModel.features)
        {
            i++;
            Console.WriteLine(" ");
            Console.WriteLine("Record Number: " + i);
            Console.WriteLine("Address: " + fac.address);
            Console.WriteLine("Owner: " + fac.CNTCTLAST);
            Console.WriteLine(" ");
        }
    }
}

public class ParcelViewModel
{
  public string displayFieldName { get; set; }
    public string geometryType { get; set; }
    public List<PropertyViewModel> features { get; set; }
}


public class PropertyViewModel
{
    public string address { get; set; }
    public string CNTCTLAST { get; set; }
}
user891073
  • 41
  • 6
  • Paste your JSON here: [QuickType](https://app.quicktype.io/#l=cs&r=json2csharp). It will create all the related classes (and *special* deserializers, if needed) for you. You can also use a partial class (a class that doesn't include all the properties) if you don't care about some of the values. – Jimi Feb 23 '19 at 19:04

1 Answers1

0

The ViewModel structure you have used is incorrect. attributes should be an object of type AttributeViewModel looking at the JSON response. I have set a few properties just to show how it should be set.

    static void Main(string[] args)
    {
        WebClient client = new WebClient();
        string json = client.DownloadString("https://gis.nccde.org/agsserver/rest/services/CustomMaps/Ownership/MapServer/0/query?where=UPPER(SUBDIV)%20like%20'%25ENCLAVE%20AT%20ODESSA%25'&outFields=*&outSR=4326&f=json");

        var myModel = JsonConvert.DeserializeObject<ParcelViewModel>(json);

        Console.WriteLine("Top Level ID: " + myModel.displayFieldName);

        Console.WriteLine("Property Count: " + myModel.features.Count);

        int i = 0;
        foreach (var fac in myModel.features)
        {
            i++;
            Console.WriteLine(" ");
            Console.WriteLine("Record Number: " + i);
            Console.WriteLine("Address: " + fac.attributes.ADDRESS);
            Console.WriteLine("Owner: " + fac.attributes.CNTCTLAST);
            Console.WriteLine(" ");
        }
    }

    public class ParcelViewModel
    {
        public string displayFieldName { get; set; }
        public string geometryType { get; set; }
        public List<FeatureViewModel> features { get; set; }
    }


    public class FeatureViewModel
    {
        public AttributeViewModel attributes { get; set; }            
    }

    public class AttributeViewModel
    {
        public string ADDRESS { get; set; }
        public string CNTCTLAST { get; set; }
    }
Praveen Paulose
  • 5,741
  • 1
  • 15
  • 19