如何在vb.net中序列化包含2个或更多项的JSON对象数组

时间:2015-10-31 01:38:23

标签: arrays json vb.net object serialization

我在使用维度数组创建json对象时遇到了问题。

请参阅我使用的代码,如果可以,请帮助我

  

输出字符串json对象:

    {
      "title":"The title",
      "subtitle":"Some subtitle here",
      "category_id":"your category",
      "price":10,
      "currency_id":"Real",
      "available_quantity":1,
      "buying_mode":"buy_it_now",
      "listing_type_id":"bronze",
      "condition":"new",
      "description": "Description",
      "video_id": "YOUTUBE_ID_HERE",
      "warranty": "12 months",
      "pictures":[
        {"source":"http://www.yourimage/1.jpg"},
        {"source":"http://www.yourimage/2.jpg"},
        {"source":"http://www.yourimage/3.jpg"}
      ]
    }
  

Json Class:

     

http://jsonutils.com/生成

 Public Class Picture
    Public Property source As String
End Class

Public Class newItem
    Public Property title As String
    Public Property subtitle As String
    Public Property category_id As String
    Public Property price As Integer
    Public Property currency_id As String
    Public Property available_quantity As Integer
    Public Property buying_mode As String
    Public Property listing_type_id As String
    Public Property condition As String
    Public Property description As String
    Public Property video_id As String
    Public Property warranty As String
    Public Property pictures() As Picture
End Class
  

使用该类生成json对象:

    Dim itempost As New PostItem()
                itempost.title = "Não compre isso é apenas um teste"
                itempost.subtitle = "Sub título"
                itempost.category_id = "MLB42369"
                itempost.price = 100
                itempost.currency_id = "BRL"
                itempost.available_quantity = 1
                itempost.buying_mode = "buy_it_now"
                itempost.listing_type_id = "gold_special"
                itempost.condition = "new"
                itempost.description = "Descrição do produto"
                itempost.video_id = "https://www.youtube.com/watch?v=Gta9HU6M0hk"
                itempost.warranty = "Jesus Cristo"

               **'here is where I am in trouble**
                itempost.pictures = New String() {"http://www.yourimage/1.jpg", ""http://www.yourimage/2.jpg""}
                MessageBox.Show(JsonConvert.SerializeObject(itempost).ToString)

我已经在很多方面尝试过,但没有成功。 对于语言错误,我使用谷歌翻译

编辑:

我向前迈进了一步。遵循代码:

Dim itempost As New PostItem() With {
             .title = "Não compre isso é apenas um teste",
            .category_id = "MLB42369",
            .price = 100,
            .currency_id = "BRL",
            .available_quantity = 1,
            .buying_mode = "buy_it_now",
            .listing_type_id = "gold_special",
            .condition = "new",
            .description = "Descrição do produto",
            .video_id = "https://www.youtube.com/watch?v=Gta9HU6M0hk",
            .warranty = "Jesus Cristo",
            .pictures = New Picture() With {
            .source = ""
            }
           }

MessageBox.Show(JsonConvert.SerializeObject(itempost,Formatting.Indented))

关注输出json:

{
  "title": "Não compre isso é apenas um teste",
  "category_id": "MLB42369",
  "price": 100,
  "currency_id": "BRL",
  "available_quantity": 1,
  "buying_mode": "buy_it_now",
  "listing_type_id": "gold_special",
  "condition": "new",
  "description": "Descrição do produto",
  "video_id": "https://www.youtube.com/watch?v=Gta9HU6M0hk",
  "warranty": "Jesus Cristo",
  "pictures": {
    "source": ""
  }
}

唯一缺少的是改变语法:

"pictures": {
        "source": ""
      }

有:

"pictures": [
       { 
         "source": ""
    }
      ]

1 个答案:

答案 0 :(得分:0)

祝贺所有人。

查看代码如何变为

班级代码

Public Class Picture
    Public Property source As String
End Class



Public Class PostItem

    Public Property title As String
    Public Property category_id As String
    Public Property price As Integer
    Public Property currency_id As String
    Public Property available_quantity As Integer
    Public Property buying_mode As String
    Public Property listing_type_id As String
    Public Property condition As String
    Public Property description As String
    Public Property video_id As String
    Public Property warranty As String
    Public Property pictures As List(Of Picture)
End Class

代码序列化

Dim itempost As New PostItem() With {
             .title = "Não compre isso é apenas um teste",
            .category_id = "MLB42369",
            .price = 100,
            .currency_id = "BRL",
            .available_quantity = 1,
            .buying_mode = "buy_it_now",
            .listing_type_id = "gold_special",
            .condition = "new",
            .description = "Descrição do produto",
            .video_id = "https://www.youtube.com/watch?v=Gta9HU6M0hk",
            .warranty = "Jesus Cristo",
            .pictures = New List(Of Picture) From {
                New Picture() With {
                    .source = "Foto 1"},
                New Picture() With {
                    .source = "Foto 2"}
            }
        }

输出Json字符串

{
  "title": "Não compre isso é apenas um teste",
  "category_id": "MLB42369",
  "price": 100,
  "currency_id": "BRL",
  "available_quantity": 1,
  "buying_mode": "buy_it_now",
  "listing_type_id": "gold_special",
  "condition": "new",
  "description": "Descrição do produto",
  "video_id": "https://www.youtube.com/watch?v=Gta9HU6M0hk",
  "warranty": "Jesus Cristo",
  "pictures": [
    {
      "source": "Foto 1"
    },
    {
      "source": "Foto 2"
    }
  ]
}