lidermin
lidermin

Reputation: 2832

Deserialize Json with multiple values

I'm having a hard time trying to deserialize the following Json data with C#:

{folder:{name:'MainFolder',subfolder:[{name:'OneFolder',document:'OneDocument'},{name:'TwoFolder',document:['TwoDocumentA','TwoDocumentB']}]}}

To make it a little more clear, I've created the XML version of the same Json just for visualization purpose:

<folder name="MainFolder">
<subfolder name="OneFolder">
    <document>OneDocument</document>
</subfolder>
<subfolder name="TwoFolder">
    <document>TwoDocumentA</document>
    <document>TwoDocumentB</document>
</subfolder></folder>

So, I guess my problem is that I have two type of documents: A & B as childs of a Subfolder. Here is my code and how I've tried, but this doesn't work (C#):

[Serializable, DataContract(Name = "folder")]
internal class Folder
{
    private string _name;
    private SubFolder[] _subFolder;

    [DataMember(Name = "name")]
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    [DataMember(Name = "subfolder")]
    public SubFolder[] SubFolder
    {
        get { return _subFolder; }
        set { _subFolder = value; }
    }

}

internal class SubFolder
{
    private string _name;
    private string[] _document;

    [DataMember(Name = "name")]
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    [DataMember(Name = "document")]
    public string[] Document
    {
        get { return _document; }
        set { _document = value; }
    }
}

I'm new with desealization of Json data, so please forgive me if I've made huge mistakes, please help me with this.

Thanks in advance.

Upvotes: 1

Views: 1934

Answers (2)

lidermin
lidermin

Reputation: 2832

For anyone interested, I'll answer my own question. Firts, Rex M was right, I needed to include each document on an array, even if it's only one. And there was some useless data on the Json array, so that was the reason I could not deserialize it with the class hierarchy I was using. The right Json would be this one:

{"name":"MainFolder","subfolder":[{"name":"OneFolder","document":["OneDocument"]},{"name":"TwoFolder","document":["TwoDocumentA","TwoDocumentB"]}]}

Second, I was missing some necessary tags on the classes:

[DataContract]
internal class Folder
{
    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMemberAttribute(Name = "subfolder")]
    public List<SubFolder> SubFolder { get; set; }
}

[DataContractAttribute(Name = "subfolder")]
internal class SubFolder
{
    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "document")]
    public List<string> Document { get; set; }
}

With this modifications, the code works fine. Hope it helps somebody.

Upvotes: 0

Rex M
Rex M

Reputation: 144182

It looks like in your JSON, the first subfolder's document member is not an array, but a single string. Instead of document:'OneDocument', it should be document:['OneDocument'].

Upvotes: 1

Related Questions