0

一直试图弄清楚如何从twitter 搜索页面中解析出“in_reply_to_status_id_str -> id_str” :

https://twitter.com/phoenix_search.phoenix?q=hello&headers%5BX-Twitter-Polling%5D=true&headers%5BX-PHX%5D=true&since_id=203194965877194752&include_entities=1&include_available_features=1&contributor_details=true&mode=relevance&query_source=unknown

任何人都可以写一个小例子来展示它是如何完成的?

4

5 回答 5

1

使用Json.Net

dynamic jObj = JsonConvert.DeserializeObject(new WebClient().DownloadString("your url"));
foreach (var item in jObj.statuses)
{
    Console.WriteLine("{0} {1}", item.in_reply_to_status_id_str, item.id_str);
}
于 2012-05-17T20:02:39.340 回答
1

所以这里是我拉我的 Json 的地方,这是我的清单的制作地点,你们都准备好了:

public JsonResult AllStatuses() //from the json called in the _client view
    {
        var buildStatuses = new List<BuildStatus>();
        var projects = Client.AllProjects();           

        foreach (var project in projects)
        {                
            try 
            {
                var buildConfigs = Client.BuildConfigsByProjectId(project.Id);

                foreach (var buildConfig in buildConfigs)
                {
                    var b = new BuildStatus();
                    var build = Client.LastBuildByBuildConfigId(buildConfig.Id);
                    var status = build.Status; // Used to loop through BuildConfigID's to find which is a FAILURE, SUCCESS, ERROR, or UNKNOWN

                    var change = Client.LastChangeDetailByBuildConfigId(buildConfig.Id); // Provides the changeID
                    var changeDetail = Client.ChangeDetailsByChangeId(change.Id); // Provides the username, this one populates the usernames

                    if (changeDetail != null)
                        b.user = changeDetail.Username;

                    b.id = buildConfig.Id.ToString();

                    // If the date isn't null place the start date in long format
                    if (build.StartDate != null)
                        b.date = build.StartDate.ToString();

                    // If block; set the status based on the BuildconfigID from the var status
                    if (status.Contains("FAILURE")){
                        b.status = "FAILURE";
                    }
                    else if (status.Contains("SUCCESS")){
                        b.status = "SUCCESS";
                    }
                    else if (status.Contains("ERROR")){
                        b.status = "ERROR";
                    }
                    else{
                        b.status = "UNKNOWN";
                    }
                    buildStatuses.Add(b);
                }

            } catch { }

        }
        var query = buildStatuses.OrderBy(x => x.status); // Create a sorted list from Error - Unknown               

        return Json(query, JsonRequestBehavior.AllowGet);

然后我复制了我也链接你的 JsonConverter。

在我的网站上,我终于拆开了 Json 的列表。

 public JsonResult AllStatuses() //from the json called in the _client view
    {
        List<Client> clients = storeDB.Clients.Include("Projects").Include("Projects.Builds").ToList();
        var buildStatuses = new List<BuildStatus>();

        foreach (var client in clients) {
            // Network credentials
            // Used to get the Json Service request                         // URL here: client.ClientURL
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:81/Status/AllStatuses");
            var response = request.GetResponse();
            var reader = new StreamReader(response.GetResponseStream());
            var responseString = reader.ReadToEnd();

            var serializer = new JavaScriptSerializer();
            serializer.RegisterConverters((new[] { new DynamicJsonConverter() }));
            dynamic obj = serializer.Deserialize(responseString, typeof(object)) as dynamic;

            foreach (var objects in obj) // Pull apart the dynamic object
            {
                var id = objects.id;
                var status = objects.status;
                var date = objects.date;
                var user = objects.user;

                var bs = new BuildStatus();
                try
                {
                    bs.status = status;
                    bs.date = date;
                    bs.id = id;
                    bs.user = user;
                }
                catch { throw; }
                buildStatuses.Add(bs);
            }
        }              

        return Json(buildStatuses, JsonRequestBehavior.AllowGet);
    }
于 2012-05-17T20:05:56.570 回答
0

使用 jQuery 方法:

var obj = jQuery.parseJSON(jsonString);
alert(obj.in_reply_to_status_id_str.id_str);
于 2012-05-17T19:56:56.457 回答
0

您可以使用json 库来完成此操作。

于 2012-05-17T19:56:59.543 回答
0

添加对 System.Runtime.Serialization 的引用后,您还可以使用 .NET 中可用的 DataContractJsonSerializer 类。

您需要做的就是创建两个 DataContract 类。就像是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace MyNamespace
{
   [DataContract]
   public class TwitterObject
   {
      [DataMember(Name = "statuses")]
      public TwitterStatus[] Statuses { get; set; }
   }

   [DataContract]
   public class TwitterStatus
   {
       [DataMember(Name = "in_reply_to_status_id_str")]
       public string InReplyToStatusIdStr { get; set; }

       [DataMember(Name = "id_str")]
       public string IdStr { get; set; }
   }
}

然后从您希望的任何其他方法中,您只需使用 DataContractJsonSerializer 将您的 JSON 构建到 .NET 对象中:

DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(TwitterObject));

// assume the twitterResponse is the JSON you receive
MemoryStream memoryStream = new MemoryStream(Encoding.ASCII.GetBytes(twitterResponse));

var twitterJson = jsonSerializer.ReadObject(memoryStream) as TwitterObject;

可能有一些错别字,但这应该会给你提示。我目前正在研究服务器应用程序和网站之间的广泛同步,这是我目前用于两者之间的 JSON 通信的方法。我发现 DataContracts 和 DataContractJsonSerializer 的组合比 3rd 方库更易于使用。

于 2012-05-17T20:11:51.963 回答