开发者

Deserialize Json with multiple values

开发者 https://www.devze.com 2023-02-06 21:30 出处:网络
I\'m having a hard time trying to deserialize the following Json data with C#: {folder:{name:\'MainFolder\',subfolder:[{name:\'OneFolder\',docume开发者_如何学JAVAnt:\'OneDocument\'},{name:\'TwoFolder

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

{folder:{name:'MainFolder',subfolder:[{name:'OneFolder',docume开发者_如何学JAVAnt:'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.


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'].


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消