Picking up a json string

0

I'm wondering if I'm querying an api and it returns me this:

"{\"optimized\": null, \"optimized_slo\": null, \"normal_slo\": {}, \"normal\": {\"estimated_cost\": \"28.40\", \"distance\": 10198, \"original_eta\": 2236, \"path_suggested_gencoded\": \"tiynClnx{GmBzBwAxCkDfEwAbBlEvExKjL~EfFlCpCiAxAwAhBsAhBmA'BqGfKSd@mA~Cy@xBqA'AYN^h@hBhCzAxBPZpAvCdAbC|JrUhKdVtC|GnArCJPNTvEzKjAvCNjANzA'@zH?ZCPPhDJbBHx@Hf@Fr@FpBl@rKFfATrDVzBLrAP|DRxDGb@Mh@Kp@?v@B'@D^d@tBj@lB^bAnAhDBp@El@y@rDaBtHJbBJt@ThApA|H|@'Gl@rCd@lBr@vCRfA\\rAd@tAxAxDf@|@~AxBP\\Pb@Lp@FbAKlCMjCDv@Jd@Xn@'DrFh@pAZxAjCdOfBdGbBpFf@x@TT^Rh@RpAXv@P\\L\\TZ^Vj@b@nAhBvFlCzHz@'Cj@bAt@rAfA~Bv@tBN'@Bb@@LR'AtAjGTbChA|C\\|@J^D'@Dv@@jAWxQMpG@bBXvCJfAc@GwFaC}BuAe@WOEUC\"}}"

and I wanted to get for example "Normal_SLO: estimated_cost: 28.40" and "Normal: estimated_cost: 20.40" in this example I used only appears slo but have cases that appear 2 different prices, I wanted to know how I could get these values being that their position is not fixed.

Code used to fetch values:

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                   //o texto que estou pegando está armazenado nessa variavel responseText
                    var responseText = streamReader.ReadToEnd();

                return true;
            }
        }
    
asked by anonymous 31.08.2017 / 07:24

1 answer

1

Hello.

Using Newtonsoft.Json, you can do the following:

class Program
{
    static void Main(string[] args)
    {
        string s = "{\"optimized\": null, \"optimized_slo\": null, \"normal_slo\": {}, \"normal\": {\"estimated_cost\": \"28.40\", \"distance\": 10198, \"original_eta\": 2236, \"path_suggested_gencoded\": \"tiynClnx{GmBzBwAxCkDfEwAbBlEvExKjL~EfFlCpCiAxAwAhBsAhBmA'BqGfKSd@mA~Cy@xBqA'AYN^h@hBhCzAxBPZpAvCdAbC|JrUhKdVtC|GnArCJPNTvEzKjAvCNjANzA'@zH?ZCPPhDJbBHx@Hf@Fr@FpBl@rKFfATrDVzBLrAP|DRxDGb@Mh@Kp@?v@B'@D^d@tBj@lB^bAnAhDBp@El@y@rDaBtHJbBJt@ThApA|H|@'Gl@rCd@lBr@vCRfA\\rAd@tAxAxDf@|@~AxBP\\Pb@Lp@FbAKlCMjCDv@Jd@Xn@'DrFh@pAZxAjCdOfBdGbBpFf@x@TT^Rh@RpAXv@P\\L\\TZ^Vj@b@nAhBvFlCzHz@'Cj@bAt@rAfA~Bv@tBN'@Bb@@LR'AtAjGTbChA|C\\|@J^D'@Dv@@jAWxQMpG@bBXvCJfAc@GwFaC}BuAe@WOEUC\"}}";

        DataJsonConvert objecto = Newtonsoft.Json.JsonConvert.DeserializeObject<DataJsonConvert>(s);


    }

    public class DataJsonConvert
    {
        public Normal_SLO normal_slo { get; set; }
        public Normal normal { get; set; }
    }

    public class Normal
    {
        public decimal estimated_cost { get; set; }
    }

    public class Normal_SLO
    {
        public decimal estimated_cost { get; set; }
    }
}

In this way you have the json content in an object.

    
31.08.2017 / 13:57