1

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

enter image description here

I've been following this Deserialize JSON into C# dynamic

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.

Community
  • 1
  • 1
Code Ratchet
  • 5,758
  • 18
  • 77
  • 141
  • You need to change your for loop like this `for (int i = 0; i < data.Count; i++)` – Rahul Nikate Apr 27 '15 at 05:21
  • @RahulNikate totally forgot about the .Count anyways I've put that in now but I now get Additional information: 'System.Array' does not contain a definition for 'Count' – Code Ratchet Apr 27 '15 at 05:30
  • @RahulNikate I've added additional code to my question – Code Ratchet Apr 27 '15 at 05:33
  • Ok so I changed count to length and it worked but when it hit var t = data[i][3].text I got the following error Additional information: The best overloaded method match for 'System.Collections.Generic.Dictionary.this[string]' has some invalid arguments – Code Ratchet Apr 27 '15 at 05:38
  • 1
    Can you show us the JSON you are trying to parse? What JavaScriptSerializer actually returns are 1) `IList` for arrays; `IDictionary` for objects; and an appropriate primitive type for a value. It sounds like your array elements are dictionaries not nested arrays so their properties need to be addressed by name, not index. – dbc Apr 27 '15 at 05:50
  • @dbc changed it to reference the name and it worked. thanks – Code Ratchet Apr 27 '15 at 06:17
  • Any reason not to use typed model and JSON.NET? – Aleksandr Ivanov Apr 27 '15 at 09:18
  • Please select mark your question as answered in case it is. – typie34 Apr 28 '15 at 20:31

1 Answers1

1

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

Json Tree

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/

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

  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; }
    }
    
  2. 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;
    
typie34
  • 358
  • 4
  • 12