Unable to deserialize json using json.net

This is my first time using json.net and I cannot 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 cannot deserialize the attributes of STATE_NAME. What am I missing?

I keep getting this error

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

+5
source share
3 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; }
}
+7
source

JSON is returned from this URL:

{
  "displayFieldName": "STATE_NAME",
  "fieldAliases": {
    "STATE_NAME": "STATE_NAME"
  },
  "fields": [
    {
      "name": "STATE_NAME",
      "type": "esriFieldTypeString",
      "alias": "STATE_NAME",
      "length": 25
    }
  ],
  "features": [
    {
      "attributes": {
        "STATE_NAME": "Maine"
      }
  }
}

So we see here that the root is an object, not enumerated as List<>

JSON Linq ( json.net ).

+3

, , ArcGIS WP7 SDK ( !). QueryTask.

( JSON, . )

    QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/1/");
    queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
    queryTask.Failed += QueryTask_Failed;

    ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
    query.Text = "e";
    query.ReturnGeometry = false;

    queryTask.ExecuteAsync(query);


private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
{
    FeatureSet featureSet = args.FeatureSet
    // use the featureSet to do something. It contains everything you need
}

- QueryTask, FromJson FeatureSet

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    var featureSet = ESRI.ArcGIS.Client.Tasks.FeatureSet.FromJson(e.Result);
    // Use it
}

JSON, .

1)

2) .

3)

JSON.NET JsonProperty . , , json

[JsonProperty("STATE_NAME")]
public string StateName { get; set; }
+3

All Articles