0

I have a json response that looks like this:

"corps":
[
{
"id": "1007",
"company_id": "1007",
"org_name": "My organization 1",
"org_addr1": "123 W. 1234 S.",
"org_addr2": "",
},
{
"id": "1008",
"org_name": "My organization 2",
"org_addr1": "123 W. 1234 S.",
"org_addr2": "",
}
]

I have successfully gotten a single response into my HCO object properly using:

DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(HCO));
HCO Company = (HCO)serializer.ReadObject(response.Content.ReadAsStreamAsync().Result);

This works well, but I'm trying to get all elements under corps. So I thought of trying something like this:

DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(HCO));
HCO element = (HCO)serializer.ReadObject(response.Content.ReadAsStreamAsync().Result);
Companies.Add(element);

But this simply doesn't work. How do I parse the json result and then serialize each element in the response?

HCO Class:

public class HCO
    {
        public int id { get; set; }
        public int comapny_id { get; set; }
        public string org_name { get; set; }
        public string org_addr1 { get; set; }
        public string org_addr2 { get; set; }
    }
5
  • CAn you show what HCO looks like Commented Aug 20, 2014 at 21:36
  • Companies.AddRange(Company.corps) Commented Aug 20, 2014 at 21:36
  • @L.B I don't have corps type under my HCO. Updated question to include sample HCO Commented Aug 20, 2014 at 21:42
  • Kyle, then how would your first code work? (As you say "This works well") Your Json is not of type HCO. It is an object where its coprs property is a List of HCO. Commented Aug 20, 2014 at 21:47
  • The first code was working when their was no array (i.e. only one element returned by the json server) Commented Aug 20, 2014 at 21:55

1 Answer 1

2

You can wrap up the HCO class in a Response like this:

public class HCOResponse
{
    public List<HCO> corps {get; set;}
}

And then try to deserialize the json using DataContractJsonSerializer like this:

DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(HCOResponse));
Companies = (HCOResponse)serializer.ReadObject(response.Content.ReadAsStreamAsync().Result);

Hope it helps.

EDIT: (from feedback)

HCOResponse hco_resp = (HCOResponse)serializer.ReadObject(response.Content.ReadAsStreamAsync().Result);
Companies = hco_resp.corps;
Sign up to request clarification or add additional context in comments.

4 Comments

Ok I gave this a shot but it said it could not convert HCOResponse to a generic list.
Let me understand the situation, you wrote this: Companies.Add(element); since element is a HCO instance, could you let me know what type is the object Companies? There might be a cast error instead of a serialize error in my answer.
Let's see it appears as though I had to change Companies to type HCOResponse instead of List<HCO> and it worked. Thank you very much!
I updated my answer to asign companies HCOResponse.corps, glad I could help.

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.