反序列化从Twitter API 1.1 C#返回的推文 [英] Deserialize tweets returned from twitter api 1.1 c#

查看:100
本文介绍了反序列化从Twitter API 1.1 C#返回的推文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我传递一个特定的屏幕名称时,以下内容会返回给我.

The following is returned to me when I pass in a particular screen name.

我一直在关注将JSON反序列化为C#动态

我正在尝试通过执行以下操作来引用文本字段:

I'm trying to reference the text field by doing the following:

       var serializer = new JavaScriptSerializer();

       dynamic data = serializer.Deserialize(tweets, typeof(object));

       for (int i = 0; i < data.Count; i++)
       {
           var t = data[i][3].text;    
       }

显然是错误的,因为我正在获得

Clearly its wrong because I'm getting

Additional information: Operator '<' cannot be applied to operands of type 'int' and 'object[]'

有人可以帮我检索该推文的文本吗?

Can some one help me retrieve the text for the tweet.

推荐答案

如您所见,文本是所接收json的根元素的属性.

As you an see, the text is an attribute of the root element of the json you are receiving.

我建议您使用Json.net库来解析json字符串并将其映射到可以在代码中使用的对象. 您可以在此处下载: https://www.nuget.org/packages/newtonsoft.json /

I would recommend you using the Json.net library for parsing the json string and mapping it to objects you can use in your code. You can download it here: https://www.nuget.org/packages/newtonsoft.json/

为了从json字符串中获取文本,您必须执行以下操作:

In order to get the text from the json string you have to do the following:

  1. 使用所需属性在c#中创建一个类.这是类,根节点的属性将被映射到.为此,您必须在程序中实现要处理的属性(例如文本").

  1. Create a class in c# with the desired attributes. This is the class, the root node's attributes will be mapped to. Do do this, you have to implement the attributes you want to process in your programm ( eg. "text").

class Tweet{
public string created_at { get; set; }
public long id { get; set; }
public string id_str { get; set; }
public string text { get; set; }
public string source { get; set; }
}

  • 现在,您可以使用该库来解析Tweet对象,然后获取文本属性.

  • Now you can use the library to parse the Tweet object and after that get the text attribute.

    var tweet = JsonConvert.DeserializeObject<Tweet>(jsonString);
    var text = Tweet.text;
    

  • 这篇关于反序列化从Twitter API 1.1 C#返回的推文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆