JSON Class Construction and Consumption

Asked

Viewed 293 times

0

I’m trying to consume a JSON, but in the structure I developed the code does not return data. Please, could you help me and explain the concept of consumption and how the JSON structure works?

Code I developed, does not return data from Lomadeelink.

 var json = new System.Net.WebClient().DownloadString("http://bws.buscape.com.br/service/createLinks/lomadee/7569446956636c7142716f3d/BR/?sourceId=28777832&format=json&link1=http://www.walmart.com.br/produto/Telefonia/Smartphones/Motorola/471928-motorola-moto-g-2-geracao-dtv-colors-preto-tv-digital-dual-chip-quad-core-16gb-2-capas-colori&link2=http://www.walmart.com.br/produto/Telefonia/Smartphones/Motorola/471928-motorola-moto-g-2-geracao-dtv-colors-preto-tv-digital-dual-chip-quad-core-16gb-2-capas-colori");

 LomadeeLinks produtos = Newtonsoft.Json.JsonConvert.DeserializeObject<LomadeeLinks>(json.ToString());

I created the classes this way because JSON returns irrelevant data, I only selected the relevant data that are included in the class.

    public class LomadeeLinks {

        public List<LomadeeLink> lomadeelinks { get; set; }

    }

    public class LomadeeLink {

        public int id { get; set; }
        public int code { get; set; }
        public string originallink { get; set; }
        public string redirectlink { get; set; }

    }

JSON

{
  "totallooseoffers": 0,
  "details": {
    "date": {
      "valid": true,
      "eonandyear": {
        "lowestsetbit": 0
      },
      "hour": 10,
      "month": 8,
      "year": 2015,
      "timezone": -180,
      "millisecond": 360,
      "xmlschematype": {
        "prefix": "",
        "localpart": "dateTime",
        "namespaceuri": "http://www.w3.org/2001/XMLSchema"
      },
      "day": 25,
      "minute": 37,
      "second": 14
    },
    "code": 0,
    "elapsedtime": 1,
    "applicationid": "7569446956636c7142716f3d",
    "message": "success",
    "status": "success"
  },
  "lomadeelinks": [
    {
      "lomadeelink": {
        "originallink": "http://www.walmart.com.br/produto/Telefonia/Smartphones/Motorola/471928-motorola-moto-g-2-geracao-dtv-colors-preto-tv-digital-dual-chip-quad-core-16gb-2-capas-colori",
        "code": 0,
        "id": 1,
        "redirectlink": "http://links.lomadee.com/ls/OUlzRDtMeThBa2FJaTsyODc3NzgzMjswOzY1ODM7MDs1NTc2O0JSOzM7aHR0cCUzQSUyRiUyRnd3dy53YWxtYXJ0LmNvbS5iciUyRnByb2R1dG8lMkZUZWxlZm9uaWElMkZTbWFydHBob25lcyUyRk1vdG9yb2xhJTJGNDcxOTI4LW1vdG9yb2xhLW1vdG8tZy0yLWdlcmFjYW8tZHR2LWNvbG9ycy1wcmV0by10di1kaWdpdGFsLWR1YWwtY2hpcC1xdWFkLWNvcmUtMTZnYi0yLWNhcGFzLWNvbG9yaTswOzA-.html"
      }
    },
    {
      "lomadeelink": {
        "originallink": "http://www.walmart.com.br/produto/Telefonia/Smartphones/Motorola/471928-motorola-moto-g-2-geracao-dtv-colors-preto-tv-digital-dual-chip-quad-core-16gb-2-capas-colori",
        "code": 0,
        "id": 2,
        "redirectlink": "http://links.lomadee.com/ls/aH5haTtDU0VPc2U2ZTsyODc3NzgzMjswOzY1ODM7MDs1NTc2O0JSOzM7aHR0cCUzQSUyRiUyRnd3dy53YWxtYXJ0LmNvbS5iciUyRnByb2R1dG8lMkZUZWxlZm9uaWElMkZTbWFydHBob25lcyUyRk1vdG9yb2xhJTJGNDcxOTI4LW1vdG9yb2xhLW1vdG8tZy0yLWdlcmFjYW8tZHR2LWNvbG9ycy1wcmV0by10di1kaWdpdGFsLWR1YWwtY2hpcC1xdWFkLWNvcmUtMTZnYi0yLWNhcGFzLWNvbG9yaTswOzA-.html"
      }
    }
  ],
  "totalpages": 1,
  "page": 1,
  "totalresultsavailable": 2,
  "totalresultsreturned": 2
}
  • 1

    Have you checked if the data is coming ok? Isolate the problem. As you are using an out-of-the-pattern library, it is more difficult to help. It is more complicated not knowing the format of the return. But it seems to be right, the error seems to be the return of the API. The only thing that might be wrong with your code is not having these fields on the return or it doesn’t see a list of data. I find it strange that you use a ToString() into something that already is string. But this won’t cause any trouble.

  • The data is being received. I believe the problem lies in the structure of my classes. The return can be viewed by accessing the json variable link. About Tostring(), really, it is unnecessary. I removed!

  • You can already see that the class is wrong. You want to keep in a list what is not a list. I don’t even know how this decoder handles it but the return has lists inside it, but it’s not a list. A list (a array) in JSON is represented by something in brackets [ ]. What is returned is very different from what you have in your class as well. It is returning several "classes". From what I understand you want to take only one of them. I don’t know if this library has any facilitator but it is certain that you will have to filter only what you want, via classes or directly in JSON.

  • @bigown Just an aside: the library he is using (Newtonsoft.Json) is already starting to be more standard than the library itself DataContractJsonSerializer which comes with the .NET. Even ASP.NET itself already uses Newtonsoft.Json by default.

  • @dcastro I had never used and did not know it was so used. But it does not change the rest.

  • @bigown Nao, the comment about the structural error is correct, that’s why it was just a comment, an aside ;)

  • Thanks guys, I got it with the @Tobymosque reply.

Show 2 more comments

1 answer

2


Note that you have a title for your Lomadeelink object. as you have not specified anything in your model, Json.NET will interpret it as a property.

{
  "lomadeelinks": [
    {
      "lomadeelink": {
        "originallink": "http://www.walmart.com.br/produto/Telefonia/Smartphones/Motorola/471928-motorola-moto-g-2-geracao-dtv-colors-preto-tv-digital-dual-chip-quad-core-16gb-2-capas-colori",
        "code": 0,
        "id": 1,
        "redirectlink": "http://links.lomadee.com/ls/OUlzRDtMeThBa2FJaTsyODc3NzgzMjswOzY1ODM7MDs1NTc2O0JSOzM7aHR0cCUzQSUyRiUyRnd3dy53YWxtYXJ0LmNvbS5iciUyRnByb2R1dG8lMkZUZWxlZm9uaWElMkZTbWFydHBob25lcyUyRk1vdG9yb2xhJTJGNDcxOTI4LW1vdG9yb2xhLW1vdG8tZy0yLWdlcmFjYW8tZHR2LWNvbG9ycy1wcmV0by10di1kaWdpdGFsLWR1YWwtY2hpcC1xdWFkLWNvcmUtMTZnYi0yLWNhcGFzLWNvbG9yaTswOzA-.html"
      }
    },
    {
      "lomadeelink": {
        "originallink": "http://www.walmart.com.br/produto/Telefonia/Smartphones/Motorola/471928-motorola-moto-g-2-geracao-dtv-colors-preto-tv-digital-dual-chip-quad-core-16gb-2-capas-colori",
        "code": 0,
        "id": 2,
        "redirectlink": "http://links.lomadee.com/ls/aH5haTtDU0VPc2U2ZTsyODc3NzgzMjswOzY1ODM7MDs1NTc2O0JSOzM7aHR0cCUzQSUyRiUyRnd3dy53YWxtYXJ0LmNvbS5iciUyRnByb2R1dG8lMkZUZWxlZm9uaWElMkZTbWFydHBob25lcyUyRk1vdG9yb2xhJTJGNDcxOTI4LW1vdG9yb2xhLW1vdG8tZy0yLWdlcmFjYW8tZHR2LWNvbG9ycy1wcmV0by10di1kaWdpdGFsLWR1YWwtY2hpcC1xdWFkLWNvcmUtMTZnYi0yLWNhcGFzLWNvbG9yaTswOzA-.html"
      }
    }
  ]
}

in this case the best thing to do is to add a different Formatter, in case the Rootformatter.Cs and add the following line on startup of your program or application.

GlobalConfiguration.Configuration.Formatters.Insert(0, new RootFormatter());

finally mark your field with the appropriate title:

[JsonObject(Title = "lomadeelink")]
public class LomadeeLink 
{
    public int id { get; set; }
    public int code { get; set; }
    public string originallink { get; set; }
    public string redirectlink { get; set; }

}

its second option is to copy the Json structure in its entirety:

public class LomadeeList
{
    [JsonProperty(PropertyName = "lomadeelinks")]
    public List<LomadeeWrapper> ListOfLomadeeWrapper { get; set; }
}

public class LomadeeWrapper
{
    [JsonProperty(PropertyName = "lomadee")]
    public Lomadee Lomadee { get; set; }
}

public class Lomadee
{
    [JsonProperty(PropertyName = "id")]
    public int Id { get; set; }
    [JsonProperty(PropertyName = "code")]
    public int Code { get; set; }
    [JsonProperty(PropertyName = "originallink")]
    public string OriginalLink { get; set; }
    [JsonProperty(PropertyName = "redirectlink")]
    public string RedirectLink { get; set; }
}

Tip: You do not need and should not abandon Naming Guidelines, then use the attribution JsonProperty to map your class.

  • I agree 100% with the tip at the end. I think naming conventions are often undervalued.

  • It worked, but I had to change the Naming Guideline from [Jsonproperty(Propertyname = "lomadee")] to [Jsonproperty(Propertyname = "lomadeelink")]. Thank you!

Browser other questions tagged

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