2016-10-20 19 views
1

另一個數據檢索這裏是我的JSON字符串的樣子:JSON.Net - 從多態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" 
          } 
         ] 
        } 
       } 
      } 
     ] 
    } 
} 

我從下面的LINQ to Json data retrieval from polymorphic json代碼示例。我想選擇以下但前提是在Symbol部分單個發行人及PDF網址爲最新版本:

  • @docId
  • @PrimarySymbol
  • @BmId
  • #cdata-section(但僅限於PDF文件的最新版本)

這是我的查詢的樣子:

from content in jsonFeed.SelectTokens("DataFeed.Content").SelectMany(i => i.ObjectsOrSelf()) 
let issuer = content 
     .SelectToken("[email protected]") 
     .SingleOrMultiple() 
     .Select(t => (string)t) 
     .ToArray() 
where issuer.FirstOrDefault().Equals("Issuer") 
select new 
{ 
    DocID = (string)content.SelectToken("[email protected]"), 
    Symbol = (string)content.SelectToken("[email protected]"), 
    BMId = (int)content.SelectToken("[email protected]"), 
    PDFUrl = (string)content.SelectToken("ContentData.ContentVersion.Url[3].#cdata-section") 
}).ToList(); 

現在,我可以成功獲取Google的所有字段,但我無法獲得PDFUrl for Apple(因爲我不知道如何獲得最新版本)。另外,如何填寫Where子句,以便只選擇帶有1個符號的文檔?所以,到最後,我的名單將在下面的圖片:

Results

+0

你想要「@type」:「PDF」URL還是「@type」:「ALTERNATE PDF」URL?因爲您目前正在索引3處獲得索引,這是另一個PDF網址。 – dbc

+0

只是'「@type」:「PDF」。「 –

回答

1

您可以結合letwhere計算和對中間結果進行過濾。

下面的查詢使用這種技術,並且還查詢與@type等於"PDF"的URL,而不是硬編碼是在指數3(它實際上是在指數2,順便說一句):

string pdfUrlType = "PDF"; 
var query = 
    (from content in jsonFeed.SelectTokens("DataFeed.Content").SelectMany(i => i.ObjectsOrSelf()) 
    let issuer = content 
      .SelectToken("[email protected]") 
      .SingleOrMultiple() 
      .Select(t => (string)t) 
      .ToArray() 
    where issuer.FirstOrDefault().Equals("Issuer") 
    // Get the @PrimarySymbol where there is exactly one, or null otherwise. 
    let symbol = (string)content.SelectToken("[email protected]") 
    // Skip content without a single primary symbol 
    where symbol != null 
    // Get the last ContentVersion record in the JSON document. 
    let lastContentVersion = content 
     .SelectToken("ContentData.ContentVersion") 
     .SingleOrMultiple() 
     .Where(v => (bool?)v.SelectToken("@lastVersion") == true) 
     .LastOrDefault() 
    // Skip content with no ContentVersion 
    where lastContentVersion != null 
    // Get the PDF Url record 
    let pdfUrl = lastContentVersion.SelectToken("Url") 
     .SingleOrMultiple() 
     .Where(u => (string)u.SelectToken("@type") == pdfUrlType) 
     .FirstOrDefault() 
    // Skip content with no PDF Url record 
    where pdfUrl != null 
    select new 
    { 
     DocID = (string)content.SelectToken("[email protected]"), 
     Symbol = symbol, 
     BMId = (int?)content.SelectToken("[email protected]"), 
     PDFUrl = (string)pdfUrl.SelectToken("#cdata-section") 
    }).ToList(); 

如果您確實需要替代PDF網址,請設置爲

string pdfUrlType = "ALTERNATE PDF"; 

樣本fiddle

如果您是確保每個"Content"記錄(一個用於其"@lastVersion"等於"true")的ContentVersion的最新版本,保證是由文檔順序是最後一個,你可以簡化查詢,如下所示:

 let lastContentVersion = content 
     .SelectToken("ContentData.ContentVersion") 
     .SingleOrMultiple() 
     .LastOrDefault() 
+0

謝謝@dbc。 –