How to return a list of a json?

Asked

Viewed 419 times

-2

It follows result:

{"response":{"numFound":2654,"start":0,"docs":[{"id":"l3ade68b8g5db3b0b3","langID":1,"url":"/ministerio-codigo-de-honra/quao-grande.html","title":"Quão grande","band":"Ministério Código de Honra"},{"id":"l3ade68b8geb421fa3","langID":1,"url":"/mattos-nascimento/quao-grande.html","title":"Quão Grande","band":"Mattos Nascimento"},{"id":"l3ade68b8g7acefea3","langID":1,"url":"/andre-valadao/quao-grande-es-tu.html","title":"Quão Grande És Tu","band":"André Valadão"},{"id":"l3ade68b7g0109aea3","langID":1,"url":"/hinario-adventista/quao-grande-es-tu.html","title":"Quão Grande és Tu","band":"Hinário Adventista"},{"id":"l3ade68b8g2d4bfea3","langID":1,"url":"/padre-marcelo-rossi/quao-grande-es-tu.html","title":"Quão Grande Es Tu","band":"Padre Marcelo Rossi"}]},"highlighting":{"l3ade68b8g5db3b0b3":{},"l3ade68b8geb421fa3":{},"l3ade68b8g7acefea3":{},"l3ade68b7g0109aea3":{},"l3ade68b8g2d4bfea3":{}}}

How can I generate a list or array with the result above?

1 answer

1


Following the same logic of the question How to get value from a json? the Json that returns in classes, example:

public class Rootobject
{
    public Response response { get; set; }
    public Highlighting highlighting { get; set; }
}

public class Response
{
    public int numFound { get; set; }
    public int start { get; set; }
    public Doc[] docs { get; set; }
}

public class Doc
{
    public string id { get; set; }
    public int langID { get; set; }
    public string url { get; set; }
    public string title { get; set; }
    public string band { get; set; }
}

public class Highlighting
{
    public L3ade68b8g5db3b0b3 l3ade68b8g5db3b0b3 { get; set; }
    public L3ade68b8geb421fa3 l3ade68b8geb421fa3 { get; set; }
    public L3ade68b8g7acefea3 l3ade68b8g7acefea3 { get; set; }
    public L3ade68b7g0109aea3 l3ade68b7g0109aea3 { get; set; }
    public L3ade68b8g2d4bfea3 l3ade68b8g2d4bfea3 { get; set; }
}

public class L3ade68b8g5db3b0b3
{
}

public class L3ade68b8geb421fa3
{
}

public class L3ade68b8g7acefea3
{
}

public class L3ade68b7g0109aea3
{
}

public class L3ade68b8g2d4bfea3
{
}

Note some classes are open, ie without characteristics of their properties, but, by Json is what can be used.

About invalid characters use Encoding thus:

using (WebClient wc = new WebClient())
{
     wc.Encoding = System.Text.Encoding.UTF8;
}

Using the package Newtonsoft.Json, do:

Rootobject result = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(json);
  • public L3ade68b8g5db3b0b3 it is necessary ?

  • 1

    @Matheusmiranda I did according to the json, but, alas, it’s up to you if you don’t just hide the class Highlighting

  • @Matheusmiranda gives a better explanation in your question, is taking negatives, do the same as other.

  • 1

    @Matheusmiranda just don’t forget to accept as an answer.

Browser other questions tagged

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