0

Been trying to figure out how to parse out "in_reply_to_status_id_str -> id_str" form the twitter search page:

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

Anyone that could write a small example to show how it can be done?

8
  • You are going to have to get a Dynamic Json Converter, then you will have to loop through each of the objects pulling them apart. Have you tried that? Commented May 17, 2012 at 19:57
  • stackoverflow.com/questions/3142495/… This is a great example Commented May 17, 2012 at 19:58
  • I cant get that to work, tried but getting errors all over the place Commented May 17, 2012 at 19:58
  • What type of errors have you received? I just used this the other day to basically do the same thing except not from twitter but from TeamCity. Commented May 17, 2012 at 19:59
  • Could you post an example? :) Commented May 17, 2012 at 20:01

5 Answers 5

1

Using 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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

how would i print that into a messagebox instead of into the console?
MessageBox.Show(item.in_reply_to_status_id_str.ToString() + " " + item.id_str.ToString());. You may need to check whether item.xxx is null before calling ToString()
1

SO here is where I pull my Json, this is where my list gets made, which you all ready have:

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);

Then I copied the JsonConverter I linked you too.

On my Website I finally pulled apart the list of Json with.

 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);
    }

Comments

0

Go for a jQuery approach:

var obj = jQuery.parseJSON(jsonString);
alert(obj.in_reply_to_status_id_str.id_str);

Comments

0

You can use this json libraryfor accomplish this.

Comments

0

You could also use the DataContractJsonSerializer class available in .NET once you add a reference to System.Runtime.Serialization.

All you need to do is a create two DataContract classes. Something like:

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; }
   }
}

Then from any other method you wish, you just have to use the DataContractJsonSerializer to build your JSON into a .NET object:

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;

There may be some typos, but this should give you the hint. I'm currently working on an extensive synchronization between a server app and a website and this is the method I currently use for JSON communication between the two. I've found the combination of DataContracts and DataContractJsonSerializer is easier to use than 3rd party libraries.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.