C#net中的JSON Twitter列表

C#net中的JSON Twitter列表,c#,json,C#,Json,我的代码如下。我无法提取“名称”和“查询”列表 通过DataContracted类从JSON(如下所示) 我花了很长时间试图解决这个问题,我真的可以做到 在一些帮助下 我的Json字符串: {"as_of":1266853488,"trends":{"2010-02-22 15:44:48":[{"name":"#nowplaying","query":"#nowplaying"},{"name":"#musicmonday","query":"#musicmonday"},{"name":"

我的代码如下。我无法提取“名称”和“查询”列表 通过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?
} 
WebClient wc=新的WebClient();
wc.Credentials=新的网络凭据(this.Auth.UserName,this.Auth.Password);
stringres=wc.DownloadString(新Uri(链接));
//下载字符串为我提供了上面的JSON字符串-没有问题
趋势=新趋势();
趋势obj=反序列化(res);
私有T反序列化(字符串json)
{
T obj=Activator.CreateInstance();
使用(MemoryStream ms=new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
DataContractJsonSerializer serialiser=新的DataContractJsonSerializer(obj.GetType());
obj=(T)serialiser.ReadObject(ms);
Close女士();
返回obj;
}
}
[数据合同]
公共阶级趋势
{
[DataMember(Name=“as_of”)]
公共字符串AsOf{get;set;}
//返回值的As_,但是如何获取
//来自JSON的多维名称和查询数组?
} 
考虑以下示例:

public struct TwitterResponse
{
  public int32 as_of;
  public Trend[] Trends;
}

public struct Trends
{
  public String name;
  public String query;
}

Trend[] obj = JavaScriptConvert.DeserializeObject<TwitterResponse>( res ).Trends;
公共结构TwitterResponse { 公共int32作为!; 公共趋势[]趋势; } 公共结构趋势 { 公共字符串名称; 公共字符串查询; } Trend[]obj=JavaScriptConvert.DeserializeObject(res).Trends;
可能需要微调,但这是如何进行微调的一般思路。

您考虑过使用吗

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

如果将其映射为对象,您将看到: object root int as_of object trends array[object] <date value of as_of> string query string name 对象根 整型 对象趋势 数组[对象] 字符串查询 字符串名

如您所见,趋势对象有一个名称更改的属性。该名称基于日期值的“as_”。因此,它不能自动反序列化

我的第一个解决方案是使用System.Web.Script.Serialization.JavaScriptSerializer.DeserializeObject()。该方法返回弱类型嵌套字典实例的层次结构。然后我自己一步一步地完成了结果

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;
}
内部静态TwitterTrendTimeframe ConvertWeakTrend(对象值)
{
字典值字典=(字典)值;
DateTime日期=新的日期时间(1970,1,1,0,0,0).AddSeconds((int)valueDictionary[“as_of”]);
object[]趋势=(object[])((Dictionary)valueDictionary[“趋势”])[date.ToString(“yyy-MM-dd HH:MM:ss”);
TwitterTrendTimeframe convertedResult=新TwitterTrendTimeframe()
{
生效日期=日期,
趋势=新集合()
};
对于(int i=0;i
虽然很难看,但还是奏效了


由于Json.NET的速度和简单性,我已经接受了它的使用。

次要问题:如果你在反序列化方法中添加了新的约束“where T:new()”,你就不需要Activator.CreateInstance位,只需要使用new T()。嗨,这并不能回答问题。我正在寻找如何在C#中实现这一点。我不是问在哪里下载第三方dll。尽管如此,还是要谢谢你。这就是C#的用法。您的意思是如何仅在.Net Framework中执行此操作。(-1)您(以任何方式)都没有指出为什么这样更好,如何使用此功能,以及OP在再次遇到相同问题时应该做什么。此外,建议某人在不了解项目的情况下完全重新开始某项工作通常不会有任何成效。很抱歉,你发现这个答案令人反感,但在存在做同样工作的完美框架的情况下,使用自己的JSON解析似乎是徒劳的。如果James在问这个问题时不知道JSON.net,并且用自定义代码把自己画进了一个角落,这可能会对他有所帮助。就目前情况而言,他对这个答案的回答是,他想在C#中推出自己的,在这种情况下,我的回答是“祝你好运,先生”。(不幸的是,由于我不是通灵者,所以在回答时我不知道这一点)