Picking up a json string

Asked

Viewed 242 times

0

I’m having doubts I’m consulting 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 take for example "Normal_slo : estimated_cost : 28.40" and "Normal : estimated_cost : 20.40" in this example that I used appears only the It but has cases that appears 2 different prices, I wanted to know how I could take these values being that their position is not fixed.

Code used to pick up 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;
            }
        }

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; }
    }
}

This way you have the json content in an object.

  • Perfect, thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.