Как распарсить Json - C# (181093)
Формулировка задачи:
Здравствуйте! Подскажите пожалуйста, как распарсить этот json (получаю в метод "public IHttpActionResult UpdateClient(dynamic client)" из клиентской части).
На выходе необходимо получить два класса:
Получаю пустой ClientInfoDTO cl, что не так?
Возможно для этого необходимо на стороне JS формировать в ручную объект ClientInfoDTO?
Листинг программы
- {{
- "client": [
- {
- "name": "ClientGuid",
- "value": "1b8fd911-c7ac-4341-b104-c790504854ca"
- },
- {
- "name": "F",
- "value": "Прокопенко"
- },
- {
- "name": "I",
- "value": "Тамара"
- },
- {
- "name": "O",
- "value": "Георгиевна"
- },
- {
- "name": "DateOfBirth",
- "value": "01.10.1944"
- },
- {
- "name": "Gender",
- "value": "true"
- },
- {
- "name": "advertising",
- "value": ""
- },
- {
- "name": "Comment",
- "value": ""
- },
- {
- "name": "Address",
- "value": ""
- },
- {
- "name": "doc_Serial",
- "value": ""
- },
- {
- "name": "doc_Number",
- "value": ""
- },
- {
- "name": "doc_Date",
- "value": ""
- },
- {
- "name": "doc_Issue",
- "value": ""
- },
- {
- "name": "IsSmsAgree",
- "value": "false"
- },
- {
- "name": "IsMailAgree",
- "value": "false"
- }
- ]
- }}
Листинг программы
- public class ClientInfoDTO
- {
- /// <summary>
- /// Guid клиента
- /// </summary>
- [JsonProperty("ClientGuid")]
- public Guid ClientGuid { get; set; }
- /// <summary>
- /// Фамилия клиента
- /// </summary>
- [JsonProperty("F")]
- public string F { get; set; }
- /// <summary>
- /// Имя клиента
- /// </summary>
- [JsonProperty("I")]
- public string I { get; set; }
- /// <summary>
- /// Отчество клиента
- /// </summary>
- [JsonProperty("O")]
- public string O { get; set; }
- /// <summary>
- /// Полные ФИО клиента
- /// </summary>
- public string Fio { get { return ((this.F + " " + this.I).Trim() + " " + this.O).Trim(); } }
- /// <summary>
- /// Пол клиента (true - M, false - Ж)
- /// </summary>
- [JsonProperty("Gender")]
- public bool Gender { get; set; }
- /// <summary>
- /// Id возрастной группы, к которой принадлежит клиент
- /// </summary>
- [JsonProperty("AgeGroupId")]
- public long AgeGroupId { get; set; }
- /// <summary>
- /// Дата рождения клиента
- /// </summary>
- [JsonProperty("DateOfBirth")]
- public DateTime DateOfBirth { get; set; }
- /// <summary>
- /// Индекс телефона, отмеченного главным
- /// </summary>
- [JsonProperty("MainPhone")]
- public int MainPhone { get; set; }
- /// <summary>
- /// Считаем, что элемент с индексом [0] - это
- /// всегда телефон, определенный с помощью АОН
- /// </summary>
- [JsonProperty("Phones")]
- public List<string> Phones { get; set; }
- /// <summary>
- /// Другие контакты (почта, Skype и прочее)
- /// </summary>
- [JsonProperty("Contacts")]
- public List<string> Contacts { get; set; }
- /// <summary>
- /// Комментарий
- /// </summary>
- [JsonProperty("Comment")]
- public string Comment { get; set; }
Листинг программы
- public class ClientCard : IValidatableObject
- {
- [JsonProperty("Id")]
- [Range(1, long.MaxValue)]
- public long Id { get; set; }
- [JsonProperty("ClientGuid")]
- public Guid ClientGuid { get; set; }
- [JsonProperty("Address")]
- public string Address { get; set; }
- [JsonProperty("doc_Name")]
- public string doc_Name { get; set; }
- [JsonProperty("doc_Serial")]
- public string doc_Serial { get; set; }
- [JsonProperty("doc_Number")]
- public string doc_Number { get; set; }
- [JsonProperty("doc_Issue")]
- public string doc_Issue { get; set; }
- [JsonProperty("doc_Date")]
- public DateTime doc_Date { get; set; }
- [JsonProperty("adv_InviteId")]
- [Range(1, int.MaxValue)]
- public int adv_InviteId { get; set; }
- [JsonProperty("advTitle")]
- public string AdvTitle { get; set; }
- [JsonProperty("IsMailAgree")]
- public bool IsMailAgree { get; set; }
- [JsonProperty("IsSmsAgree")]
- public bool IsSmsAgree { get; set; }
- [JsonProperty("InputTime")]
- public DateTime InputTime { get; set; }
ClientInfoDTO cl = Newtonsoft.Json.JsonConvert.DeserializeObject(client);
не работает
пишет Наиболее подходящий перегруженный метод для System.Web.Script.Serialization.JavaScriptSerializer.Deserialize
Пробую так:
Листинг программы
- string q = client.ToString();
- ClientInfoDTO cl = Newtonsoft.Json.JsonConvert.DeserializeObject<ClientInfoDTO>(q);
В идеале было бы сразу в метод получать объект нужного класса (public IHttpActionResult UpdateClient([FromBody] ClientInfoDTO client)), но такая штука тож не работает, приходит пустой объект...
В идеале было бы сразу в метод получать объект нужного класса (public IHttpActionResult UpdateClient([FromBody] ClientInfoDTO client))
Решение задачи: «Как распарсить Json»
textual
Листинг программы
- var toSend = Tools.getFormatFormData($("#UpdateForm").serializeArray());
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д