4

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."

1
  • The JSON isn't just a list, it has other stuff too, like "displayFieldName", "fieldAliases", "fields and "features" (your list). I'm not sure if that makes a difference for json.net, but try making an object to accommodate all of them maybe? Commented May 20, 2012 at 23:05

3 Answers 3

6

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; }
}
Sign up to request clarification or add additional context in comments.

Comments

3

IF you are trying to hit that endpoint, you should not be manually submitting the query, you should use the ArcGIS WP7 SDK (it's FREE!). Then use the QueryTask.

(if you just need help with parsing JSON, see below)

    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
}

If for whatever reason, you do not want to use the QueryTask, you can still use the FromJson method of the FeatureSet

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

If you need help with JSON, here are some key concepts.

1) Curly braces represent an object

2) square brackets represent an array.

3) properties are separated by commas

When using JSON.NET, you should add the JsonProperty attribute to a property. This way you can maintain proper names even if the json sucks

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

Comments

2

The JSON returned from that url is:

{
  "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 can see here the root is an object, not an enumerable like a List<>

You'll have to fix the class structure to match the JSON, or access it with Linq queries (there are some samples of this in the json.net website).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.