C# JSON.Net-从多态JSON字符串检索另一个数据

C# JSON.Net-从多态JSON字符串检索另一个数据,c#,linq,c#-4.0,json.net,linq-to-json,C#,Linq,C# 4.0,Json.net,Linq To Json,以下是我的JSON字符串的外观: { "?xml" : { "@version" : "1.0", "@encoding" : "UTF-8" }, "DataFeed" : { "@FeedName" : "content", "now" : "2016-10-17T14:11:38.00", "Content" : [{ "ContentData" : {

以下是我的JSON字符串的外观:

{
    "?xml" : {
        "@version" : "1.0",
        "@encoding" : "UTF-8"
    },
    "DataFeed" : {
        "@FeedName" : "content",
        "now" : "2016-10-17T14:11:38.00",
        "Content" : [{
                "ContentData" : {
                    "@statusType" : "Published",
                    "@isReleased" : "Yes",
                    "@docId" : "21756",
                    "ProductStatus" : {
                        "@productCategory" : "Issuer",
                        "@focus" : "Issuer",
                        "Symbol" : {
                            "@PrimarySymbol" : "GOOGL",
                            "@SecondarySymbol" : "Google",
                            "@BmId" : "1790"
                        }
                    },
                    "ContentVersion" : {
                        "@versionId" : "1",
                        "@lastVersion" : "true",
                        "@lastPublished" : "true",
                        "Url" : [{
                                "@type" : "XML",
                                "@size" : "146410",
                                "#cdata-section" : "https://aaa.yyy.com/docs/xml/708893eb-1fd9-4278-8b0e-e7e0738d8105.xml"
                            }, {
                                "@type" : "HTML",
                                "@size" : "46105",
                                "#cdata-section" : "https://aaa.yyy.com/docs/html/708893eb-1fd9-4278-8b0e-e7e0738d8105.html"
                            }, {
                                "@type" : "PDF",
                                "@size" : "1274885",
                                "#cdata-section" : "https://aaa.yyy.com/docs/pdf/708893eb-1fd9-4278-8b0e-e7e0738d8105.pdf"
                            }, {
                                "@type" : "ALTERNATE PDF",
                                "@size" : "60960",
                                "#cdata-section" : "https://aaa.yyy.com/docs/shortPdf/708893eb-1fd9-4278-8b0e-e7e0738d8105.pdf"
                            }
                        ]
                    }
                }
            }, {
                "ContentData" : {
                    "@statusType" : "Published",
                    "@isReleased" : "Yes",
                    "@docId" : "22457",
                    "ProductStatus" : {
                        "@productCategory" : "Issuer",
                        "@focus" : "Issuer",
                        "Symbol" : {
                            "@PrimarySymbol" : "AAPL",
                            "@SecondarySymbol" : "Apple",
                            "@BmId" : "1121"
                        }
                    },
                    "ContentVersion" : [{
                            "@versionId" : "1",
                            "@lastVersion" : "false",
                            "@lastPublished" : "false",
                            "Url" : [{
                                    "@type" : "XML",
                                    "#cdata-section" : "https://aaa.yyy.com/docs/version/708893eb-1fd9-4278-8b0e-e7e0738d8105.1476742297.xml"
                                }, {
                                    "@type" : "HTML",
                                    "#cdata-section" : "https://aaa.yyy.com/docs/version/708893eb-1fd9-4278-8b0e-e7e0738d8105.1476742297.html"
                                }, {
                                    "@type" : "PDF",
                                    "#cdata-section" : "https://aaa.yyy.com/docs/version/708893eb-1fd9-4278-8b0e-e7e0738d8105.1476742297.pdf"
                                }, {
                                    "@type" : "ALTERNATE PDF",
                                    "#cdata-section" : "https://aaa.yyy.com/docs/version/708893eb-1fd9-4278-8b0e-e7e0738d8105.1476742297_short.pdf"
                                }
                            ]
                        }, {
                            "@versionId" : "2",
                            "@lastVersion" : "true",
                            "@lastPublished" : "true",
                            "Url" : [{
                                    "@type" : "XML",
                                    "@size" : "174520",
                                    "#cdata-section" : "https://aaa.yyy.com/docs/xml/708893eb-1fd9-4278-8b0e-e7e0738d8105.xml"
                                }, {
                                    "@type" : "HTML",
                                    "@size" : "46105",
                                    "#cdata-section" : "https://aaa.yyy.com/docs/html/708893eb-1fd9-4278-8b0e-e7e0738d8105.html"
                                }, {
                                    "@type" : "PDF",
                                    "@size" : "1270150",
                                    "#cdata-section" : "https://aaa.yyy.com/docs/pdf/708893eb-1fd9-4278-8b0e-e7e0738d8105.pdf"
                                }, {
                                    "@type" : "ALTERNATE PDF",
                                    "@size" : "60960",
                                    "#cdata-section" : "https://aaa.yyy.com/docs/version/708893eb-1fd9-4278-8b0e-e7e0738d8105.1577742297_short.pdf"
                                }
                            ]
                        }
                    ]
                }
            }, {
                "ContentData" : {
                    "@statusType" : "Published",
                    "@isReleased" : "Yes",
                    "@docId" : "22501",
                    "ProductStatus" : {
                        "@productCategory" : "Issuer",
                        "@focus" : "Issuer",
                        "Symbol" : [{
                                "@PrimarySymbol" : "AAA",
                                "@SecondarySymbol" : "Some name 1",
                                "@BmId" : "561"
                            }, {
                                "@PrimarySymbol" : "ABC",
                                "@SecondarySymbol" : "Some Name 2",
                                "@BmId" : "29"
                            }, {
                                "@PrimarySymbol" : "BBB",
                                "@SecondarySymbol" : "Some Name 3",
                                "@BmId" : "36"
                            }, {
                                "@PrimarySymbol" : "CCC",
                                "@SecondarySymbol" : "Some Name 4",
                                "@BmId" : "624"
                            }
                        ]
                    },
                    "ContentVersion" : {
                        "@versionId" : "1",
                        "@lastVersion" : "true",
                        "@lastPublished" : "true",
                        "Url" : [{
                                "@type" : "XML",
                                "@size" : "2706269",
                                "#cdata-section" : "https://aaa.yyy.com/docs/xml/1c420081-0ce5-4959-91d3-848bbbccd3e3.xml"
                            }, {
                                "@type" : "HTML",
                                "@size" : "32445",
                                "#cdata-section" : "https://aaa.yyy.com/docs/html/1c420081-0ce5-4959-91d3-848bbbccd3e3.html"
                            }, {
                                "@type" : "PDF",
                                "@size" : "887608",
                                "#cdata-section" : "https://aaa.yyy.com/docs/pdf/1c420081-0ce5-4959-91d3-848bbbccd3e3.pdf"
                            }, {
                                "@type" : "ALTERNATE PDF",
                                "@size" : "34366",
                                "#cdata-section" : "https://aaa.yyy.com/docs/shortPdf/1c420081-0ce5-4959-91d3-848bbbccd3e3.pdf"
                            }
                        ]
                    }
                }
            }
        ]
    }
}
from content in jsonFeed.SelectTokens("DataFeed.Content").SelectMany(i => i.ObjectsOrSelf())
let issuer = content
        .SelectToken("ContentData.ProductStatus.@productCategory")
        .SingleOrMultiple()
        .Select(t => (string)t)
        .ToArray()
where issuer.FirstOrDefault().Equals("Issuer")
select new 
{
    DocID = (string)content.SelectToken("ContentData.@docId"),
    Symbol = (string)content.SelectToken("ContentData.ProductStatus.Symbol.@PrimarySymbol"),
    BMId = (int)content.SelectToken("ContentData.ProductStatus.Symbol.@BmId"),
    PDFUrl = (string)content.SelectToken("ContentData.ContentVersion.Url[3].#cdata-section")
}).ToList();
我将遵循中的代码示例。我想选择以下内容,但前提是
Symbol
部分中只有一个发行人,并且最新版本的PDF url:

  • @docId
  • @primarycommbol
  • @BmId
  • #cdata部分
    (但仅适用于最新版本的PDF)
我的查询是这样的:

{
    "?xml" : {
        "@version" : "1.0",
        "@encoding" : "UTF-8"
    },
    "DataFeed" : {
        "@FeedName" : "content",
        "now" : "2016-10-17T14:11:38.00",
        "Content" : [{
                "ContentData" : {
                    "@statusType" : "Published",
                    "@isReleased" : "Yes",
                    "@docId" : "21756",
                    "ProductStatus" : {
                        "@productCategory" : "Issuer",
                        "@focus" : "Issuer",
                        "Symbol" : {
                            "@PrimarySymbol" : "GOOGL",
                            "@SecondarySymbol" : "Google",
                            "@BmId" : "1790"
                        }
                    },
                    "ContentVersion" : {
                        "@versionId" : "1",
                        "@lastVersion" : "true",
                        "@lastPublished" : "true",
                        "Url" : [{
                                "@type" : "XML",
                                "@size" : "146410",
                                "#cdata-section" : "https://aaa.yyy.com/docs/xml/708893eb-1fd9-4278-8b0e-e7e0738d8105.xml"
                            }, {
                                "@type" : "HTML",
                                "@size" : "46105",
                                "#cdata-section" : "https://aaa.yyy.com/docs/html/708893eb-1fd9-4278-8b0e-e7e0738d8105.html"
                            }, {
                                "@type" : "PDF",
                                "@size" : "1274885",
                                "#cdata-section" : "https://aaa.yyy.com/docs/pdf/708893eb-1fd9-4278-8b0e-e7e0738d8105.pdf"
                            }, {
                                "@type" : "ALTERNATE PDF",
                                "@size" : "60960",
                                "#cdata-section" : "https://aaa.yyy.com/docs/shortPdf/708893eb-1fd9-4278-8b0e-e7e0738d8105.pdf"
                            }
                        ]
                    }
                }
            }, {
                "ContentData" : {
                    "@statusType" : "Published",
                    "@isReleased" : "Yes",
                    "@docId" : "22457",
                    "ProductStatus" : {
                        "@productCategory" : "Issuer",
                        "@focus" : "Issuer",
                        "Symbol" : {
                            "@PrimarySymbol" : "AAPL",
                            "@SecondarySymbol" : "Apple",
                            "@BmId" : "1121"
                        }
                    },
                    "ContentVersion" : [{
                            "@versionId" : "1",
                            "@lastVersion" : "false",
                            "@lastPublished" : "false",
                            "Url" : [{
                                    "@type" : "XML",
                                    "#cdata-section" : "https://aaa.yyy.com/docs/version/708893eb-1fd9-4278-8b0e-e7e0738d8105.1476742297.xml"
                                }, {
                                    "@type" : "HTML",
                                    "#cdata-section" : "https://aaa.yyy.com/docs/version/708893eb-1fd9-4278-8b0e-e7e0738d8105.1476742297.html"
                                }, {
                                    "@type" : "PDF",
                                    "#cdata-section" : "https://aaa.yyy.com/docs/version/708893eb-1fd9-4278-8b0e-e7e0738d8105.1476742297.pdf"
                                }, {
                                    "@type" : "ALTERNATE PDF",
                                    "#cdata-section" : "https://aaa.yyy.com/docs/version/708893eb-1fd9-4278-8b0e-e7e0738d8105.1476742297_short.pdf"
                                }
                            ]
                        }, {
                            "@versionId" : "2",
                            "@lastVersion" : "true",
                            "@lastPublished" : "true",
                            "Url" : [{
                                    "@type" : "XML",
                                    "@size" : "174520",
                                    "#cdata-section" : "https://aaa.yyy.com/docs/xml/708893eb-1fd9-4278-8b0e-e7e0738d8105.xml"
                                }, {
                                    "@type" : "HTML",
                                    "@size" : "46105",
                                    "#cdata-section" : "https://aaa.yyy.com/docs/html/708893eb-1fd9-4278-8b0e-e7e0738d8105.html"
                                }, {
                                    "@type" : "PDF",
                                    "@size" : "1270150",
                                    "#cdata-section" : "https://aaa.yyy.com/docs/pdf/708893eb-1fd9-4278-8b0e-e7e0738d8105.pdf"
                                }, {
                                    "@type" : "ALTERNATE PDF",
                                    "@size" : "60960",
                                    "#cdata-section" : "https://aaa.yyy.com/docs/version/708893eb-1fd9-4278-8b0e-e7e0738d8105.1577742297_short.pdf"
                                }
                            ]
                        }
                    ]
                }
            }, {
                "ContentData" : {
                    "@statusType" : "Published",
                    "@isReleased" : "Yes",
                    "@docId" : "22501",
                    "ProductStatus" : {
                        "@productCategory" : "Issuer",
                        "@focus" : "Issuer",
                        "Symbol" : [{
                                "@PrimarySymbol" : "AAA",
                                "@SecondarySymbol" : "Some name 1",
                                "@BmId" : "561"
                            }, {
                                "@PrimarySymbol" : "ABC",
                                "@SecondarySymbol" : "Some Name 2",
                                "@BmId" : "29"
                            }, {
                                "@PrimarySymbol" : "BBB",
                                "@SecondarySymbol" : "Some Name 3",
                                "@BmId" : "36"
                            }, {
                                "@PrimarySymbol" : "CCC",
                                "@SecondarySymbol" : "Some Name 4",
                                "@BmId" : "624"
                            }
                        ]
                    },
                    "ContentVersion" : {
                        "@versionId" : "1",
                        "@lastVersion" : "true",
                        "@lastPublished" : "true",
                        "Url" : [{
                                "@type" : "XML",
                                "@size" : "2706269",
                                "#cdata-section" : "https://aaa.yyy.com/docs/xml/1c420081-0ce5-4959-91d3-848bbbccd3e3.xml"
                            }, {
                                "@type" : "HTML",
                                "@size" : "32445",
                                "#cdata-section" : "https://aaa.yyy.com/docs/html/1c420081-0ce5-4959-91d3-848bbbccd3e3.html"
                            }, {
                                "@type" : "PDF",
                                "@size" : "887608",
                                "#cdata-section" : "https://aaa.yyy.com/docs/pdf/1c420081-0ce5-4959-91d3-848bbbccd3e3.pdf"
                            }, {
                                "@type" : "ALTERNATE PDF",
                                "@size" : "34366",
                                "#cdata-section" : "https://aaa.yyy.com/docs/shortPdf/1c420081-0ce5-4959-91d3-848bbbccd3e3.pdf"
                            }
                        ]
                    }
                }
            }
        ]
    }
}
from content in jsonFeed.SelectTokens("DataFeed.Content").SelectMany(i => i.ObjectsOrSelf())
let issuer = content
        .SelectToken("ContentData.ProductStatus.@productCategory")
        .SingleOrMultiple()
        .Select(t => (string)t)
        .ToArray()
where issuer.FirstOrDefault().Equals("Issuer")
select new 
{
    DocID = (string)content.SelectToken("ContentData.@docId"),
    Symbol = (string)content.SelectToken("ContentData.ProductStatus.Symbol.@PrimarySymbol"),
    BMId = (int)content.SelectToken("ContentData.ProductStatus.Symbol.@BmId"),
    PDFUrl = (string)content.SelectToken("ContentData.ContentVersion.Url[3].#cdata-section")
}).ToList();
现在,我可以为谷歌成功获取所有字段,但我无法为苹果获取PDFUrl(因为我不知道如何获取最新版本)。另外,如何完成Where子句,使其仅选择带有1个符号的文档?因此,最后,我的列表将如下图所示:

您可以合并和,以计算和过滤中间结果

下面的查询使用了这种技术,还查询了
@type
等于
“PDF”
的URL,而不是将其硬编码为索引3(顺便说一下,它实际上位于索引2):

如果您确实想要替代PDF url,请设置

string pdfUrlType = "ALTERNATE PDF";
样品

如果您确定每个
“Content”
记录(其中
“@lastVersion”
等于
“true”
的记录)中的
ContentVersion
的最新版本按文档顺序保证为最后一个版本,则可以按如下方式简化查询:

     let lastContentVersion = content
        .SelectToken("ContentData.ContentVersion")
        .SingleOrMultiple()
        .LastOrDefault()
您想要
“@type”:“PDF”
URL,还是
“@type”:“备用PDF”
URL?因为您当前得到的是索引3中的一个,这是另一个PDF url。只需
“@type”:“PDF”。