Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON deserialization nested square brackets

I'm using DataContractJsonSerializer to deserialize the response from /delta in the Dropbox Core API. The response looks like this:

{
  "entries": [
    [
      "/foo.dbf", 
      {
        "bytes": 226822, 
        "client_mtime": "Thu, 26 Sep 2013 23:21:50 +0000", 
        ...
      }
    ], 
    [
      "bar.dbf", 
      {
        "bytes": 147, 
        "client_mtime": "Thu, 26 Sep 2013 23:21:49 +0000", 
        ...
      }
    ],
    ...
  ] 
}

I believe the problem is the nested set of square brackets. The innermost ones contain an array consisting of a string and an object. I'm not sure how to represent that in my C# classes.

I've tried representing "entries" as:

[DataContract]
public class Delta
{    
    [DataMember(Name="entries")]
    public DeltaInfo[] Entries { get; internal set; }
}

and "DeltaInfo" as:

[DataContract]
public class DeltaInfo
{
    [DataMember]
    public string Path { get; internal set; }

    [DataMember]
    public PathInfo MetaData { get; internal set; }
}

While I end up with the expected number of DeltaInfo objects in my Entries array, both Path and MetaData are null in all of the DeltaInfo objects.

I think my problem is that "entries" doesn't really contain an array of DeltaInfo objects, but rather an array of arrays containing a path and metadata, and I'm not sure how to represent that in my code.

like image 836
user3681364 Avatar asked Feb 18 '26 03:02

user3681364


1 Answers

An opening bracket '[' signifies the beginning of a list. Two opening brackets should correspond to a list of lists. The structure would look like this.

[DataContract]
public class Delta
{
    [DataMember(Name="entries")]
    public List<List<DeltaInfo>> entries { get; set; }
}

[DataContract]
public class DeltaInfo
{
    [DataMember]
    public string Path { get; internal set; }

    [DataMember]
    public PathInfo MetaData { get; internal set; }
 }
like image 163
tezromania Avatar answered Feb 20 '26 17:02

tezromania