Deserialize json string array to string array

1

Hello, I'm having trouble deserializing a Json, I'm not getting the right data.

There is the Main Class that would receive the data list

 public class PermissaoDossie 
    {
        [JsonProperty(PropertyName = "data")]
        public string data { get; set; }

        [JsonProperty(PropertyName = "listPermissoes")]
        public string listPermissoes { get; set; }

        [JsonProperty(PropertyName = "dadosPermissao")]
        public string dadosPermissao { get; set; }


        [JsonProperty(PropertyName = "name")]
        public string name { get; set; }

        [JsonProperty(PropertyName = "value")]
        public string value { get; set; }   

    }

And the List Class

        public class RootObject 
    {
        public List<PermissaoDossie> dossie { get; set; }
    }

Here is Json and the process

String JsonString = {"data":{"listPermissoes":[{"dadosPermissao":[{"name":"usuario","value":"ADMINISTRADOR"},{"name":"codigo_usuario","value":"1"},
{"name":"unidade","value":"Unidade qualquer"},{"name":"codigo_unidade","value":"001"},
{"name":"nivel","value":"R"}]},
{"dadosPermissao":[{"name":"usuario","value":"Usuario qualquer"},{"name":"codigo_usuario","value":"23945"},{"name":"unidade","value":"Administração"},{"name":"codigo_unidade","value":"1302"},
{"name":"nivel","value":"M"}]}]},"service":{"name":"ConsultaPermissao"},"message":{"type":"success","value":""}}

//////// Here's the wrong Deserialization

var result = JsonConvert.DeserializeObject<IDictionary<string, RootObject>>(JsonString);

This is the result, but I want the value inside the name and value

    
asked by anonymous 08.08.2017 / 17:01

2 answers

1

I will propose a solution by following the loading of this information into class types, example :

Json:

{
  "data": {
    "listPermissoes": [
      {
        "dadosPermissao": [
          {
            "name": "usuario",
            "value": "ADMINISTRADOR"
          },
          {
            "name": "codigo_usuario",
            "value": "1"
          },
          {
            "name": "unidade",
            "value": "Unidade qualquer"
          },
          {
            "name": "codigo_unidade",
            "value": "001"
          },
          {
            "name": "nivel",
            "value": "R"
          }
        ]
      },
      {
        "dadosPermissao": [
          {
            "name": "usuario",
            "value": "Usuario qualquer"
          },
          {
            "name": "codigo_usuario",
            "value": "23945"
          },
          {
            "name": "unidade",
            "value": "Administração"
          },
          {
            "name": "codigo_unidade",
            "value": "1302"
          },
          {
            "name": "nivel",
            "value": "M"
          }
        ]
      }
    ]
  },
  "service": { "name": "ConsultaPermissao" },
  "message": {
    "type": "success",
    "value": ""
  }
}

Classes:

public class Root
{
    public Service Service { get; set; }
    public Message Message { get; set; }
    public Data Data { get; set; }
}

public class Service
{
    public string Name { get; set; }
}

public class Message
{
    public string Type { get; set; }
    public string Value { get; set; }
}

public class DadosPermissao
{
    public string Name { get; set; }
    public string Value { get; set; }
}

public class Data
{
    public DadosPermissaoArray[] ListPermissoes { get; set; }
}

public class DadosPermissaoArray
{
    public DadosPermissao[] DadosPermissao { get; set; }
}

How to use?

string json = File.ReadAllText("./base.json");
Root result = JsonConvert.DeserializeObject<Root>(json);

within the variable result , has the same information as the , all organized into class objects.

Printing the information on the screen:

System.Console.WriteLine("Service Name: {0}", result.Service.Name);
System.Console.WriteLine("Message Value: {0}", result.Message.Value);
System.Console.WriteLine("Message Type: {0}", result.Message.Type);
System.Console.WriteLine("----------------------------------------------------");
foreach(DadosPermissaoArray item1 in result.Data.ListPermissoes)
{
    foreach(DadosPermissao item2 in item1.DadosPermissao)
    {
        System.Console.WriteLine("DadosPermissao Name: {0}", item2.Name);
        System.Console.WriteLine("DadosPermissao Value: {0}", item2.Value);
        System.Console.WriteLine("");
    }
    System.Console.WriteLine("----------------------------------------------------");
}

Example NET. Fiddle

    
08.08.2017 / 17:59
1

try changing your classes to the following.:

[DataContract]
public class DadoPermissao
{
    [DataMember(Name = "name")]
    public string Name { get; set; }
    [DataMember(Name = "value")]
    public string Value { get; set; }
}

[DataContract]
public class ListaPermissoes
{
    [DataMember(Name = "dadosPermissao")]
    public List<DadoPermissao> Dados { get; set; }
}

[DataContract]
public class ListasPermissoes
{
    [DataMember(Name = "listPermissoes", EmitDefaultValue = false)]
    public List<ListaPermissoes> Dados { get; set; }

    [DataMember(Name = "name", EmitDefaultValue = false)]
    public string Name { get; set; }

    [DataMember(Name = "type", EmitDefaultValue = false)]
    public string Type { get; set; }

    [DataMember(Name = "value", EmitDefaultValue = false)]
    public string Value { get; set; }
}

Then you can serialize / deserialize as follows:

var json = Properties.Resources.exemplo;
var lista = JsonConvert.DeserializeObject<Dictionary<string, ListasPermissoes>>(json);
var json2 = JsonConvert.SerializeObject(lista, Formatting.Indented);

In the case above, the Properties.Resources.example has the JSON you entered, and json2 has the following string.:

{
  "data": {
    "listPermissoes": [
      {
        "dadosPermissao": [
          {
            "name": "usuario",
            "value": "ADMINISTRADOR"
          },
          {
            "name": "codigo_usuario",
            "value": "1"
          },
          {
            "name": "unidade",
            "value": "Unidade qualquer"
          },
          {
            "name": "codigo_unidade",
            "value": "001"
          },
          {
            "name": "nivel",
            "value": "R"
          }
        ]
      },
      {
        "dadosPermissao": [
          {
            "name": "usuario",
            "value": "Usuario qualquer"
          },
          {
            "name": "codigo_usuario",
            "value": "23945"
          },
          {
            "name": "unidade",
            "value": "Administração"
          },
          {
            "name": "codigo_unidade",
            "value": "1302"
          },
          {
            "name": "nivel",
            "value": "M"
          }
        ]
      }
    ]
  },
  "service": {
    "name": "ConsultaPermissao"
  },
  "message": {
    "type": "success",
    "value": ""
  }
}

You can access the value of the list like this:

var key = lista[i].Dados?[j].Dados[k]?.Name;
var value = lista[i].Dados?[j].Dados[k]?.Value;
    
08.08.2017 / 17:39