gpt4 book ai didi

c# - 无法使用 json.net 反序列化 json

转载 作者:太空狗 更新时间:2023-10-29 22:23:40 25 4
gpt4 key购买 nike

这是我第一次使用 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 属性。我错过了什么?

我一直收到这个错误

"Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[WPJsonSample.MainPage+Attributes]'. Line 1, position 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; }
}

关于c# - 无法使用 json.net 反序列化 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10677913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com