将JSON字符串转换为JSON对象

时间:2012-01-18 02:45:32

标签: vb.net json

我有来自Web服务的JSON响应,我需要将其转换为对象然后转换为数组。我的回答类似于下面的回复:

  {"status":{"error":"NO","code":"200","description":"none","message":"Request ok"},"geolocation":{"lat":"38.89515","lng":"-77.0310"},"stations":[{"country":"United States","regPrice":"0.00","midPrice":"0.00","prePrice":"0.00","streetAddress":"1401, I St NW","ID":"1900","lat":"38.901440","lng":"-77.032127","stationName":"Shell","logo":"http:\/\/www.nyneaxis.com\/logo\/stations\/noLogo.png","state":"District of Columbia","city":"Washington D.C.","regDate":null,"midDate":null,"preDate":null,"distance":"0.7"},{"country":"United States","regPrice":"0.00","midPrice":"0.00","prePrice":"0.00","streetAddress":"2116-2150, M St NW","ID":"13029","lat":"38.905201","lng":"-77.048103","stationName":"Exxon","logo":"http:\/\/www.nyneaxis.com\/logo\/stations\/noLogo.png","state":"District of Columbia","city":"Washington D.C.","regDate":null,"midDate":null,"preDate":null,"distance":"1.9"}]}

我现在在控制台中执行此操作是VB.NET。基本上我正在尝试创建一种简单的方法来测试我的API调用并输出信息。我想要完成的是必须遍历JSON数组并列出站点。我以前从未在VB.NET中这样做,并希望得到一些帮助。我一直在阅读有关反序列化的内容,但却不理解它。

4 个答案:

答案 0 :(得分:7)

您可以使用Json.NET

Dim ThisToken as Token = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Token)(JSonString)

答案 1 :(得分:1)

有一个很好的.NET库,叫做Json.NET,对于做这类东西很有用http://json.codeplex.com/

有很多例子,主要是C#。

答案 2 :(得分:1)

http://msdn.microsoft.com/en-us/library/bb412179.aspx在此主题中有详细的“操作方法”

答案 3 :(得分:0)

如果您使用的是.NET 4.0+,则可以使用JavaScriptSerializer反序列化方法,检查: Deserialize JSON with JavaScriptSerializer

注意:请记住,您需要具有一个.NET类,其中包含要反序列化的JSON对象的所有属性。