16
Good afternoon, I decided to ask because I’ve been holding on to this for three days and as much as I’ve looked, I haven’t found a solution to my problem. Access a Web Service via C#, Httpclient, and get a Json in the following format:
{
"games": [
{
"IdJogo": "1",
"Titulo": "No Man's Sky",
"DtaLancamento": "Junho 2016",
"ResumoBR": "Em No Man's Sky você assume o papel de um explorador planetário com uma nave espacial, permitindo-lhes explorar a superfície de vários planetas e interagir com a flora e fauna, e viajar pelo espaço, entrar em combate com forças inimigas e viajar a outros planetas.",
"NomeImg": "NoManSky.jpg"
},
{
"IdJogo": "2",
"Titulo": "Starbound",
"DtaLancamento": "Dezembro 2013",
"ResumoBR": "Starbound é um sandbox/open world, nele você vive em uma espaçonave e pode explorar diferentes mundos, sistemas solares, galáxias, etc. As possibilidades são praticamente infinitas!",
"NomeImg": "Starbound.jpg"
}
I am using Json.net (newtonsoftjson) to convert each "Game" contained in Json to a object with the same attributes existing in Json, but I get the most diverse errors. Currently, I do the conversion in this way:
string jsonR = resposta.Content.ReadAsStringAsync().Result;
foreach (JObject elemento in jsonR)
{
Game game = JsonConvert.DeserializeObject<Game>(elemento.ToString());
}
However, the following error returns to me:
It is not possible to convert an object of type Newtonsoft.Json.Linq.Jvalue into type Newtonsoft.Json.Linq.Jobject
and when I try to put as type Jvalue inside the foreach, I obey the error:
Error Converting value 123 to type 'Wpfapplication1.Game'. Path ",line1 position 3.
I’m sorry for any mistake in posting this way, but I’ve reviewed the Newtonsoft documentation, I’ve reviewed the Google and nothing has helped me.
...<Game>(elemento.ToString());
? This is how it should be?– Jéf Bueno
When I shoot Tostring I already get an error in that line saying that you can’t convert from Newtonsoft.Json.Linq.Jobject to string..
– Fumegz
The Deserializeobject method accepts a Json string as parameter.
– Fumegz
If you already have one
JObject
, you can call the methodToObject<T>
instead of converting it to string and then converting that string to your object.– carlosfigueira