0

I am pulling twitter data via python from the Twitter API. It is pulling the data back and putting it in a text file. I want to use C# to parse the file but I am having some problems. I am using JsonSerializer to run through it. I have a separate class for the JSON items but as I am creating the class and going through the JSON file I have realized 2 things. Every field in the JSON file needs to be in the class and it needs to be in order. There are over 100 items in this one tweet and it looks like they are dynamic depending on the content. How can I parse the file easily without having to create 100 items in the Twitter class? Below are the code and sample data.

public class TwitterItems
{
}
static void Main(string[] args)
{
    TwitterItems twitList = JsonConvert.DeserializeObject<TwitterItems>(File.ReadAllText(@"C:\Users\soliver\Documents\VS Code\twitter_test\tweets.txt"));
    using (StreamReader file = File.OpenText(@"C:\Users\soliver\Documents\VS Code\twitter_test\tweets.txt"))
    {
        JsonSerializer serializer = new JsonSerializer();
        TwitterItems twitListDeserialized = (TwitterItems)serializer.Deserialize(file, typeof(TwitterItems));
    }
}
Dieter
  • 401
  • 1
  • 9
  • 31
  • 3
    That's going to deserialize the entire file into a single object and is probably not what you want. You probably want to extract individual components like dates and names. You should be using a [`JsonTextReader`](https://stackoverflow.com/questions/43747477/). – Dour High Arch Dec 22 '20 at 23:02
  • "Every field in the JSON file needs to be in the class and it needs to be in order" - Please elaborate. In JSON, property order is not significant. Array element order is, but object properties are not. – Dai Dec 23 '20 at 00:38
  • Where is the `sample data.`? – mjwills Dec 23 '20 at 01:49

1 Answers1

0

You are on the right track that you need to build out a class. You only need to include the fields that you care about from the Twitter tweet data model like in the example below. But, you need to make sure you use JsonProperty and set the name to what Twitter has in their data model

public class Tweet
{
    [JsonProperty("author_id")]
    public string AuthorId {get; set;}
    [JsonProperty("text")] 
    public string Text {get; set;}
}

Then instead of deserializing all the tweets into one data model, you deserialize them into a list of tweets.

var tweets = JsonConvert.DeserializeObject<List<Tweet>>(File.ReadAllText(@"C:\Users\soliver\Documents\VS Code\twitter_test\tweets.txt"));
ejwill
  • 141
  • 8