无法使用 json.net 反序列化 json

本文关键字:json net 反序列化 | 更新日期: 2023-09-27 18:34:57

这是我第一次使用 json.net,我无法弄清楚。这是我下面的代码。

// 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; }
    }

我无法反序列化STATE_NAME属性。我错过了什么?

我不断收到此错误

"无法将 JSON 对象反序列化为类型 'System.Collections.Generic.List'1[WPJsonSample.MainPage+Attributes]'. 第 1 行,位置 20。

无法使用 json.net 反序列化 json

这是

你的类结构(我用过 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; }
}
如果您

尝试命中该端点,则不应手动提交查询,而应使用 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,您仍然可以使用 FeatureSet 的 FromJson 方法。

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; }

从该 url 返回的 JSON 为:

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

因此,我们可以在这里看到根是一个对象,而不是像List<>那样可枚举

您必须修复类结构以匹配 JSON,或使用 Linq 查询访问它(json.net 网站中有一些示例(。