We help IT Professionals succeed at work.

List not returning values

Hi,

I have create 2 classes. the problem is following List no records is showing
public class DirectoryResponse
    {
        public List<Company> Companyname { get; set; }
    }


    [XmlType(TypeName = "Company")]
    public class Company
    {
    public string _id { get; set; }
    public string  name { get; set; }
    public string friendly_id { get; set; }
    public DateTime creation_date { get; set; }
    public DateTime updated_date { get; set; }
    public int member_count { get; set; }
    public string initial { get; set; }

    
    





    }

Open in new window


Call form following class

RestAPI1 Class

public  List<Company> GetCompanyDetails(bool onlyCurrent = true)
        {


 var raw = response.Content.Replace("Date\":\"0000-00-00\"", "Date\":null").RemoveTroublesomeCharacters();
                var package = JsonConvert.DeserializeObject<DirectoryResponse>(raw);
                // return JsonConvert.DeserializeObject<T>(s);
                //raw.FromJson<DirectoryResponse>()



                if (package != null)
                {


                    var com = package.Companyname;

                    return com;
}

Open in new window


No errors . But no records returning. When it's debug raw variable get all the results.
But package.Companyname is not getting results


 Interface of  RestAPI1 Class
public interface IrestClient
    {
        List<Company> GetCompanyDetails(bool onlyCurrent = true);
    }

Open in new window

Comment
Watch Question

Rikin ShahMicrosoft Dynamics CRM Consultant
CERTIFIED EXPERT

Commented:
Hi,

When you write-

var com = package.Companyname;

are you sure you will get List<Company> ? I doubt that.

As per my understanding, you will first need to traverse through the list and possibly extract the Company Name information from it and store it to new object Company. And that object then should be added to the List<Company>. Check below code-

// Assuming package.Companyname will return a list of company names here
var com = package.Companyname;

Company c = null;
List<Company> CompanyList = new List<Company>();
foreach (var cname in com)
{
    c = new Company();
    c.name = cname;
    CompanyList.Add(c);
}

return CompanyList;

Open in new window

Eduard GherguArchitect - Coder - Mentor
CERTIFIED EXPERT

Commented:
HI!
public  List<Company> GetCompanyDetails(bool onlyCurrent = true)
        {


 var raw = response.Content.Replace("Date\":\"0000-00-00\"", "Date\":null").RemoveTroublesomeCharacters();
                var package = JsonConvert.DeserializeObject<DirectoryResponse>(raw);
                // return JsonConvert.DeserializeObject<T>(s);
                //raw.FromJson<DirectoryResponse>()

Who is response? Where is it defined?

Author

Commented:
@Eduard Ghergu

Thank you very much for your reply.much appriciated
Who is response? don't worry about response. until response data is receiving.
Where is it defined?
response means it's comming from
REST API.Rest API records is working perfectly.Only issue is retuning no records
Eduard GherguArchitect - Coder - Mentor
CERTIFIED EXPERT

Commented:
Hi,
My pleasure!
Do you have a sample JSON payload?

Author

Commented:
@Rikin Shah
earlier my code is worked fine, i forget what i changed.
yes, i can understand what you mean.really appiciated. But i want this way to work out.It's really
easy no need to with forach .Any idea much appriciated

Author

Commented:
@Eduard Ghergu
Thank you very much for your reply
see atatched screenshot and file
Capture1.PNG
Sample-JSON-Results.txt
Eduard GherguArchitect - Coder - Mentor
CERTIFIED EXPERT

Commented:
Hi!

The problem is that you want to deserialize to DirectoryResponse instead of a Company. Also, the attribute [XmlType(TypeName = "Company")] is useless.
Based on your payload, there will be just a single Company deserialized. If you want a list with companies, you'll need to have something like:
{
"company": [{
"_id": "18f62e940d1444",
"name": "Test",
"friendly_id": "Test1",
"creation_date": "2017-10-17T10:36:33.378Z",
"updated_date": "2019-08-29T08:05:30.421Z",
"member_count": 43,
"initial": "R"
}]
}

Author

Commented:
yes that's right. This code is working perfectly my previous programe.
I can't understand why it's not working here
According to this logic

public static T FromJson<T>(this string s)
        {
            return JsonConvert.DeserializeObject<T>(s);
        }

Refere:
https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_JsonConvert_DeserializeObject__1.htm



but it's work when i enter Company class instead of DirectResponse class

public class DirectoryResponse
    {
        public List<Company> Companyname { get; set; }

    }

I think i'm doing small mistake i can't understand what is that.
Eduard GherguArchitect - Coder - Mentor
CERTIFIED EXPERT

Commented:
So, to understand correctly, you want an extension method that is going to use Generics and that's not working? raw is correctly formatted after the Replace call?

Author

Commented:
So, to understand correctly, you want an extension method that is going to use
Generics and that's not working?

Yes,That's right

raw is correctly formatted after the Replace call?

yes.
Eduard GherguArchitect - Coder - Mentor
CERTIFIED EXPERT

Commented:
So, you want to get a DirectoryResponse from that payload? This is simply not possible with a generic method like the one you're trying to implement.

Author

Commented:
So, you want to get a DirectoryResponse from that payload? Yes
This is simply not possible with a generic method like the one you're trying to implement.

But this working my previous program.Unfortunaly i can't figure out what is missing.
Anyway do you have any example what you are suggestion
Architect - Coder - Mentor
CERTIFIED EXPERT
Commented:
Hi,

You'll need to change the payload according to the sample that I gave to you earlier. In that sample, you'll need to replace "company" with "companyname" (pretty bad name for a List property). Also, you can add a [JsonProperty("companyname")] attribute to your Companyname property.
Nageswara rao SuryadevaraSoftware consultant

Commented:
Hi ,

It appears you are returning 'Company' instead of 'DirectoryResponse' in the Rest api.   Can you please post your Rest api method code?