1

Hi I am developing web application in c#. I have one JSON object. I am trying to de-serialize the object as below. Below is my result.

 [ {
    "id": "ce053195-ae48-4457-bb88-6ca32f236bd1",
    "model": {
      "objectId": [
        "c760d95e-3fcb-43d1-a7db-111d3384ecbc"
      ],
      "name": [
        "Device1-Configuration"
      ]
    },
    "childs": 0,
    "access": 0,
    "allow": 0
  },
  {
    "id": "42ad8eb6-9f35-447c-8ea0-e1346dee410c",
    "model": {
      "objectId": [
        "c760d95e-3fcb-43d1-a7db-111d3384ecbc"
      ],
      "name": [
        "Device1-DeviceModel"
      ]
    },
    "childs": 1,
    "access": 0,
    "allow": 0
  }
] 

Below are my models.

public partial class Resource
{
    public System.Guid Id { get; set; }

    public Models Model { get; set; }

    public bool Selected { get; set; }

    public bool hasChildren { get; set; }

    public bool IsAllowed { get; set; }
}

Below is my model class.

public partial class Models
{
    public List<string> Name { get; set; }
}

I am trying to assign name array from the models.

foreach (JObject singScopeRes in result)
{
    var permission = new Rbac.BusinessEntities.V2.Resource();
    permission.Id = new Guid(singScopeRes["id"].ToString());
    permission.Model = new Rbac.BusinessEntities.V2.Models()
    {
        Name= singScopeRes["model"]["name"][0]
    };
 }

This is throwing error saying:

cannot implicit convert type newtonsoft.json.linq.jtoken to system.generic.list

Can someone help me to fix this issue?

croxy
  • 4,082
  • 9
  • 28
  • 46
Niranjan
  • 537
  • 2
  • 14
  • 31
  • Why don't you have Visual Studio create your classes? See https://stackoverflow.com/questions/11260631/convert-json-into-class-object-in-c-sharp/48023576#48023576 – Ole EH Dufour Aug 30 '18 at 09:46

2 Answers2

4

Use a tool like json2csharp to generate your model:

public class Model
{
    public List<string> objectId { get; set; }
    public List<string> name { get; set; }
}

public class Resource
{
    public string id { get; set; }
    public Model model { get; set; }
    public int childs { get; set; }
    public int access { get; set; }
    public int allow { get; set; }
}

Now you can deserialize your json like this:

var obj = JsonConvert.DeserializeObject<Resource[]>(json);

If you want to stick with your current code you can do so by casting the JToken while assigning it to the Name property:

foreach (JObject singScopeRes in result)
{
    var permission = new Rbac.BusinessEntities.V2.Resource();
    permission.Id = new Guid(singScopeRes["id"].ToString());
    permission.Model = new Rbac.BusinessEntities.V2.Models()
    {
        Name = (singScopeRes["model"]["name"]).ToObject<List<string>>()
    };
 }
croxy
  • 4,082
  • 9
  • 28
  • 46
  • Thanks. When i convert i am getting error cannot convert from system.collections.generic to string – Niranjan Aug 30 '18 at 09:39
  • 1
    Visual studio can create models as well. Copy your JSON, then in Visual studio Do `Edit -> Paste Special -> Paste JSON as Classes`. Unfortunately it creates arrays instead of lists. – ChristianMurschall Aug 30 '18 at 09:43
  • @Niranjan Are you using this exact model? I cannot reproduce this error. For me the deserialization works like a charm. – croxy Aug 30 '18 at 09:46
  • Yes. But i am getting error cannot convert from system.collections.generic to string – Niranjan Aug 30 '18 at 09:50
  • @Niranjan Are you using the same json to test the deserialization as you provided in your question? – croxy Aug 30 '18 at 09:50
  • Yes exactly same json I am using. – Niranjan Aug 30 '18 at 09:52
  • @Niranjan I do not know why this isn't working for you without seeing how you use the code provided above. See my edit for another solution. – croxy Aug 30 '18 at 10:10
0

You could try to Convert your JSON to an dynamic and then loop over those values as following: dynamic obj = JsonConvert.DeserializeObject(JSON);

Another option is to make your Objects have the same structure as the JSON.

public class Model
{
    public List<string> objectId { get; set; }
    public List<string> name { get; set; }
}

public class Resource
{
    public string id { get; set; }
    public Model model { get; set; }
    public int childs { get; set; }
    public int access { get; set; }
    public int allow { get; set; }
}
svenQ
  • 119
  • 1
  • 1
  • 13
  • Thanks. When i convert i am getting error cannot convert from system.collections.generic to string – Niranjan Aug 30 '18 at 09:46
  • I checked with dynamic also but same error is popping up – Niranjan Aug 30 '18 at 09:46
  • Where do you get this exception. Since if it happens in your foreach then just remove this part of code. Becuase this has been filled in already due to the deserialize done by @croxy his code. – svenQ Aug 30 '18 at 09:59