0

In the Norway they have a register of organisations (brreg.no), which you can access through a webservice. If an organisation owns one or more other organisations, you can also access a list of those.

Now my problem is this: I would like to create a class for the parrent organisation and populate it with DataContractJsonSerializer. That par is easy. What I would also like to do, is to have a List<ChildrenOrganisation> in the parent class. This would also be easy, if the parrent and children organisation were in the same JSON file. But they are not. Is there a way to work around this, perhaps merging two different Streams?

Here is my how I get the HTTP Web Response:

public async Task GetHttpWebRespsoneAsJson(bool isSubOrganisation)
        {
            string href;
            string requestString;

            // Set the request string, depending on whether or not it is a sub-organisation
            if (isSubOrganisation)
            {
                href = "http://data.brreg.no/enhetsregisteret/underenhet.json?page=0&size=100&$filter=overordnetEnhet+eq+{0}";
                requestString = String.Format(href, organisationNumber);
            }
            else
            {
                href = "http://data.brreg.no/enhetsregisteret/enhet/{0}.json";
                requestString = String.Format(href, organisationNumber);
            }

            // Create the HTTP request, using the input parameters
            WebRequest httpRequest = WebRequest.Create(requestString);

            // Get the respone of the HTTP request
            httpResponse = await httpRequest.GetResponseAsync();

            // Get the statuscode (hopefully "OK")
            httpStatus = ((HttpWebResponse)httpResponse).StatusCode;
}

Here is my implementation of DataContractJsonSerializer:

public CompanyModel GetCompany()
{
    CompanyModel company = new CompanyModel();

    using (Stream httpDataStream = httpResponse.GetResponseStream())
    {
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(CompanyModel));
        company = (CompanyModel)ser.ReadObject(httpDataStream);
        httpDataStream.Close();
    }

    return company;
}

The parent organisation simplifed (see an actual example here):

{
 "organisationId": 123,
 "name": "Parent Organisation",
 "address": {
   "streetname": "Electric Avenue",
   "number": 42,
   "city": "Los Angeles"
  }
}

The list of children organisations simplified (see an actual example here):

"organisations": [
  {
    "organisationId": 456,
    "parentOrganisation": 123,
    "name": "Children Organisation 1",
    "address": {
      "streetname": "Sunset Boulevard",
      "number": 69,
      "city": "San Fransisco"
    }
  },
  {
    "organisationId": 789,
    "parentOrganisation": 123,
    "name": "Children Organisation 2",
    "address": {
      "streetname": "Ocean Drive",
      "number": 13,
      "city": "Miami"
    }
  }
]
dbc
  • 104,963
  • 20
  • 228
  • 340
Jakob Busk Sørensen
  • 5,599
  • 7
  • 44
  • 96
  • That *list of children organisations simplified* is invalid JSON. There should at least be outer curly braces `{` and `}`. But the actual JSON in the link provided looks like `{ "links": [ { ... }, { ... }, ... ], "data" : [ { ... }, { ... }, ... ] }` Can you update your sample JSON? – dbc Jul 13 '17 at 08:03
  • Thanks, I missed that. Let me know if you find anything else, which is missing :-) – Jakob Busk Sørensen Jul 13 '17 at 08:21

0 Answers0