2016-12-02 74 views
0

我想使用c#從下面的對象中提取所有ID。 需要在visual studio webtest中提取響應值。 使用提取事件獲取JSON。如何從C#中的以下JSON對象中提取ID?

{ 
     "d": [ 
      { 
       "__type": "QuestionZoneEditor.Entities.QuestionTag", 
       "Id": 2080, 
       "Name": "01", 
       "Items": [ 
       "1a", 
       "1b", 
       "1c", 
       "1d" 
       ] 
      }, 
      { 
       "__type": "QuestionZoneEditor.Entities.QuestionTag", 
       "Id": 2081, 
       "Name": "02", 
       "Items": [ 
       "2a(i)", 
       "2a(ii)", 
       "2b", 
       "2c" 
       ] 
      }, 
      { 
       "__type": "QuestionZoneEditor.Entities.QuestionTag", 
       "Id": 2082, 
       "Name": "03", 
       "Items": [ 
       "3a", 
       "3b", 
       "3c" 
       ] 
      } 
      } 
      ] 
     } 

回答

3

它反序列化JObject是採取從JArray所有JObject後,打印Id

var result = JsonConvert.DeserializeObject<JObject>(json); 

foreach(JObject obj in result["d"]) 
{ 
    Console.WriteLine(obj["Id"]); 
} 

完整的示例:dotNetFiddle

+0

添加Json.NET的參考:www.newtonsoft.com / –