-2

I am attempting to use an API that returns the following JSON structure:

{
  standardList: [2]
    0:  {
      created: "/Date(1393282097000)/"
      createdBy: "User Name"
      envId: "1234-1234-1234"
      extension: "nbsq"
      id: "4826-5680-3258"
      modified: "/Date(1328572097000)/"
      modifiedBy: "user Name"
      name: "File Name Here"
      officialVer: 1
      size: 1
      syncMod: 3210357275
      url: "https://www.example.com"
      versions: 1
    }-
    1:  {
      created: "/Date(139345635000)/"
      createdBy: "User Name"
      envId: "2345-2345-2345"
      extension: "nbsq"
      id: "4956-4538-4321"
      modified: "/Date(13934425017000)/"
      modifiedBy: "User Name"
      name: "File Name Here"
      officialVer: 1
      size: 1
      syncMod: 2812280243
      url: "https://www.example.com"
      versions: 1
    }
}

I need to deserialize the contents of the standardListand set the id and name values. I have a model:

public class FolderList
{
    [JsonProperty("standardList")]
    public Folder FolderList { get; set; }
}

public class Folder
{ 
  [JsonProperty("envId")]
  public string Id {get; set;}
  [JsonProperty("name")]
  public string Name {get;set;}
}

I tried to do as suggested here and run

JsonConvert.DeserializeObject<FolderList<Folder>>(json);

but it's not working because the response doesn't have a standardList key for each item, it's an array. How do I deserialize into an array?

EDIT: Actual JSON

{"standardList":[{"created":"\/Date(13963113097000)\/","createdBy":"User Name","envId":"1234-1234-1234","extension":"nbsq","id":"5326-0160-3098","modified":"\/Date(1396323497000)\/","modifiedBy":"User Name","name":"File Name Here","officialVer":1,"size":1,"syncMod":32350008075,"url":"https:\/\/www.example.com","versions":1},{"created":"\/Date(1334556017000)\/","createdBy":"User Name","envId":"1234-1234-1234","extension":"nbsq","id":"4728-1586-4633","modified":"\/Date(13933576017000)\/","modifiedBy":"User Name","name":"File Name Here","officialVer":1,"size":1,"syncMod":2822280170,"url":"https:\/\/www.example.com","versions":1}]}
Community
  • 1
  • 1
Alex Haynes
  • 322
  • 3
  • 16
  • 3
    (It's generally better to report/show *actual* JSON, not some rendition of it.) – user2864740 Jun 13 '14 at 16:05
  • 2
    That top thing is not valid JSON. Can you post the actual JSON and not what you copied out of a browser or tool? – Joe Jun 13 '14 at 16:06
  • You deserialize into an array by having an array in your C# Model. e.g.: `public Folder[] FolderList {get;set;}`. – Simon Belanger Jun 13 '14 at 16:07
  • FolderList should contain some sort of `IEnumerable`, not a single folder. – Yuval Itzchakov Jun 13 '14 at 16:25
  • @SimonBelanger I changed my model to use an array. `public Folder[] FolderList {get;set;}` but I'm getting a `System.OverflowException` from the Netwonsoft.Json.dll when I call `JsonConvert.DeserializeObject(json);` – Alex Haynes Jun 13 '14 at 16:32

1 Answers1

3

Go to Json2csharp.com. pass in some valid JSON. Reference the classes generated in your project and then deserialise into them. Lint your JSON first by going to jsonlint. Should work a treat.

brumScouse
  • 3,166
  • 1
  • 24
  • 38
  • 2
    Or in visual studio: Edit -> Paste Special -> Paste as JSON classes – crashmstr Jun 13 '14 at 16:32
  • Thanks! Turns out I had the wrong datatype for the syncMod key, and that was throwing an exception. I never would have noticed without that site. What a great resource. – Alex Haynes Jun 13 '14 at 16:48