.net - 将 JSON 反序列化为对象 (VB.NET)

标签 .net vb.net json deserialization

我在从此 json 字符串获取一些值时遇到问题:

{
 "kind": "shopping#products",
 "etag": "\"YZWJaKE3MHROIW8rCIlu9mAACLM/6qxBB-GwuSPy5L3_zVS6sS2NYFI\"",
 "id": "tag:google.com,2010:shopping/products",
 "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products?country=US&q=Bauerfeind+MalleoTrain+Ankle+Support,+Circumference+in+inches+6+3/4+-+7+1/2+,+Left,+Color+Titanium+&rankBy=price:descending&maxResults=1&startIndex=1",
 "nextLink": "https://www.googleapis.com/shopping/search/v1/public/products?country=US&q=Bauerfeind+MalleoTrain+Ankle+Support,+Circumference+in+inches+6+3/4+-+7+1/2+,+Left,+Color+Titanium+&rankBy=price:descending&maxResults=1&startIndex=2",
 "totalItems": 46,
 "startIndex": 1,
 "itemsPerPage": 1,
 "currentItemCount": 1,
 "items": [
  {
   "kind": "shopping#product",
   "id": "tag:google.com,2010:shopping/products/5944931/17136892246969389705",
   "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products/5944931/gid/17136892246969389705",
   "product": {
        "googleId": "17136892246969389705",
        "author": {"name": "Superemployee.com","accountId": "5944931"},
        "creationTime": "2011-08-28T07:46:29.000Z",
        "modificationTime": "2011-09-11T06:02:54.000Z",
        "country": "US",
        "language": "en",
        "title": "Bauerfeind MalleoTrain Ankle Support Circumference in inches 6 3/4 - 7 1/2 Left Color Black",
        "description": "Bauerfeind MalleoTrain Ankle Support Circumference in inches 6 3/4 - 7 1/2 Left Color Black : Bauerfeind MalleoTrain Ankle Support, Circumference in inches 6 3/4 - 7 1/2 , Left, Color Black MalleoTrain relieves ankle pain and swelling during sports and everyday activities. Product Features: Knitted ankle support incorporating an anatomically contoured silicone insert behind each ankle bone . Silicone inserts leave ankle bones pressure-free and provide intermittent compression to the soft tissue of the joint, leading to increased circulation, thus aiding in the reduction of swelling and edema . Promotes proprioception , thus heightening sensory awareness in the ankle for increased joint stabilization . Anatomical knit carries controlled compression graduated at the edges to prevent constriction of circulation . Lightweight, breathable knit will not retain heat and is completely machine washable . Can be used to treat: Ankle swelling and soreness . Ankle sprains . Ligamental weakness and slight ligamentous tears . Degenerative joint disease (osteoarthritis) . Synovitis . ? Bursitis . Arthritis, osteoarthritis . Post cast . Product photo may not exactly match the product offered for sale. Please refer to the product description.",
        "link": "http://superemployee-com.amazonwebstore.com/Bauerfeind-MalleoTrain-Ankle-Support-Circumference-in/M/B001D0PFRY.htm?traffic_src=froogle&utm_medium=CSE&utm_source=froogle",
        "brand": "Bauerfeind",
        "condition": "new",
        "inventories": [{"channel": "online", "availability": "inStock","price": 90.0,"currency": "USD"} ],
        "images": [{"link": "http://ecx.images-amazon.com/images/I/31xD5bPI4sL.jpg?gdapi"}
    ]
   }
  }
 ]

我尝试过使用和创建类,但除了前两个级别之外无法返回数据,例如我试图检索价格,但不知道如何提取此数据?

这是我正在使用的代码,它不返回任何内容:

<DataContract(Namespace:="")> _
    Public Class items

        <DataMember(Name:="product")>
        Public Property product As product

    End Class

    <DataContract(Name:="product", Namespace:="")> _
    Public Class product
        <DataMember(Name:="inventories")>
        Public Property inventories As inventories

    End Class

    <DataContract(Name:="inventories", Namespace:="")> _
    Public Class inventories
        <DataMember(Name:="price")>
        Public Property price As Double

    End Class

感谢您的帮助

使用 JSON.net 框架,我将我的类更改为以下内容,但仍然没有得到任何返回?

   Public Class items
        Public Property product As product()
        Public Property kind As String
    End Class

    Public Class product
        Public Property inventories As inventories()
    End Class

    Public Class inventories
        Public Property price As Double
    End Class

最佳答案

您看过 JSON.net 吗?

这是一个非常快的序列化/反序列化框架。一旦放入您的项目中,您可以执行以下操作:

Dim myObj as <Enter object type> _
= JsonConvert.DeserializeObject(Of <Enter object type>)("my json string here")

主页是:http://james.newtonking.com/pages/json-net.aspx

注意:我的示例可能略有偏差,没有检查它的语法:)

关于.net - 将 JSON 反序列化为对象 (VB.NET),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7406948/

相关文章:

vb.net - 在 Visual Basic 中使用 Excel 对象库的脆弱性

c# - 您将如何从 MS Project .mpp 文件中提取数据?

wpf - 将 View 显示为对话框

java - JAX-RS Jackson Json 提供者日期格式问题

ios - 将 NSDictionary 转换为 Json String 会导致元素重新排序

.net - 在 .NET 中使用哪些好的倒排索引库?

c# - .NET 异常处理有多繁重?

c# - 如何使用 LINQ 添加两条记录?

javascript - JS中如何将json转为树形数组?

c# - 将日期时间 js 转换为 c#