使用newtonsoft json反序列化器解析复杂的JSON字符串

时间:2015-04-24 12:28:12

标签: c# json

{
  "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响应

我试着像这样解析它:

 dynamic dynObj = JsonConvert.DeserializeObject(output);

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

            return output;

其中包含结果数组。但是,当我尝试在结果中获取名称类型:

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

该集合为空。如何从结果数组中获取名称值?

3 个答案:

答案 0 :(得分:3)

您可以使用http://json2csharp.com从json生成这组POCO:

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; }
}

之后只需调用var a = Jsonconvert.Deserialize<RootObject>(output);即可获得var output = a.Results[0].name;所需的值

我建议不要使用dynamic关键字,除非你的json真的是动态的。

答案 1 :(得分:0)

您的问题不需要答案,但无论如何它是(工作代码):

https://dotnetfiddle.net/LioznP

答案 2 :(得分:0)

重新启动我的开发机器后,此代码有效。我将其归类为:obsure

支持THX!