2015-04-24 4 views
0
{ 
    "type": "xxx", 
    "version": "1", 
    "totalResults": -1, 
    "resultsFrom": 0, 
    "previousResultsUri": null, 
    "nextResultsUri": "xxx", 
    "exceptionNoticeUri": null, 
    "results": [ 
    { 
     "businessId": "xxx", 
     **"name": "xxx",** <---- THIS ONE 
     "registrationDate": "xx", 
     "companyForm": "xxx", 
     "detailsUri": null, 
     "bisDetailsUri": "xxx", 
     "language": "xx", 
     "latestRegistrationDate": "xxx", 
     "checkDate": "xxx", 

.... 

Это ответ JSONАнализировать комплекс JSON строку с newtonsoft JSon десериализатор

Я пытаюсь разобрать его так:

dynamic dynObj = JsonConvert.DeserializeObject(output); 

      output = Convert.ToString(dynObj.results[0]); 

      return output; 

Что дает содержит от результатов массива. Однако, когда я пытаюсь получить только название типа в результатах с:

output = Convert.ToString(dynObj.results[0].name); 

Набор пуст. Как получить значение имени из массива результатов?

+1

Пожалуйста, покажите короткую, но * полную * программу, демонстрирующую проблему. Обратите внимание, что для этого вам не понадобится большая часть этого JSON ... –

+1

(В частности, код работает для меня ...) –

+1

Работает для меня тоже! –

ответ

3

Вы можете использовать http://json2csharp.com для создания этого набора Pocos из вашего JSON:

public class Name 
{ 
    public int order { get; set; } 
    public string name { get; set; } 
    public string registrationDate { get; set; } 
    public object endDate { get; set; } 
    public object language { get; set; } 
} 

public class CompanyForm 
{ 
    public string type { get; set; } 
    public string registrationDate { get; set; } 
} 

public class Address 
{ 
    public string street { get; set; } 
    public string postCode { get; set; } 
    public int type { get; set; } 
    public string city { get; set; } 
    public string country { get; set; } 
    public string website { get; set; } 
    public object phone { get; set; } 
    public object fax { get; set; } 
    public string registrationDate { get; set; } 
    public object endDate { get; set; } 
} 

public class RegisteredOffice 
{ 
    public string registeredOffice { get; set; } 
    public string language { get; set; } 
    public string registrationDate { get; set; } 
    public object endDate { get; set; } 
} 

public class Result 
{ 
    public string businessId { get; set; } 
    public string name { get; set; } 
    public string registrationDate { get; set; } 
    public string companyForm { get; set; } 
    public object detailsUri { get; set; } 
    public string bisDetailsUri { get; set; } 
    public string language { get; set; } 
    public string latestRegistrationDate { get; set; } 
    public string checkDate { get; set; } 
    public List<Name> names { get; set; } 
    public List<object> auxiliaryNames { get; set; } 
    public List<CompanyForm> companyForms { get; set; } 
    public List<Address> addresses { get; set; } 
    public List<object> publicNotices { get; set; } 
    public List<RegisteredOffice> registeredOffices { get; set; } 
} 

public class RootObject 
{ 
    public string type { get; set; } 
    public string version { get; set; } 
    public int totalResults { get; set; } 
    public int resultsFrom { get; set; } 
    public object previousResultsUri { get; set; } 
    public string nextResultsUri { get; set; } 
    public object exceptionNoticeUri { get; set; } 
    public List<Result> results { get; set; } 
} 

После этого просто вызовите вар a = Jsonconvert.Deserialize<RootObject>(output);, и вы можете получить нужное значение var output = a.Results[0].name;

Я бы посоветовал не используйте ключевое слово dynamic, если ваш json по-настоящему динамичен.

+3

Вы также можете использовать меню EDIT -> Вставить Special -> Paste JSON as Classes – Plutonix

+0

Whoah, я не знал об этом - спасибо! –

+0

Linkwise, JSONLint.com немного более полезен - он помогает указывать на ошибки и как их исправлять, один за другим для неисправного JSON – Plutonix

0

После перезагрузки Dev машины, этот код работает. Я буду классифицировать это как: obsure

THX ДЛЯ ПОДДЕРЖКИ!

Смежные вопросы