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
}
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.– Maniero
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!
– Jean Gustavo Prates
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.– Maniero
@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
@dcastro I had never used and did not know it was so used. But it does not change the rest.
– Maniero
@bigown Nao, the comment about the structural error is correct, that’s why it was just a comment, an aside ;)
– dcastro
Thanks guys, I got it with the @Tobymosque reply.
– Jean Gustavo Prates