2013-05-20 36 views
9

Tôi có một chuỗi json như,lặp qua mảng json trong C#

{"objectType" : "Subscriber", "objectList":[{"firstName":"name1","email":"[email protected]","address":"exampleAddress"},{"firstName":"name2","email":"[email protected]","address":"exampleAddress2"}]} 

tôi cần phải phân tích nó trong C# mã của tôi. Tôi đã thử,

JavaScriptSerializer json_serializer = new JavaScriptSerializer(); 
object routes_list = json_serializer.DeserializeObject(myjson here); 

Nhưng tôi không thể lặp lại mảng "objectList". Làm thế nào nó có thể được thực hiện?

+2

thể trùng lặp của http://stackoverflow.com/questions/11132288/iterating-over-json-object-in-c-sharp – Robert

+0

bạn có thể chỉ cho bạn ' TargetType' xin vui lòng – yogi

Trả lời

22
var jsonObj = new JavaScriptSerializer().Deserialize<RootObj>(json); 
foreach (var obj in jsonObj.objectList) 
{ 
    Console.WriteLine(obj.address); 
} 


public class ObjectList 
{ 
    public string firstName { get; set; } 
    public string email { get; set; } 
    public string address { get; set; } 
} 

public class RootObj 
{ 
    public string objectType { get; set; } 
    public List<ObjectList> objectList { get; set; } 
} 

Gợi ý: Bạn có thể sử dụng this site để chuyển đổi chuỗi json của bạn để C# lớp

EDIT

sử dụng Json.Net

dynamic jsonObj = JsonConvert.DeserializeObject(json); 

foreach (var obj in jsonObj.objectList) 
{ 
    Console.WriteLine(obj.address); 
} 
+0

Cảm ơn bạn đã trả lời nhanh chóng. Làm việc tốt. Có thể làm việc mà không có lớp ObjectList và RootObj không? –

+0

@WingsOfFire Xem chỉnh sửa của tôi ... – I4V

+0

Tôi có cần thêm bất kỳ dll nào cho điều đó không? –

3
var routes_list = (Dictionary<string, object>)json_serializer.DeserializeObject(myjson); 

foreach (var record in routes_list) 
{ 
    Console.WriteLine(record); 
} 
1

này đã làm việc cho tôi, cải đổi JSON sang YA ML yếu

string JSONDeserialized {get; set;} 
    public int indentLevel; 

    private bool JSONDictionarytoYAML(Dictionary<string, object> dict) 
    { 
     bool bSuccess = false; 
     indentLevel++; 

     foreach (string strKey in dict.Keys) 
     { 
      string strOutput = "".PadLeft(indentLevel * 3) + strKey + ":"; 
      JSONDeserialized+="\r\n" + strOutput; 

      object o = dict[strKey]; 
      if (o is Dictionary<string, object>) 
      { 
       JSONDictionarytoYAML((Dictionary<string, object>)o); 
      } 
      else if (o is ArrayList) 
      { 
       foreach (object oChild in ((ArrayList)o)) 
       { 
        if (oChild is string) 
        { 
         strOutput = ((string)oChild); 
         JSONDeserialized += strOutput + ","; 
        } 
        else if (oChild is Dictionary<string, object>) 
        { 
         JSONDictionarytoYAML((Dictionary<string, object>)oChild); 
         JSONDeserialized += "\r\n"; 
        } 
       } 
      } 
      else 
      { 
       strOutput = o.ToString(); 
       JSONDeserialized += strOutput; 
      } 
     } 

     indentLevel--; 

     return bSuccess; 

    } 

sử dụng

 Dictionary<string, object> JSONDic = new Dictionary<string, object>(); 
     JavaScriptSerializer js = new JavaScriptSerializer(); 

      try { 

      JSONDic = js.Deserialize<Dictionary<string, object>>(inString); 
      JSONDeserialized = ""; 

      indentLevel = 0; 
      DisplayDictionary(JSONDic); 

      return JSONDeserialized; 

     } 
     catch (Exception) 
     { 
      return "Could not parse input JSON string"; 
     } 
+0

Cảm ơn bạn đã trả lời. –