2017-02-28 80 views
1

我有一個JSON文件,我希望獲得屬性值land_use_type內部的數組屬性records。我的第一次嘗試是使用LINQ到JSON與Newtonsoft參考,但始終LINQ給我這個消息:C#linq to json如何讀取數組中的屬性值

System.Collections.Generic.List'1 [Newtonsoft.Json.Linq.JToken]

C#代碼:

string path = @"C:\...\json1.json"; 

     using (StreamReader read = File.OpenText(path)) 
     { 
      JObject jsondata = (JObject)JToken.ReadFrom(new JsonTextReader(read)); 

      string bearing = (string)jsondata["bearing"]; 
      string distance = (string)jsondata["distance"]; 
      string source = (string)jsondata["source"]; 

      var Land_use = from x in jsondata["records"] 
          select x["land_use_type"]; 




      Console.WriteLine(String.Format("{0}\n{1}\n{2}\n{3}", bearing, distance, source, Land_use.ToList())); 

JSON文件:

{ 
    ... 
    "records": [ 
{ 
    "crop_primary": 0, 
    "crop_primary_coverage": null, 
    "crop_secondary": 0, 
    "crop_secondary_coverage": null, 
    "crop_tertiary": 0, 
    "crop_tertiary_coverage": null, 
    "date_created": "2017-02-27T20:25:28.981681", 
    "date_updated": "2017-02-27T20:25:28.981681", 
    "history": [ 
    { 
     "data": "{\"crop_primary\": 0, \"crop_primary_coverage\": null, \"crop_secondary\": 0, \"crop_secondary_coverage\": null, \"crop_tertiary\": 0, \"crop_tertiary_coverage\": null, \"date_created\": \"2017-02-27T20:25:28.981681\", \"date_updated\": \"2017-02-27T20:25:28.981681\", \"id\": 172812, \"intensity\": 0, \"land_use_type\": 3, \"location_id\": 272769, \"month\": 2, \"ndvi\": null, \"ndvi_mean\": null, \"protected\": false, \"rating\": 0, \"scale\": -1, \"source_class\": null, \"source_description\": \"mobile_application\", \"source_id\": null, \"source_type\": \"ground\", \"user_id\": 140, \"water\": 0, \"year\": 2017}", 
     "date_edited": "2017-02-27T20:25:29.359834", 
     "id": 66588, 
     "record_id": 172812, 
     "user_id": 140 
    } 
    ], 
    "id": 172812, 
    "intensity": 0, 
    "land_use_type": 3, 
    "location_id": 272769, 
    "month": 2, 
    "ndvi": null, 
    "ndvi_mean": null, 
    "protected": false, 
    "rating": 0, 
    "scale": -1, 
    "source_class": null, 
    "source_description": "mobile_application", 
    "source_id": null, 
    "source_type": "ground", 
    "user_id": 140, 
    "water": 0, 
    "year": 2017 
} 
], 
... 

} 

回答

0

你可以試試這個你的情況:

var Land_use = jsondata["records"].Values("land_use_type").Single().ToString(); 

Land_use"3"

或者,你可以這樣做:

var Land_use = var Land_use = jsondata["records"].Values("land_use_type").FirstOrDefault()?.Value<string>(); 
+0

一個字符串謝謝kat1330;) –

+0

沒問題:)請注意,你必須處理結果恰當,因爲'。單()'拋出一個異常如果序列是空的或者有很多! – kat1330

+0

是否有任何其他方式來做單旁邊() –

0

如果你期待records包含多個項目的數組,你可能想用這個辦法。

var Land_use = from x in jsondata["records"] 
     select x.Value<string>("land_use_type"); 

    Console.WriteLine($"Land use: {string.Join(",", Land_use)}"); 
    Console.ReadLine(); 

這會給你一個輸出Land use: 3,4 - 如果有多個。如果沒有結果,那麼您只會落得像Land use:

+0

有什麼方法可以檢查元素是否爲空 –

+0

@JacobC。你想檢查x是否爲null或Land_use?要檢查'x'是否爲空,您可以通過在linq查詢中添加where子句並執行'where x.Value (「land_use_type」)!= null – kghantous