VB.Net多维JSON字符串 [英] VB.Net multidimentional Json String

查看:200
本文介绍了VB.Net多维JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是一个VB.Net程序员,但都被塞进不得不这样做。我一直戳在这几天和搜索对谷歌反反复复。越是有经验的程序员告诉我到这里来问,因为他们不知道VB.net无论是。

I'm not a VB.Net programmer, but have been thrust into having to do it. I've been poking at this for a few days and have searched on Google repeatedly. The more experienced programmers told me to come here and ask because they didn't know VB.net either.

我有一个JSON字符串,剥夺了简洁一些内部数据...

I have a json string, stripped of some inner data for brevity...

{
    "_links": {
        "self": {
            "href": "URL"
        },
        "first": {
            "href": "URL"
        },
        "last": {
            "href": "URL"
        }
    },
    "_embedded": {
        "account": [
            {
                "user": "Bob",
                "text": "Some Text",
                "status": "*ENABLED",
                "_links": {
                    "self": {
                        "href": "Some URL"
                    }
                }
            },
            {
                "user": "Joe",
                "text": "Some Other Text",
                "status": "*ENABLED",
                "_links": {
                    "self": {
                        "Some Other URL"
                    }
                }
            }
        ]
    },
    "page_count": 1,
    "page_size": 25000,
    "total_items": 1109,
    "page": 1
}

我使用的是.NET 3.5,可以通过内部的项目重复,以获取数据:

I'm using .net 3.5 and can iterate through the internal items to get the data with:

Dim myData As Dictionary(Of String, Object)
myData = serializer.DeserializeObject(myResult)

Dim newData = myData.Item("_embedded")
Dim accountData= newData.Item("account")

For Each account As Object In accountData
    Dim tmpData As Dictionary(Of String, Object) = CType(account, Dictionary(Of String, Object))
    Console.WriteLine("User: " & tmpData.Item("user").ToString())
    Console.WriteLine("Account Text: " & tmpData.Item("text").ToString())
    Console.WriteLine("Status: " & tmpData.Item("status").ToString())
    tmpData.Clear()
Next

不过,我必须把严,当我这样做,因为我期望得到的code错误。大部分我读过的建议说,使用丰富金额CTYPE,TryCast的,等等。我还听说,json.net会更好地工作,但我一直在努力为谷歌在过去3天,尝试不同的东西...

However, I have to turn "Strict On", and when I do, I get errors with the code as expected. Most of the suggestions I've read say to use copious amounts of CType, TryCast, and so on. I also hear that json.net would work better, but I have been trying for the past 3 days of Google and trying different things...

我跑了严格的关闭调试器,看着有什么不同的线被转换为,并试图重新无济于事......

I ran the debugger with Strict off and watched what the various lines were cast as and tried to recreate to no avail...

打开,要比将严格关闭,因为我没有这方面的奢侈其他试图建议...

Open to trying suggestions other than turning Strict off as I don't have that luxury...

推荐答案

使用NewtonSoft,你可以很容易地重复数据pretty的:

Using NewtonSoft, you can iterate the data pretty easily:

Dim jstr = ...
Dim jobj = JObject.Parse(jstr)

Dim jdata = jobj("_embedded")("account")

For Each j As JObject In jdata
    Console.WriteLine("User: " & j.Item("user").ToString())
    Console.WriteLine("Account Text: " & j.Item("text").ToString())
    Console.WriteLine("Status: " & j.Item("status").ToString())
Next

输出:

网友:鲍勃
  账户文字:一些文本
  状态:*启用
  用户:乔
  账户文字:其它文本
  状态:*启用

User: Bob
Account Text: Some Text
Status: *ENABLED
User: Joe
Account Text: Some Other Text
Status: *ENABLED

而不是仅仅打印,你可以存储到集合。你也可以使用一些类直接创建一个类型的集合:

Rather than just printing, you could store to a collection. You could also use some classes to create a typed collection directly:

Public Class Account
    Public Property user As String
    Public Property text As String
    Public Property status As String
    <JsonProperty("_links")>
    Public Property Links As LinkInfo
End Class

Public Class Self
    Public Property href As String
End Class

Public Class LinkInfo
    Public Property self As Self
End Class

这是JSON的帐户部分的结构,所以现在我们可以反序列化到列表(帐户)

That is the structure of the "account" portion of the json, so now we can deserialize to a List(Of Account):

Dim jobj = JObject.Parse(jstr)
Dim jdata = jobj("_embedded")("account")
Dim myAccts = JsonConvert.DeserializeObject(Of List(Of Account))(jdata.ToString)

myAccts 将成为列表包含帐户在JSON每一件事情的项目。请注意,其中一些类可能需要的工作 - 2有限的样本可能不给他们看起来像一个完整的画面

myAccts will be a List containing an Account item for each thing in the json. Note that some of those classes may need work - the limited sample of 2 may not give a complete picture of what they look like.

这篇关于VB.Net多维JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆