Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't deserialize json using json.net

This is my first time using json.net and I can't figure it out. Here is my code below.

// Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    private void btnRefreshTweets_Click(object sender, RoutedEventArgs e)
    {
        string ServerURL = @"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/1/query?text=e&geometry=&geometryType=esriGeometryPoint&inSR=&spatialRel=esriSpatialRelIntersects&relationParam=&objectIds=&where=&time=&returnCountOnly=false&returnIdsOnly=false&returnGeometry=false&maxAllowableOffset=&outSR=&outFields=&f=json";

        WebClient webClient = new WebClient();
        webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
        webClient.DownloadStringAsync(new Uri(ServerURL));
    }

    void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            return;
        }
        List<Attributes> tweets = JsonConvert.DeserializeObject<List<Attributes>>(e.Result);
        this.lbTweets.ItemsSource = tweets;
    }

    public class Attributes
    {
        public string STATE_NAME { get; set; }
    }

I can't deserialize the STATE_NAME attributes. What am I missing?

I keep getting this error

"Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[WPJsonSample.MainPage+Attributes]'. Line 1, position 20."

like image 235
user1362269 Avatar asked Dec 21 '22 21:12

user1362269


1 Answers

Here is your class structure ( I used http://json2csharp.com/)

public class FieldAliases
{
    public string STATE_NAME { get; set; }
}

public class Field
{
    public string name { get; set; }
    public string type { get; set; }
    public string alias { get; set; }
    public int length { get; set; }
}

public class Attributes
{
    public string STATE_NAME { get; set; }
}

public class Feature
{
    public Attributes attributes { get; set; }
}

public class RootObject
{
    public string displayFieldName { get; set; }
    public FieldAliases fieldAliases { get; set; }
    public List<Field> fields { get; set; }
    public List<Feature> features { get; set; }
}
like image 127
Byronic Coder Avatar answered Dec 23 '22 10:12

Byronic Coder