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",

 ....

This is the JSON response

I try to parse it with like so:

 dynamic dynObj = JsonConvert.DeserializeObject(output);

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

            return output;

Which gives contains of the results-array. However when i try to get just the name type in the results with :

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

The set is empty. How do I get the name value from the results array?

3
  • 1
    Please show a short but complete program demonstrating the problem. Note that to keep it short, you really won't need much of that JSON... Commented Apr 24, 2015 at 12:33
  • 1
    (In particular, the code works for me...) Commented Apr 24, 2015 at 12:36
  • 1
    It works. Tested with v6.0.8. Commented Apr 24, 2015 at 12:43

3 Answers 3

3

You could use http://json2csharp.com to generate this set of POCOs from your 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; }
}

After that just call var a = Jsonconvert.Deserialize<RootObject>(output); and you can get your required value by var output = a.Results[0].name;

I'd advise not to use the dynamic keyword unless your json is truly dynamic.

Sign up to request clarification or add additional context in comments.

2 Comments

You can also use EDIT menu -> Paste Special -> Paste JSON as Classes
Linkwise, JSONLint.com is a bit more worthwhile - it helps point out errors and how to fix it, one by one for faulty JSON
0

Your question doesn't need an answer but anyway here it is (working code):

https://dotnetfiddle.net/LioznP

Comments

0

After rebooting my dev machine, this code works. I will classify this as: obsure

THX FOR THE SUPPORT!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.