2016-01-15 4 views
0
  String zohoResponse = @" 

    [ 
     { 
     'Title':'Mr.', 
     'First Name':'ram', 
     'Last Name':'chang', 
     'Employed at Trade Client Name':'mile travel', 
     'Telephone':'657498333', 
     'E-mail':'[email protected]', 
     'Fax':' ', 
     'Street':'123 Street', 
     'City':'Winnipeg', 
     'State/Province':'Manitoba', 
     'Country':'Canada', 
     'Postcode':'R4T 600', 
     'Agent ID':70, 
     'Primary Sales Agent':' ', 
     'Employed at From':'09/12/2008', 
     'Employed at To':'09/12/2028' 
     } 
      ] 
     "; 

    dynamic jsonObj = JsonConvert.DeserializeObject(zohoResponse); 


     xmlData = new XDocument(
          `enter code here` new XElement("Leads", 
            new XElement("row", new XAttribute("no", "1"), 
            new XElement("FL", new XAttribute("val", "Lead Source"), jsonObj["Last Name"]), 
           new XElement("FL", new XAttribute("val", "Title"), jsonObj["First Name"]) 
          ))); 

Я хочу, чтобы получить динамические значения индекса. я использовал новыйКак получить значения по индексу из динамического объекта

XElement ("FL", новый XAttribute ("Вал", "Title"), jsonObj.GetType(). GetProperty ("Title"). ПолучитьЗначение (jsonObj, NULL)) , но я все еще получая исключение при извлечении значений по индексу

ответ

0

jsonObj - это массив, поэтому вам нужно сначала добраться до первого элемента массива, прежде чем запрашивать json-литерал.

dynamic jsonObj = JsonConvert.DeserializeObject(zohoResponse); 
dynamic arr = jsonObj.First; 

var xmlData = new XDocument(
        new XElement("Leads", 
          new XElement("row", new XAttribute("no", "1"), 
          new XElement("FL", new XAttribute("val", "Lead Source"), arr["Last Name"]), 
         new XElement("FL", new XAttribute("val", "Title"), arr["First Name"]) 
        )));