Convert JSON String to JSON Object

匿名 (未验证) 提交于 2019-12-03 02:59:02

问题:

I have a JSON response from a web service that I need to be converted to an object then to an array. My response is similar to the one below:

  {"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"}]} 

I am doing this is VB.NET within a console for now. Basically I am trying to create a simple way to test my API calls and output the information. What I am trying to accomplish is having to loop through the JSON array and list the stations. I have never done this in VB.NET before and would like some help. I have been reading about deserialization but do not understand it.

回答1:

There's a good library for .NET called Json.NET that's useful for doing this kind of stuff http://json.codeplex.com/

there are a bunch of examples, mostly C# though.



回答2:

You can use Json.NET

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


回答3:

http://msdn.microsoft.com/en-us/library/bb412179.aspx has a detailed "How to" on this topic



回答4:

If you are using .NET 4.0+, you can user JavaScriptSerializer Deserialize method, check: Deserialize JSON with JavaScriptSerializer

Note: Keep in mind that you need to have a .NET class with all the properties of the JSON object to deserialize.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!