Mileage calculator using MapQuest error for assembly reference

I am trying to get a mileage calculator to work using map quest.
I found code on line and pieced it together but there is not an assembly in Epicor for JavaScriptSerializer.
“Error: CS0246 - line 182 (7626) - The type or namespace name ‘JavaScriptSerializer’ could not be found (are you missing a using directive or an assembly reference?)”

Is there something else in Epicor that can be used or if anyone has done something like this in epicor who can give me some direction.

Code I am using:

public class MapQuestResponse 
{
   public Route[] route { get; set; }
}

public class Route
{
   public double distance { get; set; }
}

// Define the button click event handler
private void btnTest_Click(object sender, EventArgs e)
{
   // Get the zip codes from the text fields
   string zipCode1 = txtCalHome.Text;
   string zipCode2 = txtShpTo.Text;

   // Build the URL for the MapQuest API request
   string url = string.Format("http://www.mapquestapi.com/directions/v2/route?key=APIKEY&from={0}&to={1}", zipCode1, zipCode2);

   // Send the API request and get the response
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
   HttpWebResponse response = (HttpWebResponse)request.GetResponse();
   Stream stream = response.GetResponseStream();
   StreamReader reader = new StreamReader(stream);
   string responseText = reader.ReadToEnd();

   // Parse the JSON response
   JavaScriptSerializer serializer = new JavaScriptSerializer();
   MapQuestResponse mapQuestResponse = serializer.Deserialize<MapQuestResponse>(responseText);

   // Get the distance from the response and display it in the text field
   double distance = mapQuestResponse.route[0].distance;
   txtLoad.Text = distance.ToString();
} ```

Check your usings, could also just use Newtonsoft

Or System.Text.Json.

(That’s for @klincecum)

1 Like