ソースコード
public class Trend
{
public string name { get; set; }
public string url { get; set; }
public object promoted_content { get; set; }
public string query { get; set; }
public int? tweet_volume { get; set; }
}
public class Location { public string name { get; set; } public int woeid { get; set; } } public class JsonData { public List<Trend> trends { get; set; } public DateTime as_of { get; set; } public DateTime created_at { get; set; } public List<Location> locations { get; set; } } public class Root { public List<JsonData> json_data { get; set; } }
~~
{
//API key
string apiKey = "";
//API secret key
string apiSecretKey = "";
//Access token
string accessToken = "";
//Access token secret
string accessTokenSecret = "";
//認証情報 var tokens = CoreTweet.Tokens.Create(apiKey, apiSecretKey, accessToken, accessTokenSecret); //大阪のトレンド情報取得 var TrendsJson = tokens.Trends.Place(15015370); //Osaka //JSONデータを取得 string JsonData = TrendsJson.Json.ToString(); //JSON文字列をデシリアライズ var d = JsonConvert.DeserializeObject<Root>(JsonData); ←ここでエラーが出ます。 //取得数 int DataCnt = 3; Random Rnd = new Random(); int TrendRdm = Rnd.Next(0, DataCnt); try { for (int idx = 0; idx < DataCnt; idx++) { mForm.cmbTrend.Items.Add("【" + (idx + 1) + "】" + d.json_data[idx].trends[TrendRdm].query); } } catch (TwitterException e) { MessageBox.Show(e.Message); } }
エラーメッセージ
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'UsefulApp.Logic.AppLogics+Root' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.'
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2021/03/26 23:18
2021/03/27 11:10
2021/03/27 11:13
退会済みユーザー
2021/03/27 11:27