Reputation: 115
I wrote a program where a user inputs an address, clicks a linklabel and the program will download the text on a website into a text box.
That code looks like this:
private void llbMap_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var _address = txtAddress.Text + " " + txtCity.Text + " " + "NY " + txtZip.Text;
txtFullAddress.Text = _address.ToString();
string toolDistanceMeasuring = "https://mywebsite.net/distance/?Type=json&Token=TUdBIFN5c3RlbXM1&Address=" + _address;
WebClient wc = new WebClient();
byte[] raw = wc.DownloadData(toolDistanceMeasuring);
string webData = Encoding.UTF8.GetString(raw);
txtWebData.Text = webData.ToString();
}
When the user clicks the LinkLabel txtWebData is filled with this:
{
"status":"OK",
"fromlatitude":40.86791,
"fromlongitude":-73.428906,
"locationtype":"ROOFTOP",
"distancecoastmiles":1.7,
"closestdistancelatitude":40.8704815141,
"closestdistancelongitude":-73.4612902712,
"elevationstart":91.9,
"elevationend":0
}
I want to know how I can extract just the "distancecoastmiles" from that textbox and put that data in another textbox. Any ideas how I might be able to accomplish that?
Upvotes: 0
Views: 535
Reputation: 2297
Here is a VB.Net 4.5+ version with the reference/import documented:
Public Class Locat ' generated on http://jsonutils.com/
Public Property status As String
Public Property fromlatitude As Double
Public Property fromlongitude As Double
Public Property locationtype As String
Public Property distancecoastmiles As Double
Public Property closestdistancelatitude As Double
Public Property closestdistancelongitude As Double
Public Property elevationstart As Double
Public Property elevationend As Integer
End Class
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
'project reference to .Net System.Web.Extensions
'Imports System.Web.Script.Serialization
Dim jSerializer As New JavaScriptSerializer()
Dim strData2 = <j>
{
"status":"OK",
"fromlatitude":40.86791,
"fromlongitude":-73.428906,
"locationtype":"ROOFTOP",
"distancecoastmiles":1.7,
"closestdistancelatitude":40.8704815141,
"closestdistancelongitude":-73.4612902712,
"elevationstart":91.9,
"elevationend":0
}
</j>.Value
Try
Dim o As Locat = jSerializer.Deserialize(Of Locat)(strData2)
MsgBox(o.distancecoastmiles)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Upvotes: 0
Reputation: 536
well, it's obviously a JSON, but if you want to retrieve it using the Text property of a textBox, you could also do it like this:
var arrItems = txtWebData.Text.Split(",");
string coastmiles = "";
foreach (var item in arrItems)
{
if(item.Contains(distancecoastmiles))
{
var subarr = item.Split(":");
coastmiles = item.Last();
}
}
Upvotes: 1
Reputation: 150108
The text you are retrieving is JSON. The simplest way to parse it is to parse it to JsonObject and access the distancecoastmiles
property.
dynamic d = JObject.Parse(txtWebData.Text);
txtSomeOther.Text = d.distancecoastmiles;
Reference:
Deserialize json object into dynamic object using Json.net
Note that JObject is part of Json.Net, which you can add to your project using NuGet.
Upvotes: 0
Reputation: 24903
You get a json
object. You can define your custom class:
internal class Data
{
public float distancecoastmiles;
}
And deserialize this string to object:
var s = new JavaScriptSerializer();
var o = s.Deserialize<Data>(webData.ToString());
var value = o.distancecoastmiles;
Also, you can extract other values from your string, just add new fields to Data
class.
Upvotes: 1