Renan Barbosa
Renan Barbosa

Reputation: 1084

Correct way to Create Specif format Json

I'm trying to serialize a list of objects to get to the json result below:

 [
  {
        "nolan_dorris": [{
            "created_at": "Mon Sep 24 03:35:21 +0000 2012",
            "profile_link": "https://twitter.com/nolan_dorris",
            "favorite_count": 274,
            "screen_name": "nolan_dorris",
            "followers_count": 951,
            "link": "https://twitter.com/nolan_dorris/status/574184",
            "text": "Use the multi-byte AGP system, then you can calculate t @locaweb ",
            "retweet_count": 0
        }]
    },
    {
        "imogene_kovacek": [{
            "screen_name": "imogene_kovacek",
            "profile_link": "https://twitter.com/imogene_kovacek",
            "created_at": "Mon Sep 24 03:35:21 +0000 2012",
            "favorite_count": 140,
            "followers_count": 735,
            "text": "You can't hack the hard drive without backing up the optical @locaweb ",
            "link": "https://twitter.com/imogene_kovacek/status/823386",
            "retweet_count": 0
        }]
    }
]

My Class

public class TweetDto
{
    public string created_at { get; set; }
    public string profile_link { get; set; }
    public int favorite_count { get; set; }
    public string screen_name { get; set; }
    public int followers_count { get; set; }
    public string link { get; set; }
    public string text { get; set; }
    public int retweet_count { get; set; }
}

Method for Serialize list of TweetDto

 public static string SerializeTweets<T>(T list)
    {
        string json = string.Empty;
        try
        {
            json = JsonConvert.SerializeObject(list, Formatting.Indented);
        }
        catch (Exception) { }
        return json;
    }

Using the NewtonSoft.Json library I tried to perform the serialization, but I can not reach the desired format, indicating as the key of the object the name of the profile in twitter. How can I generate a json file in this format?

Upvotes: 2

Views: 109

Answers (2)

Shaun Luttin
Shaun Luttin

Reputation: 141712

Short Answer

If you start with a List<TweetDto>, then the following operation produces the structure that you asked for in your question.

var mapped = list
    .GroupBy(dto => dto.screen_name)
    .Select(group => new Dictionary<string, List<TweetDto>> 
    {
        { group.Key, group.ToList() }
    });

var json = JsonConvert.SerializeObject(mapped, Formatting.Indented);

Example

To save space, the example uses a subset of the original TweetDto class that you defined. That said it also works with the original TweetDto class.

var list = new List<TweetDto> 
{
    new TweetDto
    {
        screen_name = "nolan_dorris",
        text = "Some text"
    },
    new TweetDto
    {
        screen_name = "nolan_dorris",
        text = "Some other text"
    },
    new TweetDto
    {
        screen_name = "imogene_kovacek",
        text = "Some text"
    }
};

var mapped = list
    .GroupBy(dto => dto.screen_name)
    .Select(group => new Dictionary<string, List<TweetDto>> 
    {
        { group.Key, group.ToList() }
    });

var json = JsonConvert.SerializeObject(mapped, Formatting.Indented);

Console.WriteLine(json);

This is the output, which is the same as the structure that you asked for in your question.

[
  {
    "nolan_dorris": [
      {
        "screen_name": "nolan_dorris",
        "text": "Some text"
      },
      {
        "screen_name": "nolan_dorris",
        "text": "Some other text"
      }
    ]
  },
  {
    "imogene_kovacek": [
      {
        "screen_name": "imogene_kovacek",
        "text": "Some text"
      }
    ]
  }
]

Upvotes: 2

Renan Barbosa
Renan Barbosa

Reputation: 1084

Following the documentation on Json.NET Documentation. I set up the json manually according to my requirement

 var groupedList = users.GroupBy(sn => sn.screen_name).ToList();

        JArray root = new JArray();
        JObject profileGroup = new JObject();

        for (int i = 0; i < groupedList.Count; i++)
        {
            JArray tweets = new JArray();
            JObject tweet;
            foreach (var item in groupedList[i].ToList())
            {
                tweet = new JObject();
                tweet["created_at"] = item.created_at;
                tweet["profile_link"] = item.profile_link;
                tweet["favorite_count"] = item.favorite_count;
                tweet["screen_name"] = item.screen_name;
                tweet["followers_count"] = item.followers_count;
                tweet["link"] = item.link;
                tweet["text"] = item.text;
                tweet["retweet_count"] = item.retweet_count;

                tweets.Add(tweet);
            }
            profileGroup[groupedList[i].Key] = tweets;
        }

Here is the result of my test where the tweets were grouped by screen_nam, in this test the screen_name aaaa had 3 tweets in json, the others only 1.

[
  {
"aaaaa": [
  {
    "created_at": "Mon Sep 24 03:35:21 +0000 2012",
    "profile_link": "",
    "favorite_count": 10,
    "screen_name": "aaaaa",
    "followers_count": 10,
    "link": "",
    "text": "@locaweb 11",
    "retweet_count": 10
  },
  {
    "created_at": "Mon Sep 24 03:35:21 +0000 2012",
    "profile_link": "",
    "favorite_count": 10,
    "screen_name": "aaaaa",
    "followers_count": 10,
    "link": "",
    "text": "@locaweb 22",
    "retweet_count": 10
  },
  {
    "created_at": "Mon Sep 24 03:35:21 +0000 2012",
    "profile_link": "",
    "favorite_count": 10,
    "screen_name": "aaaaa",
    "followers_count": 10,
    "link": "",
    "text": "@locaweb 23",
    "retweet_count": 10
  }
],
"bbbbb": [
  {
    "created_at": "Mon Sep 24 03:35:21 +0000 2012",
    "profile_link": "",
    "favorite_count": 20,
    "screen_name": "bbbbb",
    "followers_count": 20,
    "link": "",
    "text": "@locaweb 22",
    "retweet_count": 20
  }
],
"cccc": [
  {
    "created_at": "Mon Sep 24 03:35:21 +0000 2012",
    "profile_link": "",
    "favorite_count": 30,
    "screen_name": "cccc",
    "followers_count": 30,
    "link": "",
    "text": "@locaweb 33",
    "retweet_count": 30
  }
]
}
]

Upvotes: 0

Related Questions