正在分析推文中的JSON



我正在尝试使用JObject从Twitter解析C#中的JSON对象,我似乎不知道我需要的结果的起点在哪里。例如:

我需要得到以下:

  • 头像URL
  • Twitter名称
  • 消息

JSON字符串如下所示:

{"completed_in":0.01,"max_id":297026363595042816,"max_id_str":"297026363592042816","page":1,"query":"UOL01","refresh_url":"?since_id=297026363594042816&q=O1",","from_user_name":"Carrie Haworth","geo":null,"id":297026363595042816,"id_str":"297026363595042816","iso_language_code":"nl","metadata":{"result_type":"recent"},"profile_image_url":"http://a0.twimg.com/profile_images/1721499350/5680_216695890261_521090261_7945528_588811_n_normal.jpg","profile_image_url_https":"https://si0.twimg.com/profile_images/1721499350/5680_216695890261_521090261_7945528_588811_n_normal.jpg","source":"<a href="http://twitter.com/"gt;web<a>","text":"Test#01","to_user":null,"to_user_id":0,"to_aser_id_str":"0","to _user_name":null}],"results_per_page":15,"since_id":0,"since _id_str":"0"}

我的假设是,如果我从"results"开始,那么我就可以访问"from_user"等

void DownloadStringCompleted(object senders, DownloadStringCompletedEventArgs e)
    {
        try
        {
            List<TwitterItem> contentList = new List<TwitterItem>();
            JObject ja = JObject.Parse(e.Result);
            int count = 0;
            JToken jUser = ja["results"];
            var name2 = (string)jUser["from_user_name"];
        }catch(Exception e){
         MessageBox.Show("There was an error");
        }
    }

但这似乎正好抓住了例外。有人知道我哪里错了吗?

您的JSON不正确-应该转义元素results[0]["source"]的"

...,"source":"<a href="http://twitter.com/">web</a>","...

此外,ja["results"]是一个阵列。不能使用字符串索引器获取其元素。首先需要获取所需索引上的元素,然后可以访问其from_user_name属性:

JObject ja = JObject.Parse(e.Result);
int count = 0;
JToken jUser = ja["results"][0];
var name2 = (string)jUser["from_user_name"];

相关内容

  • 没有找到相关文章

最新更新