2017-03-15 51 views
0

返回的JSON我在asp.net VB以下JSON:遍歷從API

Dim jsonString As String = "{'results':[ {'comments': 'some text', 'date' : 'some date', 'user':'aaa'},{'comments': 'some text2', 'date2' : 'some date2', 'user':'aaa2'}]} " 

Dim json As JObject = JObject.Parse(jsonString) 

我怎麼能循環,雖然這樣的評論的價值?

謝謝

+0

你有什麼嘗試?您是否閱讀過Newtonsoft文檔頁面[使用LINQ查詢JSON](http://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm)和[使用SelectToken查詢JSON](http://www.newtonsoft。 COM/JSON /幫助/ HTML/SelectToken.htm)? – dbc

+0

我其實已經想通了。但是這隻適用於框架4.0或更高版本。 – user3570022

+0

Dim jsonString As String =「{'results':[{'comments':'some text','date':'some date','user':'aaa'},{'comments':'some text2' , 'DATE2': '一些DATE2', '用戶': 'aaa2'}]}「 昏暗JSON作爲JObject = JObject.Parse(jsonString) 昏暗commToken作爲JToken 昏暗commValue作爲字符串 對於每行中的JSON (「results」)ToList() commToken = Row(「comments」) commValue = DirectCast(commToken,JValue).Value Next – user3570022

回答

0

的Newtonsoft文檔頁面Querying JSON with LINQQuerying JSON with SelectToken描述如何查詢嵌套一次加載JObject層次內的值。

例如,使用SelectTokens(),您可以查詢註釋值如下:

' Get all comment values, convert them to simple strings, and store in an array: 
Dim comments = json.SelectTokens("results[*].comments") _ 
    .Select(Function(t) t.ToString()) _ 
    .ToArray() 

這裏我使用通配符[*]匹配"results"陣列中的所有元素。這是JSONPath query syntax的標準運營商,其SelectTokens()支持。

下面是使用LINQ等效邏輯:

Dim query = From item In json("results") 
    Let comment = item("comments") 
    Where comment IsNot Nothing 
    Select CType(comment, String)    
Dim comments = query.ToArray() 

fiddle樣品。