Multitut
Multitut

Reputation: 2169

How to deserialize a JSON string so I can loop it in C#?

so I am receiving a serialized JSON string through a POST request from a JQuery call:

$('input:checkbox:checked.solPrivChck').each(function () {
    var sThisVal = (this.checked ? $(this).val() : "");
    requestedApprobal.push({ 'PrivId': $(this).attr('id'), 'Fachr': $(this).attr('fachr') });
});
$.post("/Home/RequestPrivilege", { allRequests: JSON.stringify(requestedApprobal) }).success(function () {
    loadTable();
});

The JSON sent looks like this:

{[
  {
    "PrivId": "00005",
    "Fachr": "0039"
  },
  {
    "PrivId": "00006",
    "Fachr": "0039"
  },
  {
    "PrivId": "00007",
    "Fachr": "0039"
  },
  {
    "PrivId": "00010",
    "Fachr": "0039"
  },
  {
    "PrivId": "00005",
    "Fachr": "0039"
  },
  {
    "PrivId": "00006",
    "Fachr": "0039"
  },
  {
    "PrivId": "00007",
    "Fachr": "0039"
  },
  {
    "PrivId": "00010",
    "Fachr": "0039"
  }
]}

This is the C# method that handles that call:

[HttpPost]
public string RequestPrivilege(string allRequests)
{  
    [...]
    //I am trying to map it to a class with the same structure but it fails
    RequestPrivilege allRequestsObj = JsonConvert.DeserializeObject<RequestPrivilege>(allRequests);
    [...]
}

This is my RequestPrivilege class:

class RequestPrivilege {
    public string Fachr { get; set; }
    public string PrivId { get; set; }
}

I need to be able of loop through the JSON elements so I can do some processing but I haven't been able to do that yet.

Thanks!

Upvotes: 3

Views: 724

Answers (3)

Rahul Singh
Rahul Singh

Reputation: 21795

Try this:-

RequestPrivilegeList result = new System.Web.Script.Serialization
                                        .JavaScriptSerializer()
                                        .Deserialize<RequestPrivilegeList>(json);

Here, I have used these Types:-

public class RequestPrivilegeList
{
   public List<RequestPrivilege> data { get; set; }
}

public class RequestPrivilege
{
   public string Fachr { get; set; }
   public string PrivId { get; set; }
}

Tested with sample JSON:-

string json =  @"{""data"":[{""PrivId"": ""00005"", ""Fachr"": ""0039"" },
                 {""PrivId"": ""00006"", ""Fachr"": ""0039"" }]}";

foreach (var item in result.data)
{
    Console.WriteLine("PrivId: {0},Fachr: {1}", item.PrivId, item.Fachr);
}

Upvotes: 1

Dean Ward
Dean Ward

Reputation: 4783

You need to deserialize an array of RequestPrivilege like so:

JsonConvert.DeserializeObject<RequestPrivilege[]>(allRequests);

You will then be able to foreach over it.

Upvotes: 0

Jauch
Jauch

Reputation: 1518

I think this will do the trick.

public class RequestPrivilege
{
    [JsonProperty("Fachr")]
    public string Fachr { get; set; }

    [JsonProperty("PrivId")]
    public string PrivId { get; set; }
}

[HttpPost]
public string RequestPrivilege(string allRequests)
{  
    [...]
    List<RequestPrivilege> allRequestsObj = JsonConvert.DeserializeObject<List<RequestPrivilege>>(allRequests);
    [...]
}

The difference is in the List instead of just RequestPrivilege. Because you have a LIST, not a single object in your json string.

Upvotes: 4

Related Questions