2013-09-22 87 views
0

如何在使用linq解析和過濾後將JSON數據添加到列表?我只想將一些數據添加到列表中。在C#中解析後將json數據添加到列表中

public class jsonData 
{ 
    public List<rootelem> data { get; set; } 
} 

public class rootelem 
{ 
    public List<employeeObj> employee { get; set; } 
} 

public class employeeObj 
{ 
    public List<DataElement> empdetails { get; set; } 
} 

public class DataElement 
{ 
    public ulong empid { get; set; } 
    public string empname { get; set; } 
    public string empdept { get; set; } 
    public List<empphone> empphone { get; set; } 
} 

public class empphone 
{ 
    public string home { get; set; } 
    public string mobile { get; set; } 
} 
static void Main(string[] args) 
{ 
    WebClient c = new WebClient(); 

    var jsonstring = @"{""data"":[{""employee"":[{""empdetails"":[{""empid"":""98977"",""empname"":""John"",""empdept"":""HR"",""empphone"":[{""home"":""868685768"",""mobile"":""89886654""}] }] }] }] }"; 

    jsonData myData = JsonConvert.DeserializeObject<jsonData>(jsonstring); 

    foreach (var items in myData.data[0].employee[0].empdetails) 
    { 
     Console.WriteLine(items.empid); 
     Console.WriteLine(items.empname); 
     Console.WriteLine(items.empdept); 
    } 

     Console.ReadKey(); 

} 
+0

你能提供代碼來解釋你的問題嗎?到目前爲止,我並沒有真正明白你的問題 – MichaC

回答

0

你在找什麼?

var myData = new JsonData 
{ 
    Data = JsonConvert 
      .DeserializeObject<List<rootelem>>(jsonstring) 
      .Where(<your predicate>); 
}; 
相關問題