使用VB.NET解析复杂的JSON [英] Parse complex JSON with VB.NET

查看:143
本文介绍了使用VB.NET解析复杂的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助解析复杂的JSON。我花了很多时间在网上搜索解决方案,但在搜索时我感到更加困惑。我通常不使用JSON,所以我很新手。



在下面的JSON中,如果有人能给我看一些能让我进入< b> trainId 字段(从底部开始大约10行),我可以从中找出其他所有内容,因为这将是此JSON最深层次之一。



I need help on parsing a complex JSON. I've spent a lot of time searching the web to find solutions but I get more confused as I search. I don't usually work with JSON so I'm pretty novice.

In the JSON below, if someone could show me some code that would get me to the trainId field (about 10 lines from the bottom), I could probably figure out everything else from that since that would be one of the deepest levels of this JSON.

{
    "properties" : {
        "messageType" : "tripOrderRequest",
        "sentDateTime" : "2017-08-10T11:40:01.454Z",
        "senderId" : "1",
        "recipientId" : "2",
        "messageIntExt" : "external",
        "eventTypeVersion" : "1.1"
    },
    "body" : {
        "tripOrderId" : "d0b0f506-acbc-42f3-b712-bed7c60ff6b5",
        "shortKey" : "0ff6b5",
        "requestedVanHub" : "Oklahoma City, OK",
        "requestedVanHubID" : "1beff5b5-2cd3-41d3-9e34-f45a10717045",
        "tripOrderItinerary" : [ {
            "sequence" : 1,
            "type" : "P",
            "status" : "P",
            "associatedTripId" : "0bb3ab5d-9f08-488f-89f3-b07b072b6d51",
            "locationId" : "d78888a4-a6da-4c8c-b2d8-fbdd4b332394",
            "locationLat" : 36.116381,
            "locationLong" : -96.010583
        },{
            "sequence" : 2,
            "type" : "D",
            "status" : "P",
            "associatedTripId" : "0bb3ab5d-9f08-488f-89f3-b07b072b6d51",
            "locationId" : "c7016a4f-da4b-41d0-9f93-b38af1c36827",
            "locationLat" : 36.097914,
            "locationLong" : -95.865680
        } ],
        "tripDetails" : [ {
            "tripId" : "0bb3ab5d-9f08-488f-89f3-b07b072b6d51",
            "shortKey" : "2b6d51",
            "sequence" : 10,
            "pickupLocation" : {
                "adhoc" : {
                    "lat" : 36.116381,
                    "lng" : -96.010583         
                },
                "knownLocation" : {
                    "lat" : 36.116381,
                    "lng" : -96.010583,
                    "locationId" : "d78888a4-a6da-4c8c-b2d8-fbdd4b332394",
                    "name" : "Tulsa OK",
                    "address" : "1631 W 33RD PL, Tulsa, Oklahoma, 74107",
                    "radioChannel" : 51
                }
            },
            "dropOffLocation" : {
                "adhoc" : {
                    "lat" : 36.097914,
                    "lng" : -95.865680              
                },
                "knownLocation" : {
                    "lat" : 36.097914,
                    "lng" : -95.865680,
                    "locationId" : "c7016a4f-da4b-41d0-9f93-b38af1c36827",
                    "name" : "Tulsa - Cherokee YA OK",
                    "address" : "1631 W 33RD PL, Tulsa, Oklahoma, 74107",
                    "radioChannel" : 52
                }
            },
            "pickupSchedule" : {
                "preferredTimeZone" : "CDT",
                "needDateTime" : "2017-09-10T11:40:01.454Z",
                "pickupDateTime" : "2017-09-10T12:40:01.454Z"
            },
            "conveyance" : {
                "crew" : {
                    "crewMember" : [ {
                        "sequence" : 1,
                        "name" : "John Doe",
                        "id" : "x8888"
                    },{
                        "sequence" : 2,
                        "name" : "Johnny Doe",
                        "id" : "x8885"
                    } ],
                    "associatedTrainDetails" : {
                        "trainId" : "HPASLVJ113A",
                        "leadLocomotiveId" : "BNSF 2347"
                    }
                },
                "equipments" : [],
                "passenger" : []
            }
        } ] 
    }
}





我尝试了什么:



我找到了解析的方法简单的JSON使用 Newtonsoft.Json 但似乎无法适应它以达到更深层次。我在截止日期前完成了这项工作。



What I have tried:

I have found ways to parse simple JSON using Newtonsoft.Json but can't seem to adapt it to get to deeper levels. I'm running short on a deadline to get this done.

推荐答案

我会带你的json并使用 jsonutils [ ^ ]。这会将您的复杂JSON转换为可用于序列化/反序列化JSON的类,而无需编写自己的解析器。所以一旦你上课了,你应该可以做一些像 Private pp As Properties = JsonConvert.DeserializeObject(Of Properties)(json)



VB可能不是我通常不能使用VB的语法。
I would take your json and use jsonutils[^]. This converts your complex JSON to classes that you can use to serialize/deserialize your JSON without trying to write your own parser. So once you get the classes you should be able to do something like Private pp As Properties = JsonConvert.DeserializeObject(Of Properties)(json)

That VB may not be exact syntax as I dont normally work with VB.


如果您要查找的字段名称对于json字符串是唯一的,那么这是我找到的最简单的方法:

https://www.codeproject.com/Tips/1176910/Easy-JSON-Recursion-in-VB-NET- with-Nested-Levels?msg = 5531222#xx5531222xx



如果您要查找的字段名称列在几个中,我将原始文件修改为更深节点但仅适用于一个级别。您需要修改才能到达正确的节点。
If the field name you are looking for is unique to the json string, then this is the easiest way I have found:
https://www.codeproject.com/Tips/1176910/Easy-JSON-Recursion-in-VB-NET-with-Nested-Levels?msg=5531222#xx5531222xx

I modified the original to go deeper if the field name you are looking for is listed in several nodes but only for one level. You would need to modify to get to the correct node.


这篇关于使用VB.NET解析复杂的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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