Class Construction and JSON Consumption

0

I'm trying to consume a JSON, but in the structure I've developed the code does not return me data. Could you please help me and explain the concept of consumption and how does the JSON framework work?

Code that I developed, does not return LomadeeLink data.

 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 just select the relevant data that is 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
}
    
asked by anonymous 25.08.2015 / 15:23

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 this case RootFormatter.cs and add the following line at startup of your program or application.

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

Finally, check 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; }

}

Your 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 and should not abandon Naming Guidelines , then use the JsonProperty attribute to map your class.

    
25.08.2015 / 15:56