我编写了一个程序,用户输入地址,点击链接标签,程序会将网站上的文本下载到文本框中。
该代码如下所示:
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();
}
当用户点击LinkLabel时,txtWebData填充了这个:
{
"status":"OK",
"fromlatitude":40.86791,
"fromlongitude":-73.428906,
"locationtype":"ROOFTOP",
"distancecoastmiles":1.7,
"closestdistancelatitude":40.8704815141,
"closestdistancelongitude":-73.4612902712,
"elevationstart":91.9,
"elevationend":0
}
我想知道如何从该文本框中提取“distancecoastmiles”并将该数据放入另一个文本框中。我有什么想法可以实现这个目标吗?
答案 0 :(得分:1)
你得到一个Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 328 [text/csv]
Saving to: ‘large_blob’
71% [========================> ] 233 --.-K/s in 0.03s
2015-10-21 09:22:57 (7.31 KB/s) - Connection closed at byte 233. Retrying.
--2015-10-21 09:22:58-- (try: 2) http://127.0.0.1/large_blob
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response... 416 Requested Range Not Satisfiable
The file is already fully retrieved; nothing to do.
个对象。您可以定义自定义类:
json
将此字符串反序列化为object:
internal class Data
{
public float distancecoastmiles;
}
此外,您可以从字符串中提取其他值,只需将新字段添加到var s = new JavaScriptSerializer();
var o = s.Deserialize<Data>(webData.ToString());
var value = o.distancecoastmiles;
类。
答案 1 :(得分:1)
嗯,它显然是一个JSON,但是如果你想使用textBox的Text属性检索它,你也可以这样做:
var arrItems = txtWebData.Text.Split(",");
string coastmiles = "";
foreach (var item in arrItems)
{
if(item.Contains(distancecoastmiles))
{
var subarr = item.Split(":");
coastmiles = item.Last();
}
}
答案 2 :(得分:0)
您要检索的文本是JSON。解析它的最简单方法是将其解析为JsonObject并访问distancecoastmiles
属性。
dynamic d = JObject.Parse(txtWebData.Text);
txtSomeOther.Text = d.distancecoastmiles;
参考:
Deserialize json object into dynamic object using Json.net
请注意,JObject是Json.Net的一部分,您可以使用NuGet将其添加到项目中。
答案 3 :(得分:0)
这是一个VB.Net 4.5+版本,其中记录了参考/导入:
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