0

I have the following JSON which is considered valid:

{
    "folderList": [
        [
            {
                "folderPath": "/Test",
                "userList": [
                    {
                        "userName": "Test User",
                        "accessType": "Read Only",
                        "startDate": "2.May.2014",
                        "endDate": "3.May.2014"
                    }
                ]
            }
        ]
    ]
}

The problem is as you can see that folderList contains an array that has no name so I'm unable to get it to deserialize properly in MVC. It's pulling the current number of items (in this case, 1) but all values end up being null.

Any ideas?

3 Answers3

1

It's a list containing lists (or an array containing arrays if you prefer that).

// Users that got access to a specific folder item
public class User
{
    public string userName {get; set; }
    public string accessType{get; set; }
    public string startDate {get; set; }
    public string endDate {get; set; }
}

// Inner object
public class FolderListItem
{
    public string folderPath { get; set; }
    public User[] userList { get; set; }
}

// See? List of Lists
public class RootObject
{
    public List<List<FolderListItem>> folderList { get; set; }
}

// Need to have a root object as the folder list is named.
public ActionResult SomeMethod(RootObject data)
{
}
jgauffin
  • 99,844
  • 45
  • 235
  • 372
1

Have a look at this answer.

https://stackoverflow.com/a/3806407/992021

I used it in the past and was able to solve issues where I wasn't in control of the JSON payload I was receiving.

Otherwise, one of the ways this can achieved is through a solution using the JSON Deserializer. This is extremely useful when you only need the values out of a JSON payload and cannot, or do not want to create a actual object to deserialize into.

JSON.net

dynamic jsonObject = JsonConvert.DeserializeObject("{ 'Name': 'Jon Smith', 'Address': {         'City': 'New York', 'State': 'NY' }, 'Age': 42 }");

string name = jsonObject.Name;
string address = jsonObject.Address.City;

Also using Newtonsoft.Json.Linq :

dynamic jsonObject = JObject.Parse("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");

string name = jsonObject.Name;
string address = jsonObject.Address.City;
Community
  • 1
  • 1
Garvice
  • 742
  • 1
  • 9
  • 13
0

you have to create class like

public class Folder
{
 public string folderPath{get;set;}
 public List<USER> userList{get;set;}
}
public class USER
{
public string userName{get;set;}
public string accessType{get;set;}
public string startDate{get;set;}
public string endDate{get;set;}
}
Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70