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?

1

2 Answers 2

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; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

You would have to have forSale or something as for-sale is not a valid name for a property.
This approach may cause problems if you want to add extra items.
I'd suggest the use of the JSON serializer in ServiceStack.Text, no need for all those ugly attribute adornments.
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")});

1 Comment

I wouldn't go with this approach when there are easy ways to map JSON on to POCOs.

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.