0

I would like to get next structure in JSON:

{
    'for-sale': { name: 'For Sale', type: 'folder' },
    'vehicles': { name: 'Vehicles', type: 'folder' },
    'rentals': { name: 'Rentals', type: 'folder' },
}

I can use JavaScriptSerializer to convert .net class to JSON format. But witch structure my class must have for above sample data?

Polaris
  • 3,643
  • 10
  • 50
  • 61
  • Not an exact duplicate, but take a look at [JavaScriptSerializer.Deserialize - how to change field names](http://stackoverflow.com/questions/1100191/javascriptserializer-deserialize-how-to-change-field-names). – CodeCaster Jul 05 '13 at 11:59

2 Answers2

3

Have you tried this:

public class MyClass
{
    [JsonProperty("for-sale")]
    public IList<Item> forSale { get; set; }
    public IList<Item> vehicles { get; set; }
    public IList<Item> rentals { get; set; }
}

public class Item
{
    public string name { get; set; }
    public string type { get; set; }
}

You could also use the DataContractJsonSerializer and instead you would use the following classes:

[DataContract]
public class MyClass
{
    [DataMember(Name = "for-sale")]
    public IList<Item> forSale { get; set; }
    [DataMember]
    public IList<Item> vehicles { get; set; }
    [DataMember]
    public IList<Item> rentals { get; set; }
}

[DataContract]
public class Item
{
    [DataMember]
    public string name { get; set; }
    [DataMember]
    public string type { get; set; }
}
Guilherme Duarte
  • 3,371
  • 1
  • 28
  • 35
0

Looks like you need a

var theStructure = Dictionary<string, Dictionary<string,string>>()

and add data to it like:

theStructure.Add("for-sale", new Dictionary<string, string>() {("For Sale", "folder")});
Kamil T
  • 2,232
  • 1
  • 19
  • 26