我正在VB.Net上使用“使用HTTP GET查询”来获得以下代码的响应:
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("http://api.site.com/api=xxxxxxxxxxxx&id=1")`
要查看我使用的字符串:
MsgBox(result)
我得到的答复是:
{“ result”:“成功”,“ message”:“找到成员”,“ member_data”:{“ member_id”:“ 1”,“ user_name”:“ John”,“ first_name”:“ John”, “ last_name”:“ Smith”,“ password”:“ xxx”,“ member_since”:“ 2018-12-08”,“ membership_level”:“ 2”,“ more_membership_levels”:null,“ account_state”:“ active”, “ last_accessed”:“ 2018-12-08 09:00:53”,“ last_accessed_from_ip”:“ 1.1.1.1”,“ email”:“ john.smigh@test.com”,“ phone”:null,“ address_street” :null,“ address_city”:null,“ address_state”:null,“ address_zipcode”:null,“ home_page”:null,“ country”:null,“ gender”:“未指定”,“ referrer”:null,“ extra_info “:null,” reg_code“:null,” subscription_starts“:” 2018-12-08“,” initial_membership_level“:null,” txn_id“:”“,” subscr_id“:”“,” company_name“:null,”注释“:null,”标志“:” 0“,” profile_image“:”“}}
我一直想弄清楚的是如何获得 姓名,姓氏,电子邮件,会员级别和帐户状态分别显示在单独的label.text中,对您的帮助非常感谢!
答案 0 :(得分:0)
您需要反序列化JSON。您可以使用JavaScript(De)Serializer或诸如newtonsoft.json(json.net)之类的外部对象。几乎是相同的概念。
Dim test As JObject = JObject.Parse(New WebClient().DownloadString("http://api.site.com/api=xxxxxxxxxxxx&id=1"))
Dim test1 As JToken = test("member_data")
Debug.Print(String.Format("{0}, {1}, {2}, {3}, {4}", test1("first_name"), test1("last_name"), test1("email"), test1("membership_level"), test1("account_state")))
'output >>> John, Smith, john.smigh@test.com, 2, active
我使用newtonsoft是因为我已经在当前项目中使用了它。
这就像字典,只需调用键(名称),就可以得到您的值。