1

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);
2
  • don't use a Dictonary. Commented Feb 4, 2015 at 17:45
  • What you get already is an array of objects. The objects are not what you expect them to be. Commented Feb 4, 2015 at 17:46

1 Answer 1

4

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/

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Jean-Philippe. I updated my post following your advice. I did not know this site. This helps a lot!
Is there still a question that you updated your post?

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.