将 JSON 字符串转换为 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8904522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert JSON String to JSON Object
提问by Brandon Wilson
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:
我有一个来自 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"}]}
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.
我现在在控制台中这样做是 VB.NET。基本上,我正在尝试创建一种简单的方法来测试我的 API 调用并输出信息。我想要完成的是必须遍历 JSON 数组并列出电台。我以前从未在 VB.NET 中这样做过,希望得到一些帮助。我一直在阅读有关反序列化的内容,但不明白。
采纳答案by Keith Nicholas
There's a good library for .NET called Json.NET that's useful for doing this kind of stuff http://json.codeplex.com/
有一个很好的 .NET 库,称为 Json.NET,对于做这种事情很有用http://json.codeplex.com/
there are a bunch of examples, mostly C# though.
有很多例子,但主要是 C#。
回答by Daria Trainor
You can use Json.NET
您可以使用 Json.NET
Dim ThisToken as Token = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Token)(JSonString)
回答by Eugen Rieck
http://msdn.microsoft.com/en-us/library/bb412179.aspxhas a detailed "How to" on this topic
http://msdn.microsoft.com/en-us/library/bb412179.aspx有关于这个主题的详细“如何”
回答by mas_oz2k1
If you are using .NET 4.0+, you can user JavaScriptSerializerDeserialize method, check: Deserialize JSON with JavaScriptSerializer
如果你使用 .NET 4.0+,你可以使用JavaScriptSerializerDeserialize 方法,检查: 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.
注意:请记住,您需要有一个 .NET 类,其中包含要反序列化的 JSON 对象的所有属性。

