-1

So I created a class using json2csharp

    public class ResponseType
    {
        public class Query
        {
            public string q { get; set; }
            public object sku { get; set; }
            public int limit { get; set; }
            public object reference { get; set; }
            public object mpn_or_sku { get; set; }
            public string mpn { get; set; }
            public object brand { get; set; }
            public string __class__ { get; set; }
            public int start { get; set; }
            public object seller { get; set; }
        }

        public class Request
        {
            public bool exact_only { get; set; }
            public string __class__ { get; set; }
            public List<Query> queries { get; set; }
        }

        public class Seller
        {
            public string display_flag { get; set; }
            public bool has_ecommerce { get; set; }
            public string name { get; set; }
            public string __class__ { get; set; }
            public string homepage_url { get; set; }
            public string id { get; set; }
            public string uid { get; set; }
        }

        public class Prices
        {
            public List<List<object>> USD { get; set; }
            public List<List<object>> JPY { get; set; }
            public List<List<object>> CNY { get; set; }
        }

        public class Offer
        {
            public string sku { get; set; }
            public string packaging { get; set; }
            public string on_order_eta { get; set; }
            public string last_updated { get; set; }
            public int? order_multiple { get; set; }
            public int in_stock_quantity { get; set; }
            public string eligible_region { get; set; }
            public int? moq { get; set; }
            public int? on_order_quantity { get; set; }
            public object octopart_rfq_url { get; set; }
            public string __class__ { get; set; }
            public Seller seller { get; set; }
            public string product_url { get; set; }
            public object factory_order_multiple { get; set; }
            public string _naive_id { get; set; }
            public int? factory_lead_days { get; set; }
            public Prices prices { get; set; }
            public bool is_authorized { get; set; }
            public bool is_realtime { get; set; }
        }

        public class Brand
        {
            public string homepage_url { get; set; }
            public string __class__ { get; set; }
            public string name { get; set; }
            public string uid { get; set; }
        }

        public class Manufacturer
        {
            public string homepage_url { get; set; }
            public string __class__ { get; set; }
            public string name { get; set; }
            public string uid { get; set; }
        }

        public class Item
        {
            public List<Offer> offers { get; set; }
            public string uid { get; set; }
            public string mpn { get; set; }
            public List<object> redirected_uids { get; set; }
            public Brand brand { get; set; }
            public string octopart_url { get; set; }
            public string __class__ { get; set; }
            public Manufacturer manufacturer { get; set; }
        }

        public class Result
        {
            public List<Item> items { get; set; }
            public int hits { get; set; }
            public string __class__ { get; set; }
            public object reference { get; set; }
            public object error { get; set; }
        }

        public class RootObject
        {
            public int msec { get; set; }
            public Request request { get; set; }
            public string __class__ { get; set; }
            public List<Result> results { get; set; }
        }
    }

The problem is at design-time, when I declare a variable with the type of my class:

ResponseType Response = new ResponseType();

Intellisense does not allow me to access the subclasses RootObject.results list. It only shows Equals, GetHashCode, GetType and ToString. I am assuming I did something wrong in my class declaration.

Thank you in advance!

Edit -- I am fairly new to C Sharp. I am trying to parse a response from a REST API. I took the JSON provided by the Rest API and converted it using json2csharp into a class. My intent was to do something like this

Within a function return:

    public ResponseType ExecuteSearch(String PartNumber)
    {

~ ALL CODE FOR GENERATING req

        // Perform the search and obtain results
        var data = client.Execute(req).Content;
        JSON = data;

    return JsonConvert.DeserializeObject<ResponseType>(data);
    }

Then being able to access the response as an object outside of the function

Edit 2:

I figured out what I did. Instead of nesting everything within the ResponseType I should have simply renamed RootObject to ResponseType.

Cade
  • 123
  • 1
  • 10
  • You havent defined any fields only more classes - what exactly are you trying to access under response? – BugFinder Feb 15 '17 at 14:20
  • That it puts all the classes you need inside of another class is really horrible. I'd suggest changing that outer class to a namespace. – juharr Feb 15 '17 at 14:23
  • what are you trying to accomplish? why did you decide for this structur? and where do you want to access the `results` variable? – Mong Zhu Feb 15 '17 at 14:24
  • I edited the question for more information regarding my intent. – Cade Feb 15 '17 at 14:48
  • If you are using Visual Studio, a better option than json2csharp would be a built in function Edit -> Paste Special -> Paste JSON As Classes. That should give you code similar to my answer below. – TJ Rockefeller Feb 15 '17 at 15:05

3 Answers3

6

Intellisense does not allow me to access the subclasses RootObject.results list

it is because the property results is not static and you try to acces it this way. A static property is accessed via ClassName.PropertyName. For more information on static variables check the link.

It only shows Equals, GetHashCode, GetType and ToString

This is the basic set of methods that every object in C# inherits from the class object. This is why you can see it.

Intellisense will allow you to do this:

ResponseType.RootObject ro = new ResponseType.RootObject();
ro.results.First();

because you will need an Instance of that class to acces the property results.

I am assuming I did something wrong in my class declaration.

It depends. Basically if the compiler does not complain then you declared your classes as supposed to be. But the declaration of the properties commands you to access them in a specific way. So if you still want to access results with RootObject.results you need to make it static:

public class RootObject
{
    public static List<Result> results { get; set; }
}

But note that this list will exist only once! and is not individual to each instance of RootObject! Since you have embedded classes you need to call it like this:

ResponseType.RootObject.results.WhatEver();

EDIT

I guess you would like to get the Object of type RootObject inside the Object of type ResponseType. If I am right then it is not necessary to declare the classes inside ResponseType but you have to declare variables of each type inside it like:

public class ResponseType
{
    public RootObject MyRootObject{ get; set; }
}

public class RootObject
{
    public int msec { get; set; }
    public Request request { get; set; }
    public string __class__ { get; set; }
    public List<Result> results { get; set; }
}

Now you will be able to access the results variable inside the ResponseType object:

ResponseType rt = new ResponseType();
rt.MyRootObject.results.WhatEver();

For more information on how to deserialize JSON to classes please read the Deserialize JSON to C# Classes post

Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • Thank you for your response. I edited my question with more information regarding my intent – Cade Feb 15 '17 at 14:49
  • @Cade I made an edit according to the updated information – Mong Zhu Feb 15 '17 at 14:56
  • @Cade for proper deserialization please check out the link to a very informative post in my edit – Mong Zhu Feb 15 '17 at 15:00
  • Ill try that thanks. I have application working fine but I used VAR instead of defining the class. I would prefer to define the class though. I think your post will help me with that. Thank you – Cade Feb 15 '17 at 15:12
  • I figured out what I did. Instead of nesting everything within the ResponseType I should have simply renamed RootObject to ResponseType. – Cade Feb 15 '17 at 15:23
  • @Cade Glad I could be of help and that you figured out how to solve your problem. Sometimes it is easier than one thinks. But I would suggest to dig more into deserialization and structuring the classes appropriately. Have fun with that :) – Mong Zhu Feb 15 '17 at 15:25
0

1) Object with ResponseType class isn't contain any fields(event static one). 2) You declare ResponseType object, but results is field of RootObject object.

So if you want to work with results you should do something like this:

ResponseType.RootObject rootObject = new ResponseType.RootObject();
rootObject.results.DoWork();
0

Below is what I think you are trying to do. I would only use it in this form if this is some kind of Data Transfer Object (DTO) because otherwise it is pretty bad practice for a class that would be used in code (mostly because of the public getters and setters on all of the fields and the field names matching the class name), but it does show your main mistake and that is that classes need to be defined outside of your main class and if you need that type of class in your top level class you need to define a public field to access it.

public class ResponseType
{
    public Query Query { get; set; }
    public Request Request { get; set; }
    public Seller Seller { get; set; }
    public Prices Prices { get; set; }
    public Offer Offer { get; set; }
    public Brand Brand { get; set; }
    public Manufacturer Manufacturer { get; set; }
    public Item Item { get; set; }
    public Result Result { get; set; }
    public RootObject RootObject { get; set; }
}

public class Query
{
    public string q { get; set; }
    public object sku { get; set; }
    public int limit { get; set; }
    public object reference { get; set; }
    public object mpn_or_sku { get; set; }
    public string mpn { get; set; }
    public object brand { get; set; }
    public string __class__ { get; set; }
    public int start { get; set; }
    public object seller { get; set; }
}
public class Request
{
    public bool exact_only { get; set; }
    public string __class__ { get; set; }
    public List<Query> queries { get; set; }
}
public class Seller
{
    public string display_flag { get; set; }
    public bool has_ecommerce { get; set; }
    public string name { get; set; }
    public string __class__ { get; set; }
    public string homepage_url { get; set; }
    public string id { get; set; }
    public string uid { get; set; }
}
public class Prices
{
    public List<List<object>> USD { get; set; }
    public List<List<object>> JPY { get; set; }
    public List<List<object>> CNY { get; set; }
}
public class Offer
{
    public string sku { get; set; }
    public string packaging { get; set; }
    public string on_order_eta { get; set; }
    public string last_updated { get; set; }
    public int? order_multiple { get; set; }
    public int in_stock_quantity { get; set; }
    public string eligible_region { get; set; }
    public int? moq { get; set; }
    public int? on_order_quantity { get; set; }
    public object octopart_rfq_url { get; set; }
    public string __class__ { get; set; }
    public Seller seller { get; set; }
    public string product_url { get; set; }
    public object factory_order_multiple { get; set; }
    public string _naive_id { get; set; }
    public int? factory_lead_days { get; set; }
    public Prices prices { get; set; }
    public bool is_authorized { get; set; }
    public bool is_realtime { get; set; }
}
public class Brand
{
    public string homepage_url { get; set; }
    public string __class__ { get; set; }
    public string name { get; set; }
    public string uid { get; set; }
}
public class Manufacturer
{
    public string homepage_url { get; set; }
    public string __class__ { get; set; }
    public string name { get; set; }
    public string uid { get; set; }
}
public class Item
{
    public List<Offer> offers { get; set; }
    public string uid { get; set; }
    public string mpn { get; set; }
    public List<object> redirected_uids { get; set; }
    public Brand brand { get; set; }
    public string octopart_url { get; set; }
    public string __class__ { get; set; }
    public Manufacturer manufacturer { get; set; }
}
public class Result
{
    public List<Item> items { get; set; }
    public int hits { get; set; }
    public string __class__ { get; set; }
    public object reference { get; set; }
    public object error { get; set; }
}
public class RootObject
{
    public int msec { get; set; }
    public Request request { get; set; }
    public string __class__ { get; set; }
    public List<Result> results { get; set; }
}
TJ Rockefeller
  • 3,178
  • 17
  • 43