[英]Can't deserialize json using json.net
这是我第一次使用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位。”
这是你的类结构(我使用过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; }
}
从该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"
}
}
}
所以,我们可以看到root是一个对象,而不是像List<>
这样的可枚举
您必须修复类结构以匹配JSON,或者使用Linq查询访问它(在json.net网站中有一些这样的示例)。
如果您尝试点击该端点,则不应手动提交查询,应使用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; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.