gpt4 book ai didi

c# - C#.net 中的 JSON Twitter 列表

转载 作者:太空狗 更新时间:2023-10-29 17:54:23 28 4
gpt4 key购买 nike

我的代码如下。我无法提取“名称”和“查询”列表通过 DataContracted 类从 JSON(如下)我花了很长时间试图解决这个问题,并且真的可以做到在一些帮助下...

我的 Json 字符串:

{"as_of":1266853488,"trends":{"2010-02-22 
15:44:48":[{"name":"#nowplaying","query":"#nowplaying"},{"name":"#musicmonday","query":"#musicmonday"},{"name":"#WeGoTogetherLike","query":"#WeGoTogetherLike"},{"name":"#imcurious","query":"#imcurious"},{"name":"#mm","query":"#mm"},{"name":"#HumanoidCityTour","query":"#HumanoidCityTour"},{"name":"#awesomeindianthings","query":"#awesomeindianthings"},{"name":"#officeformac","query":"#officeformac"},{"name":"Justin
Bieber","query":"\"Justin Bieber\""},{"name":"National
Margarita","query":"\"National Margarita\""}]}}

我的代码:

WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential(this.Auth.UserName, this.Auth.Password);
string res = wc.DownloadString(new Uri(link));
//the download string gives me the above JSON string - no problems
Trends trends = new Trends();
Trends obj = Deserialise<Trends>(res);


private T Deserialise<T>(string json)
{
T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
DataContractJsonSerializer serialiser = new DataContractJsonSerializer(obj.GetType());
obj = (T)serialiser.ReadObject(ms);
ms.Close();
return obj;
}
}


[DataContract]
public class Trends
{
[DataMember(Name = "as_of")]
public string AsOf { get; set; }

//The As_OF value is returned - But how do I get the
//multidimensional array of Names and Queries from the JSON here?
}

最佳答案

我在开发 Twitterizer 时遇到过这个问题。问题是数据集不是传统的面向对象设计。

如果您将其映射为对象,您会看到:

object root  int as_of  object trends    array[object] <date value of as_of>      string query      string name

As you can see, the trend object has a property that's name changes. The name is based on the as_of date value. As such, it can't be automatically deserialized.

My first solution was to use System.Web.Script.Serialization.JavaScriptSerializer.DeserializeObject(). That method returns a hierarchy of weakly typed, nested dictionary instances. I then stepped through the results myself.

internal static TwitterTrendTimeframe ConvertWeakTrend(object value)
{
Dictionary<string, object> valueDictionary = (Dictionary<string, object>)value;
DateTime date = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds((int)valueDictionary["as_of"]);
object[] trends = (object[])((Dictionary<string, object>)valueDictionary["trends"])[date.ToString("yyyy-MM-dd HH:mm:ss")];

TwitterTrendTimeframe convertedResult = new TwitterTrendTimeframe()
{
EffectiveDate = date,
Trends = new Collection<TwitterTrend>()
};

for (int i = 0; i < trends.Length; i++)
{
Dictionary<string, object> item = (Dictionary<string, object>)trends[i];

TwitterTrend trend = new TwitterTrend()
{
Name = (string)item["name"]
};

if (item.ContainsKey("url"))
{
trend.Address = (string)item["url"];
}

if (item.ContainsKey("query"))
{
trend.SearchQuery = (string)item["query"];
}

convertedResult.Trends.Add(trend);
}

return convertedResult;
}

这很丑陋,但它确实有效。

从那以后,我就接受了 Json.NET 的使用,因为它的速度和简单性。

关于c# - C#.net 中的 JSON Twitter 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2352536/

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