這是我第一次使用json.net,我無法弄清楚。以下是我的代碼如下。無法反序列化json使用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 +屬性]'。 行1,位置20」。
的JSON不只是一個列表,它有其他的東西太多,像「displayFieldName」,「fieldAliases」,「區域和‘功能’(您的我不確定這對json.net有什麼影響,但是可以嘗試讓一個對象容納所有這些對象嗎? – blitzen
我會在繼續使用json.net之前檢出這個帖子,這很簡單:http: //www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/using-c-4.0-and-dynamic-to-parse-json.aspx –