2016-06-21 43 views
-2

我找到了庫Newtonsoft.Json;它提供了很多與JSON對象的可能性,但我不能從JSON文件**解析Jarray? 樣品JSON文件:轉換解析配置文件JSON java到c#

{ 「0」:[-12.770680147058824,1.846047794117647,-54.265625,55.77863587895704]中, 「1」:[-18.388229927007298,6.5360401459854014,-52.65647810218978,56.156491225545878], 「2」:[-20.042738970588236, 12.849264705882353,-46.678308823529413,52.399231898471129], 「3」:[-38.242244525547449,15.836222627737227,-40.48357664233577,57.897972254845804], 「4」:[-33.016879562043798,6.3001824817518246,-38.179288321167881,50.867127813832226], 「5」:[-31.816605839416059,4.7130474452554747, -33.626824817518248,46.532489409766747]

+1

請說明它是什麼,你正在嘗試做的,而忽略你在一個有代碼先前的語言。你真的想問:「我怎樣才能解析從使用C#的JSON文件數組」 – AlBlue

+0

這足以讓你開始? https://msdn.microsoft.com/en-us/library/cc197957(v=vs.95).aspx – Marco7757

+0

是的,事實上,我想從JSON文件解析一個Jarray – djo

回答

0

您提供的示例JSON不是JArray。但是,您可以將該JSON解析爲JObject並單獨獲取JArray。

var jObject = JObject.Parse(the_json);會給你一個JObject反映你的JSON字符串。

var jObject = JObject.Parse(@"{""0"": [-12.770680147058824, 1.846047794117647, -54.265625, 55.77863587895704], ""1"": [-18.388229927007298, 6.5360401459854014, -52.65647810218978, 56.156491225545878], ""2"": [-20.042738970588236, 12.849264705882353, -46.678308823529413, 52.399231898471129], ""3"": [-38.242244525547449, 15.836222627737227, -40.48357664233577, 57.897972254845804], ""4"": [-33.016879562043798, 6.3001824817518246, -38.179288321167881, 50.867127813832226], ""5"": [-31.816605839416059, 4.7130474452554747, -33.626824817518248, 46.532489409766747]}"); 
    foreach (var property in jObject.Properties()) 
    { 
     var array = property.Value as JArray; 
    } 
0

你可以只解析此JSON爲Dictionary<string, List<double>>

var dict = JsonConvert.DeserializeObject<Dictionary<string, List<double>>>(yourJson); 

小提琴:https://dotnetfiddle.net/W6ODpE