You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET读取Newtonsoft.Json.Linq.JProperty子项的实现方法

PubMed JSON接口数据解析问题

场景说明

正在从NCBI的PubMed文献接口读取JSON数据,接口地址:
https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&retmode=json&id=31542778

接口返回的JSON结构片段如下:

{
    "header": {
        "type": "esummary",
        "version": "0.3"
    },
    "result": {
        "28569071": {
            "uid": "28569071",
            "lastauthor": "Strayer SM",
            "articleids": [
                {
                    "idtype": "pubmed",
                    "idtypen": 1,
                    "value": "28569071"
                },
                {
                    "idtype": "doi",
                    "idtypen": 3,
                    "value": "10.1177/0890117117708841"
                }
            ],
            "references": []
        }
    }
}

初始解析代码

基于Json.NET编写的初始VB.NET解析代码如下,已实现uid、pubdate等基础字段的提取:

Dim sublist As IEnumerable(Of Object)
Dim dic_list As List(Of Dictionary(Of String, String)) = New List(Of Dictionary(Of String, String))

For Each item As JProperty In data
    item.CreateReader()
    Select Case item.Name
        Case "result" '单条文献记录存储在result节点下
            For Each Entry As JProperty In item.Values
                Dim dic As Dictionary(Of String, String) = New Dictionary(Of String, String)
                For Each Ent As JToken In Entry.Children
                    sublist = Ent.ToList()
                    If IsNumeric(sublist(0)) = False Then
                        For Each d As JProperty In sublist
                            If (d.Parent("articleids") IsNot Nothing) Then
                                ' articleids字段解析逻辑待补充
                            End If

                            If (d.Parent("uid") IsNot Nothing) Then
                                dic("pmid") = d.Parent("uid").ToString
                            End If

                            If (d.Parent("pubdate") IsNot Nothing) Then
                                dic("pubdate") = d.Parent("pubdate").ToString
                            End If                                   
                    End If

                Next
                If dic.Count > 0 Then
                    dic_list.Add(dic)
                End If
            Next
    End Select
Next

故障描述

解析数组类型的articleids字段时遇到障碍,曾尝试添加如下代码处理,但运行异常:

Dim itemObjects As JToken = d.Parent("articleids").Parent
DList = itemObjects.ToList()

核心问题是无法正确获取articleids节点下的数组元素完成遍历,找不到正确的对象调用路径。

验证可用的解法

经调试后可正常运行的articleids解析代码如下,将其补入初始代码的对应判断分支即可:

If (d.Parent("articleids") IsNot Nothing) Then
    Dim articleids = New List(Of Dictionary(Of String, String))
    For Each articleNode In d.Parent("articleids")
        articleids.Add(New Dictionary(Of String, String))
        If articleNode("idtype") IsNot Nothing Then
            articleids.Last()("idtype") = articleNode("idtype").ToString()
        End If
        If articleNode("idtypen") IsNot Nothing Then
            articleids.Last()("idtypen") = articleNode("idtypen").ToString()
        End If
        If articleNode("value") IsNot Nothing Then
            articleids.Last()("value") = articleNode("value").ToString()
        End If
    Next articleNode
    dic("articleids") = articleids
End If

内容的提问来源于stack exchange,提问作者Bill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 08:57:39