1

My required json format is

{
  "nodes": {
    "1": {
      "attriba": "a1",
      "attribb": "b1",
      "label": "1",
      "attribc": false
    },
    "2": {
      "attriba": "a2",
      "label": "2",
      "attribc": false
    },
    "3": {
      "attriba": "a3",
      "label": "3",
      "attribc": false
    },
    "4": {
      "attriba": "none",
      "label": "4",
      "attribc": false
    },
    "5": {
      "attriba": "none",
      "label": "5",
      "attribc": false
    }
  }
}

Now normally I would create classes and fill them with data and call "Newtonsoft.Json.JsonConvert.SerializeObject" to get the desired json string.

But in this case the format is such that I'm unable to figure out the class structure..

The top class I think would be like the following..

public class Response
    {
        [JsonProperty("nodes")]
        public List<Node> Nodes { get; set; }

     }

The bottom class ..

public class Nodedata
    {

        [JsonProperty("attriba")]
        public string Attriba { get; set; }

        [JsonProperty("attribb")]
        public string Attribb { get; set; }

        [JsonProperty("label")]
        public string Label { get; set; }

        [JsonProperty("attribc")]
        public bool Attribc { get; set; }
    }

But, how do i manage the node class( values "1" to "5") which has no key value..

Any help will be sincerely appreciated..

Thanks

jacobz
  • 3,191
  • 12
  • 37
  • 61
Arnab
  • 2,324
  • 6
  • 36
  • 60
  • 1
    It could be a `Dictionary` where `nodes` is the reference to it? – Jeroen Vannevel Aug 07 '14 at 10:49
  • @JeroenVannevel :Tx.., I just found something similar in http://stackoverflow.com/questions/19581820/how-to-convert-json-array-to-list-of-objects-in-c-sharp?rq=1, – Arnab Aug 07 '14 at 10:57
  • 1
    Here's a little example: https://dotnetfiddle.net/zOLLXl – Jeroen Vannevel Aug 07 '14 at 10:59
  • @JeroenVannevel would appreciate if you had a look at http://stackoverflow.com/questions/25183061/converting-an-object-to-json-in-specified-format-contd - Tx.. – Arnab Aug 07 '14 at 12:55

1 Answers1

0
public class Response
{
    public Dictionary<string, Node> nodes {get;set;}
}

public class Node
{
    public string attriba { get; set; }
    public string attribb { get; set; }
    public string label { get; set; }
    public bool attribc { get; set; }
}
jacobz
  • 3,191
  • 12
  • 37
  • 61