locked
json format to c#.Net Code RRS feed

  • Question

  • User-410257276 posted

    Dear Friends

                       This is the code. I am getting Diesel rate through API . And i have sample code, i Don't know how to implement this code in my C#.net Code Like in my Page load

     Task<HttpResponse<MyClass>> response = Unirest.get("https://newsrain-petrol-diesel-prices-india-v1.p.mashape.com/state?state=Tamil+Nadu")
            .header("X-Mashape-Key", "VZznHLmshqH7p1Ka9DrjsnVHOyyRYeYOo3")
            .header("Accept", "application/json")
            .asJson();

    I don't know what type of code this. Please tell me how to transfer this code to C#.net code format. I am new this

    Tuesday, June 5, 2018 7:41 AM

Answers

  • User-369506445 posted

    hi

    you can use WebRequest below like:

    string URL = "https://newsrain-petrol-diesel-prices-india-v1.p.mashape.com/state?state=Tamil+Nadu";
    
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
                request.Headers.Add("X-Mashape-Key", "VZznHLmshqH7p1Ka9DrjsnVHOyyRYeYOo3");
                request.Method = "GET";
                request.ContentType = "application/json";
                WebResponse response = request.GetResponse();
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    var jsonResponse = reader.ReadToEnd();
                    Response.Write(String.Format("Response: {0}", jsonResponse));
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 5, 2018 8:13 AM
  • User-369506445 posted

    you have create a class same as your json result

    public class cities
            {
                public string city { get; set; }
                public string petrol { get; set; }
                public string diesel { get; set; }
                public string petrol_yesterday { get; set; }
                public string state { get; set; }
                public string date { get; set; }
    
    
            }

    and Deserialize your json result to your class

     cities list = Newtonsoft.Json.JsonConvert.DeserializeObject<cities>(jsonInput);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 5, 2018 8:25 AM
  • User-369506445 posted

    I created a sample for parse json result

    public class cities
            {
                public string city { get; set; }
                public string petrol { get; set; }
                public string diesel { get; set; }
                public string petrol_yesterday { get; set; }
                public string state { get; set; }
                public string date { get; set; }
    
    
            }

    and

        string json = @" { ""cities"": [
        {
          ""city"": ""Delhi"",
          ""petrol"": ""77.83"",
          ""diesel"": ""68.88"",
          ""petrol_yesterday"": ""78.11"",
          ""diesel_yesterday"": ""69.11"",
          ""state"": ""Delhi"",
          ""date"": ""05-06-2018""
        },
        {
          ""city"": ""Chennai"",
          ""petrol"": ""80.80"",
          ""diesel"": ""72.72"",
          ""petrol_yesterday"": ""81.09"",
          ""diesel_yesterday"": ""72.97"",
          ""state"": ""Tamil Nadu"",
          ""date"": ""05-06-2018""
        },
        {
          ""city"": ""Mumbai"",
          ""petrol"": ""85.65"",
          ""diesel"": ""73.33"",
          ""petrol_yesterday"": ""85.92"",
          ""diesel_yesterday"": ""73.58"",
          ""state"": ""Maharashtra"",
          ""date"": ""05-06-2018""
        },
        {
          ""city"": ""Kolkata"",
          ""petrol"": ""80.47"",
          ""diesel"": ""71.43"",
          ""petrol_yesterday"": ""80.75"",
          ""diesel_yesterday"": ""71.66"",
          ""state"": ""West Bengal"",
          ""date"": ""05-06-2018""
        }] } ";
                dynamic jsonObject = Newtonsoft.Json.Linq.JObject.Parse(json);
    
                string jsonToDeserializeStrongType = jsonObject["cities"].ToString();
                List<cities> result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<cities>>(jsonToDeserializeStrongType); ;
    
                foreach (var item in result)
                {
                    
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 5, 2018 8:38 AM
  • User-369506445 posted

    please try below code :

               dynamic jsonObject = Newtonsoft.Json.Linq.JObject.Parse(json);
    
                string jsonToDeserializeStrongType = jsonObject["cities"].ToString();
                List<cities> result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<cities>>(jsonToDeserializeStrongType); ;
    
                var singleRow = result.Find(x => x.city.Equals("Chennai"));// this line return as same as you want
    Response.Write(singleRow.city + " " + singleRow.date + " " + singleRow.diesel);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 5, 2018 10:23 AM
  • User-369506445 posted

    your <g class="gr_ gr_23 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="23" data-gr-id="23">json</g> value is null, you must check it before

                string jsonToDeserializeStrongType = "";
                if (jsonObject["cities"]!=null)
                {
                     jsonToDeserializeStrongType = jsonObject["cities"].ToString();
                }


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 11, 2018 7:41 AM

All replies

  • User-369506445 posted

    hi

    you can use WebRequest below like:

    string URL = "https://newsrain-petrol-diesel-prices-india-v1.p.mashape.com/state?state=Tamil+Nadu";
    
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
                request.Headers.Add("X-Mashape-Key", "VZznHLmshqH7p1Ka9DrjsnVHOyyRYeYOo3");
                request.Method = "GET";
                request.ContentType = "application/json";
                WebResponse response = request.GetResponse();
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    var jsonResponse = reader.ReadToEnd();
                    Response.Write(String.Format("Response: {0}", jsonResponse));
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 5, 2018 8:13 AM
  • User-410257276 posted

    Thanks Vahid,

                               Thank you and i need one more, i am getting data like that

     "cities": [
        {
          "city": "Delhi",
          "petrol": "77.83",
          "diesel": "68.88",
          "petrol_yesterday": "78.11",
          "diesel_yesterday": "69.11",
          "state": "Delhi",
          "date": "05-06-2018"
        },
        {
          "city": "Chennai",
          "petrol": "80.80",
          "diesel": "72.72",
          "petrol_yesterday": "81.09",
          "diesel_yesterday": "72.97",
          "state": "Tamil Nadu",
          "date": "05-06-2018"
        },
        {
          "city": "Mumbai",
          "petrol": "85.65",
          "diesel": "73.33",
          "petrol_yesterday": "85.92",
          "diesel_yesterday": "73.58",
          "state": "Maharashtra",
          "date": "05-06-2018"
        },
        {
          "city": "Kolkata",
          "petrol": "80.47",
          "diesel": "71.43",
          "petrol_yesterday": "80.75",
          "diesel_yesterday": "71.66",
          "state": "West Bengal",
          "date": "05-06-2018"
        },

    How can i separate each data... Thanks 

    Tuesday, June 5, 2018 8:18 AM
  • User-369506445 posted

    you have create a class same as your json result

    public class cities
            {
                public string city { get; set; }
                public string petrol { get; set; }
                public string diesel { get; set; }
                public string petrol_yesterday { get; set; }
                public string state { get; set; }
                public string date { get; set; }
    
    
            }

    and Deserialize your json result to your class

     cities list = Newtonsoft.Json.JsonConvert.DeserializeObject<cities>(jsonInput);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 5, 2018 8:25 AM
  • User-369506445 posted

    I created a sample for parse json result

    public class cities
            {
                public string city { get; set; }
                public string petrol { get; set; }
                public string diesel { get; set; }
                public string petrol_yesterday { get; set; }
                public string state { get; set; }
                public string date { get; set; }
    
    
            }

    and

        string json = @" { ""cities"": [
        {
          ""city"": ""Delhi"",
          ""petrol"": ""77.83"",
          ""diesel"": ""68.88"",
          ""petrol_yesterday"": ""78.11"",
          ""diesel_yesterday"": ""69.11"",
          ""state"": ""Delhi"",
          ""date"": ""05-06-2018""
        },
        {
          ""city"": ""Chennai"",
          ""petrol"": ""80.80"",
          ""diesel"": ""72.72"",
          ""petrol_yesterday"": ""81.09"",
          ""diesel_yesterday"": ""72.97"",
          ""state"": ""Tamil Nadu"",
          ""date"": ""05-06-2018""
        },
        {
          ""city"": ""Mumbai"",
          ""petrol"": ""85.65"",
          ""diesel"": ""73.33"",
          ""petrol_yesterday"": ""85.92"",
          ""diesel_yesterday"": ""73.58"",
          ""state"": ""Maharashtra"",
          ""date"": ""05-06-2018""
        },
        {
          ""city"": ""Kolkata"",
          ""petrol"": ""80.47"",
          ""diesel"": ""71.43"",
          ""petrol_yesterday"": ""80.75"",
          ""diesel_yesterday"": ""71.66"",
          ""state"": ""West Bengal"",
          ""date"": ""05-06-2018""
        }] } ";
                dynamic jsonObject = Newtonsoft.Json.Linq.JObject.Parse(json);
    
                string jsonToDeserializeStrongType = jsonObject["cities"].ToString();
                List<cities> result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<cities>>(jsonToDeserializeStrongType); ;
    
                foreach (var item in result)
                {
                    
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 5, 2018 8:38 AM
  • User-410257276 posted

    Thnaks for reply,

                                    How can i get exact one data. Like i need one data 

    ""city"": ""Chennai"",
          ""petrol"": ""80.80"",
          ""diesel"": ""72.72"",
          ""petrol_yesterday"": ""81.09"",
          ""diesel_yesterday"": ""72.97"",
          ""state"": ""Tamil Nadu"",
          ""date"": ""05-06-2018""

    How can i get exact data??

    Tuesday, June 5, 2018 10:12 AM
  • User-369506445 posted

    please try below code :

               dynamic jsonObject = Newtonsoft.Json.Linq.JObject.Parse(json);
    
                string jsonToDeserializeStrongType = jsonObject["cities"].ToString();
                List<cities> result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<cities>>(jsonToDeserializeStrongType); ;
    
                var singleRow = result.Find(x => x.city.Equals("Chennai"));// this line return as same as you want
    Response.Write(singleRow.city + " " + singleRow.date + " " + singleRow.diesel);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 5, 2018 10:23 AM
  • User-410257276 posted

    Thank you so much .... Its very useful

    Tuesday, June 5, 2018 10:36 AM
  • User-410257276 posted

    Dear Friend,

                                This Line not working 

    string jsonToDeserializeStrongType = jsonObject["cities"].ToString();

    ERROR

    {"Cannot perform runtime binding on a null reference"}

    i am getting this error.

    Monday, June 11, 2018 7:29 AM
  • User-369506445 posted

    your <g class="gr_ gr_23 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="23" data-gr-id="23">json</g> value is null, you must check it before

                string jsonToDeserializeStrongType = "";
                if (jsonObject["cities"]!=null)
                {
                     jsonToDeserializeStrongType = jsonObject["cities"].ToString();
                }


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 11, 2018 7:41 AM
  • User-410257276 posted

    Dear Friend, 

                            Already you sent me set of code for my request but when i use i am getting error i put it full code.

    string URL = "https://newsrain-petrol-diesel-prices-india-v1.p.mashape.com/state?state=Tamil+Nadu";
    
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
                request.Headers.Add("X-Mashape-Key", "VZznHLmshqH7p1Ka9DrjsnVHOyyRYeYOo3");
                request.Method = "GET";
                request.ContentType = "application/json";
                WebResponse response = request.GetResponse();
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    var jsonResponse = reader.ReadToEnd();
                   
                }

    And i try to eat individual data like chennai only  you sent me

    public class cities
            {
                public string city { get; set; }
                public string petrol { get; set; }
                public string diesel { get; set; }
                public string petrol_yesterday { get; set; }
                public string state { get; set; }
                public string date { get; set; }
    
    
            }
      string json = @" { ""cities"": [
        {
          ""city"": ""Delhi"",
          ""petrol"": ""77.83"",
          ""diesel"": ""68.88"",
          ""petrol_yesterday"": ""78.11"",
          ""diesel_yesterday"": ""69.11"",
          ""state"": ""Delhi"",
          ""date"": ""05-06-2018""
        },
        {
          ""city"": ""Chennai"",
          ""petrol"": ""80.80"",
          ""diesel"": ""72.72"",
          ""petrol_yesterday"": ""81.09"",
          ""diesel_yesterday"": ""72.97"",
          ""state"": ""Tamil Nadu"",
          ""date"": ""05-06-2018""
        },
        {
          ""city"": ""Mumbai"",
          ""petrol"": ""85.65"",
          ""diesel"": ""73.33"",
          ""petrol_yesterday"": ""85.92"",
          ""diesel_yesterday"": ""73.58"",
          ""state"": ""Maharashtra"",
          ""date"": ""05-06-2018""
        },
        {
          ""city"": ""Kolkata"",
          ""petrol"": ""80.47"",
          ""diesel"": ""71.43"",
          ""petrol_yesterday"": ""80.75"",
          ""diesel_yesterday"": ""71.66"",
          ""state"": ""West Bengal"",
          ""date"": ""05-06-2018""
        }] } ";
                dynamic jsonObject = Newtonsoft.Json.Linq.JObject.Parse(json);
    
                string jsonToDeserializeStrongType = jsonObject["cities"].ToString();
                List<cities> result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<cities>>(jsonToDeserializeStrongType); ;
    
                foreach (var item in result)
                {
                    
                }

    Here i am getting this error

    string jsonToDeserializeStrongType = jsonObject["cities"].ToString();

    Monday, June 11, 2018 7:49 AM