非数组属性的JSON反序列化(JSON Deserialization for Non

编程入门 行业动态 更新时间:2024-10-27 14:20:05
数组属性的JSON反序列化(JSON Deserialization for Non-array Property)

我是JSON反序列化的新手,需要帮助解决我遇到的问题。 我创建的类在JSON中的所有类都表示为数组时有效。 但是,如果您仔细查看我需要使用的JSON,您会注意到'property'成员实际上是成员的成员而不是数组。 这似乎是我的代码失败的地方,我认为我在我的类定义中使用错误的'type'作为产品。

以下是JSON的示例:

{ "kind": "shopping#products", "items": [{ "kind": "shopping#product", "id": "tag:google.com,2010:shopping/products/8040/8382012077897342942", "product": { "googleId": "8382012077897342942", "title": "LEGO Star Wars™: Jabba's Palace™ (9516)", "description": "Rescue Han Solo from Jabba the Hutt's desert palace!", "inventories": [ { "price": 119.99, "shipping": 12.95, "currency": "USD" } ] } } ] }

这是我的代码:

Imports System.Web.Script.Serialization Public Class g_JSON Public Property items As List(Of Items) End Class Public Class Items Public Property product As List(Of product) Public Property kind As String Public Property id As String End Class Public Class product Public Property googleid As String Public Property title As String Public Inventories As List(Of inventories) End Class Public Class inventories Public Property price As Double Public Property shipping As Double End Class Partial Class JSON_Test Inherits System.Web.UI.Page Protected Sub getbutton_Click(sender As Object, e As System.EventArgs) Handles getbutton.Click ' 1. Get JSON string Dim google_json_string As String google_json_string = json_text.Text '2. Deserialize JSON Dim jss As New JavaScriptSerializer Dim ds_results = jss.Deserialize(Of g_JSON)(google_json_string) result1.Text = ds_results.items(0).id result2.Text = ds_results.items(0).kind result3.Text = ds_results.items(0).product(0).title 'errors here End Sub End Class

当我尝试获得产品计数(0)时,它应该返回1时返回任何内容。如果我尝试返回product(0).title我收到以下错误:

指数超出范围。 必须是非负数且小于集合的大小。 参数名称:index

该消息表明产品没有返回任何内容。 任何提示或帮助非常感谢!

PS顺便说一句,当我更改JSON以包含产品作为参数[]时,如下所示,它可以工作。

{ "kind": "shopping#products", "items": [{ "kind": "shopping#product", "id": "tag:google.com,2010:shopping/products/8040/8382012077897342942", "product": [{ "googleId": "8382012077897342942", "title": "LEGO Star Wars™: Jabba's Palace™ (9516)", "description": "Rescue Han Solo from Jabba the Hutt's desert palace!", "inventories": [ { "price": 119.99, "shipping": 12.95, "currency": "USD" } ] } ] } ]

I'm new to JSON deserialization and need help with an issue I'm running into. I created classes that work when all the classes in the JSON are represented as arrays. However, if you take a close look at the JSON I am required to use you will notice the 'property' member is actual a member of a member and not an array. This seems to be where my code fails and I think that I am using the wrong 'type' for product in my class definition.

Here is an example of the JSON:

{ "kind": "shopping#products", "items": [{ "kind": "shopping#product", "id": "tag:google.com,2010:shopping/products/8040/8382012077897342942", "product": { "googleId": "8382012077897342942", "title": "LEGO Star Wars™: Jabba's Palace™ (9516)", "description": "Rescue Han Solo from Jabba the Hutt's desert palace!", "inventories": [ { "price": 119.99, "shipping": 12.95, "currency": "USD" } ] } } ] }

Here is my Code:

Imports System.Web.Script.Serialization Public Class g_JSON Public Property items As List(Of Items) End Class Public Class Items Public Property product As List(Of product) Public Property kind As String Public Property id As String End Class Public Class product Public Property googleid As String Public Property title As String Public Inventories As List(Of inventories) End Class Public Class inventories Public Property price As Double Public Property shipping As Double End Class Partial Class JSON_Test Inherits System.Web.UI.Page Protected Sub getbutton_Click(sender As Object, e As System.EventArgs) Handles getbutton.Click ' 1. Get JSON string Dim google_json_string As String google_json_string = json_text.Text '2. Deserialize JSON Dim jss As New JavaScriptSerializer Dim ds_results = jss.Deserialize(Of g_JSON)(google_json_string) result1.Text = ds_results.items(0).id result2.Text = ds_results.items(0).kind result3.Text = ds_results.items(0).product(0).title 'errors here End Sub End Class

When I try to get a count of product(0) it returns nothing when it should return 1. If I try to return product(0).title I get the following error:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

The message implies nothing is being returned for product. Any hints or help is greatly appreciated!

P.S. By the way, when I change the JSON to include product as a parameter[], like below, it works.

{ "kind": "shopping#products", "items": [{ "kind": "shopping#product", "id": "tag:google.com,2010:shopping/products/8040/8382012077897342942", "product": [{ "googleId": "8382012077897342942", "title": "LEGO Star Wars™: Jabba's Palace™ (9516)", "description": "Rescue Han Solo from Jabba the Hutt's desert palace!", "inventories": [ { "price": 119.99, "shipping": 12.95, "currency": "USD" } ] } ] } ]

最满意答案

好。 我知道了。

所以在这种情况下它真的很容易。 问题顶部的json示例表明每个项目只能有一个产品。 您的类结构定义了产品列表 - 因此反序列化失败。 将列表更改为单个产品。

如果您仍有问题 - 请在此留言,我们可以通过聊天来查看。

对于SO而言,这实际上是太局部化了。

Ok. I get it.

So in this case its real easy. The json example at the top of the question indicates that there can only be one product per item. Your class structure defines a list of products - so deserialization fails. Change the list to a single product.

If you still have trouble - leave a comment here and we can take a look at it via chat.

This is really too localized a scenario for SO.

更多推荐

本文发布于:2023-07-31 12:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1343798.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   属性   序列化   JSON   Deserialization

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!