Deserialize json string array to string array

Asked

Viewed 1,516 times

2

Hello, I’m having trouble deserializar 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 the 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 makes the Deserialization wrong

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

Esse é o Resultado, mas eu quero o valor dentro do name e do value

This is the Output, but I want the value within the name and value

  • is not a list? you have to make a loop to get the values

  • So, but I need first managed to get Jsonstring into a list structure with name and value to run a Loop

2 answers

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 above case, Properties.resources.example has the JSON you reported, and json2 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 list value so.:

var key = lista[i].Dados?[j].Dados[k]?.Name;
var value = lista[i].Dados?[j].Dados[k]?.Value;

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 , all organized in 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.