LeMoussel
LeMoussel

Reputation: 5767

Serialize as array with JSON.NET

How can I make Json.NET serializer to serialize instance into array of objects?

Basically I need something like this:

[
    {
      "name":"Page1.html",
      "size":1,
      "outlinks":[
        "Page2.html",
        "Page3.html",
        "Page4.html"
      ]
    },
    {
      "name":"Page2.html",
      "size":2,
      "outlinks":[
        "Page3.html"
      ]
    },
    {
      "name":"Page3.html",
      "size":3,
      "outlinks":[
        "Page1.html",
        "Page2.html",
        "Page3.html",
        "Page4.html"
      ]
    },
    {
       "name":"Page4.html",
       "size":4,
        "outlinks":[]
    }
]

with:

    Dictionary<string, string[]> UrlsCollection = new Dictionary<string, string[]>();
    List<string> OutLinks = new  List<string>();

    OutLinks.Add("Page2.html");
    OutLinks.Add("Page3.html");
    OutLinks.Add("Page4.html");
    UrlsCollection.Add("Page1.html", OutLinks.ToArray());
    OutLinks.Clear();

    OutLinks.Add("Page3.html");
    UrlsCollection.Add("Page2.html", OutLinks.ToArray());
    OutLinks.Clear();

    OutLinks.Add("Page1.html");
    OutLinks.Add("Page2.html");
    OutLinks.Add("Page3.html");
    OutLinks.Add("Page4.html");
    UrlsCollection.Add("Page3.html", OutLinks.ToArray());
    OutLinks.Clear();

    UrlsCollection.Add("Page4.html", OutLinks.ToArray());
    OutLinks.Clear();

    string jsonUrlsCollection = JsonConvert.SerializeObject(UrlsCollection.ToList(), Formatting.Indented);

I get:

[
  {
    "Key": "Page1.html",
    "Value": [
      "Page2.html",
      "Page3.html",
      "Page4.html"
    ]
  },
  {
    "Key": "Page2.html",
    "Value": [
      "Page3.html"
    ]
  },
  {
    "Key": "Page3.html",
    "Value": [
      "Page1.html",
      "Page2.html",
      "Page3.html",
      "Page4.html"
    ]
  },
  {
    "Key": "Page4.html",
    "Value": []
  }
]

There must be a way/something to get a simple JSON Object?

Modifying the solution to reflect Deblaton Jean-Philippe's suggestion in the comments.

public class UrlDef
{
    public UrlDef() { outlinks = new List<string>();  }
    public string name { get; set; }
    public int size { get; set; }
    public List<string> outlinks { get; set; }
}

    List<UrlDef> UrlsCollection = new List<UrlDef>();

    UrlDef urldef;

    urldef = new UrlDef();
    urldef.name = "Page1.html";
    urldef.size = 1;
    urldef.outlinks.Add("Page2.html");
    urldef.outlinks.Add("Page3.html");
    urldef.outlinks.Add("Page4.html");
    UrlsCollection.Add(urldef);

    urldef = new UrlDef();
    urldef.name = "Page2.html";
    urldef.size = 2;
    urldef.outlinks.Add("Page3.html");
    UrlsCollection.Add(urldef);

    urldef = new UrlDef();
    urldef.name = "Page3.html";
    urldef.size = 3;
    urldef.outlinks.Add("Page1.html");
    urldef.outlinks.Add("Page2.html");
    urldef.outlinks.Add("Page3.html");
    urldef.outlinks.Add("Page4.html");
    UrlsCollection.Add(urldef);

    urldef = new UrlDef();
    urldef.name = "Page4.html";
    urldef.size = 4;
    UrlsCollection.Add(urldef);

    string jsonUrlsCollection = JsonConvert.SerializeObject(UrlsCollection, Formatting.Indented);

Upvotes: 1

Views: 100

Answers (1)

Deblaton Jean-Philippe
Deblaton Jean-Philippe

Reputation: 11388

You need to serialize a list of this object

public class Url
{
    public string name { get; set; }
    public int size { get; set; }
    public List<string> outlinks { get; set; }
}

For this answer, I used a website I often use when I know what I expect from JS, but I'm not sure of what I need to feed into the CS : http://json2csharp.com/

Upvotes: 4

Related Questions