C# 如何在C中解码JSON#

C# 如何在C中解码JSON#,c#,json,C#,Json,我正在尝试使用NewtonSoft.JSON中的JSONConvert对从twitter流式API接收到的JSON进行解码 我创建这个类是为了模拟twitter状态对象 class tweeter { public string geo { get; set; } public string text { get; set; } public string name { get; set; } public string screen_name { get; se

我正在尝试使用NewtonSoft.JSON中的JSONConvert对从twitter流式API接收到的JSON进行解码

我创建这个类是为了模拟twitter状态对象

class tweeter
{
    public string geo { get; set; }
    public string text { get; set; }
    public string name { get; set; }
    public string screen_name { get; set; }
    public string created_at { get; set; }
    public string id { get; set; }

}
然后在我的代码中,我执行以下操作来解码它

try
              {
                  System.Threading.Interlocked.Increment(ref linesRead);
                  var des = JsonConvert.DeserializeObject<tweeter>(e.Line);
                  listBox1.Items.Add(e.Line);

                  dbhelper.insertTweet(des.id,des.screen_name,des.text,des.created_at,"","","", com);

                  listBox1.SelectedIndex = listBox1.Items.Count - 1;

                  listBox1.SelectedIndex = -1;
                  label1.Text = string.Format("Lines Read: {0:F0}", linesRead);
              }
              catch (Exception x)
              {
                  MessageBox.Show(x.Message);
              }
试试看
{
系统。螺纹。联锁。增量(参考线READ);
var des=JsonConvert.DeserializeObject(e.Line);
列表框1.Items.Add(e.Line);
dbhelper.insertTweet(des.id、des.screen\u name、des.text、des.created\u位于“,”,“,”,com);
listBox1.SelectedIndex=listBox1.Items.Count-1;
listBox1.SelectedIndex=-1;
label1.Text=string.Format(“行读取:{0:F0}”,行读取);
}
捕获(异常x)
{
MessageBox.Show(x.Message);
}
但是,嵌套元素(例如screen_name)总是返回null,如何访问这些嵌套元素

其他根元素(如geo、text、created_at、id)正常返回并具有值

ُ编辑:这是一个来自feed
{“in#reply_to_status_id:”null,“favorited:”false,“text:”Haa我在跟我想的每个人开玩笑!在#ibiza2012和#埃及需要它,“#in#reply_to_to_status_id_str null:”null,“实体”:{“hashtags:”{“text:”ibiza2012”,“index:[44,54]},{“text:”埃及”,“index:geo:[59,65],“url:”,“用户提到”],[},“retweet\u count”:0,“place”:null,“truncated”:false,“created\u at”:“Wed Mar 14 15:14:47+0000 2012”,“in\u reply\u to\u user\u id\u str”:null,“source”:“\u003Ca href=\”http:\/\/twitter.com\/\\!\/download\/iphone\”rel=\\“nofollow\”\u003ETwitter for iphone\u003C\/a\u003E”,“retweeted”:false,“in\u reply\u to\u屏幕名称”:null,“坐标”;“reply\u用户id”用户:{“贡献者启用”:false,“地理启用”:false,“个人资料侧边栏”填充颜色:{“DDEEF6”,“lang”:“en”,“列出的数量”:0,“创建时间”:“Sat Apr 11 17:12:45+0000 2009”,“个人资料侧边栏”边框颜色“:“C0Dect”,“is_translator”:false,“显示所有内联媒体”:true,“跟随请求发送”:null,“验证”:false,“url”:null,“描述”:Ibiza 2012的开场白!祝你一鸣惊人!,“默认配置文件”:true,“following”:null,“profile\u use\u background\u image”:true,“时区”:null,“profile\u text\u color”:“333333333”,“status\u count”:2513,“通知”:null,“profile\u background\u image\u url\u https”:“https:\/\/si0.twimg.com\/images\/themes\/theme1\/bg.png”,“profile\u background\u image\u-image\u-url”:http:\/\/a0.twimg.com\/images\/theme1\/bg.png,“地点”:“伦敦”,“个人资料链接颜色”:“0084B4”,“关注者计数”:350,“受保护”:false,“个人资料图片url\/https”:“https:\/\/si0.twimg.com\/profile图片\/1895048740\/image\/u normal.jpg”,“个人资料图片url:“http:\/\/a0.twimg.com\/profile图片\/1895048740\/image\/u normal.jpg”默认配置文件图像:false,“屏幕名称”:“wyatt69”,“姓名”:“Dean Wyatt”,“收藏夹计数”:0,“配置文件背景颜色”:“C0Dect”,“id”:30482455,“id”str:“30482455”,“配置文件背景瓷砖”:false,“utc偏移量”:null,“好友计数”:384},“id”:179948698397196288,“id”str:“179948698397196288”,“贡献者”:null}
d:false,“profile\u image\u url\u https:“https:\/\/si0.twimg.com\/profile\u images\/1673331511\/logo-for-twitter\u normal.png”,“profile\u image\u url:“http:\/\/a0.twimg.com\/profile\u images\/1673331511\/logo-for-twitter\u normal.png”,“默认配置文件图像”:false,“屏幕名称”:“itwitex”,“名称”:“itwitex”,“收藏夹”:0,“profile\u背景色”:“BEB”,“id”:307251688,“id_str”:“307251688”,“profile_background_tile”:false,“utc_offset”:3600,“friends_count”:0},“id”:179948696308432896,“可能敏感的”可编辑:true,“可能敏感的”:false,“id_str”:“179948696308432896”,“贡献者”:null}

为什么不直接使用
JavaScriptSerializer反序列化json。反序列化
System.Web.Script.Serialization
命名空间中的方法:


为什么不使用
JavaScriptSerializer反序列化json。在
System.Web.Script.Serialization
命名空间中反序列化
方法:


最近,我不得不为应用程序的一些内部测试做一些类似的事情。我用类似于以下的方法处理了这种情况:

[DataContract]
class Tweeter
{
    [DataMember]
    public string geo {get;set;}
    [DataMember]
    public User user {get;set;}
}

[DataContract]
class User
{
    [DataMember]
    public string screen_name {get;set;}
}

class Converter
{
    public Tweeter ConvertJson(string json)
    {
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Tweeter));

        using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
        {
            return serializer.ReadObject(ms) as Tweeter;
        }
    }
}

我最近不得不对一个应用程序进行一些类似的内部测试。我用类似于以下的方法处理了这种情况:

[DataContract]
class Tweeter
{
    [DataMember]
    public string geo {get;set;}
    [DataMember]
    public User user {get;set;}
}

[DataContract]
class User
{
    [DataMember]
    public string screen_name {get;set;}
}

class Converter
{
    public Tweeter ConvertJson(string json)
    {
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Tweeter));

        using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
        {
            return serializer.ReadObject(ms) as Tweeter;
        }
    }
}


添加你从twitter获得的JSON数据可能重复。你能从该提要提供JSON示例吗?你的示例中是JSON.NET吗?你应该解释你当前的方法。你的第一个问题是嵌套数据,因此Adam Gritt的答案是你最好的选择。添加JSON数据可能重复你可以从twitter上获取。你能从那个提要中提供JSON示例吗?你的示例中是JSON.NET吗?你应该解释一下你当前的方法。你的第一个问题是嵌套数据,所以Adam Gritt的答案是你最好的选择。很抱歉,我目前没有使用.NET 4.0的权限=(那么按照Adam Gritt的答案使用DataContractSerializer可能是你最好的选择。很抱歉,我目前没有使用.net 4.0的权限=(那么,按照Adam Gritt的答案使用DataContractSerializer可能是你最好的选择。这是在.net 4中吗?因为我只能使用.net 3.5我的代码是在.net 4中编写的,但序列化程序在.net 3.5中受支持--Visual studio报告说它找不到DataContractor,因此你需要添加对System.Runtime.Serialization.dll的引用。)关于最后一个类,我觉得它应该是一个方法,可能是错误的,因为这是在.net 4中吗?因为我只能使用.net 3.5我的代码是在.net 4中编写的,但序列化程序在.net 3.5中受支持--Visual studio报告说它找不到DataContractor,因此您需要添加对System.Runtime.Serialization.dllare的引用你在谈论最后一节课吗?对我来说,这似乎应该是一种方法,但可能是错误的