Run web service with user and pass and return a json

0

I need to consume a web service that returns a json and I do not know what to do to run the result since I must store it in a table in the database, could you help me please. I'm executing it in the following way, based on an answer that I found the same here in the forum.

    wsRutas.UltimaPos ws = new wsRutas.UltimaPos();
    ws.Credentials = System.Net.CredentialCache.DefaultCredentials;
    ws.PreAuthenticate = true;
    ws.ultima("user", "pass"); // al llegar a este punto me sale un mesaje que dice la respuesta no es codigo XML correcto

When I execute the method I get a json like that

 {"Geocercas":[{"Description":" Eco 79087 WM-47 53357","Latitude":19.1513000000,"Longitude":-96.1736000000,"Speed":45.62,"UtcTimeCorrected":"2018-03-08T09:20:50","Identifier":"53357"},{"Description":" Eco 93493 WR93 53283","Latitude":19.1684093652,"Longitude":-96.1298141162,"Speed":42.01,"UtcTimeCorrected":"2017-08-15T13:49:01","Identifier":"53283"}

When it reaches the point where I execute the method and I pass the user and password I can get the result

    
asked by tgc65 14.11.2018 в 18:31
source

1 answer

0

If the result is a json you have to parse it using the library json.net, you add it by nuget to your project

Newtonsoft.Json

then later you have to use the page

link

to get the class that maps with the json you have, it could be something like this

public class Geocerca
{
    public string Description { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public double Speed { get; set; }
    public DateTime UtcTimeCorrected { get; set; }
    public string Identifier { get; set; }
}

public class RootObject
{
    public List<Geocerca> Geocercas { get; set; }
}

then you would use

wsRutas.UltimaPos ws = new wsRutas.UltimaPos();

string jsonResult = ws.ultima("user", "pass");

var result = JsonConvert.DeserializeObject<RootObject>(jsonResult );

so you will have the data in a class instance

    
answered by 14.11.2018 / 21:02
source