I send data to server with this parameter
But get empty value in parameter of function
If i set data type dynamic for input function then all things is ok and i can get values.
Report Class
public class ColumnHistory
{
int ECH_COUNTER { get; set; }
int TOTAL_WEIGHT { get; set; }
int TOTAL_COUNT { get; set; }
string COLUMNS_PLACE { get; set; }
}
public class InventoryInPlace
{
int ELC_SIZE { get; set; }
int STO_COUNT { get; set; }
int STO_WGT { get; set; }
int STO_COUNTER { get; set; }
string ELC_ID { get; set; }
}
public class Report
{
public ColumnHistory[] columnHistory { get; set; }
public InventoryInPlace[] inventoryInPlace { get; set; }
}
If you do not declare the access modifier,the access level for class members is private by default.Be sure add access modifier for properties like below:
public class ColumnHistory
{
public int ECH_COUNTER { get; set; }
public int TOTAL_WEIGHT { get; set; }
public int TOTAL_COUNT { get; set; }
public string COLUMNS_PLACE { get; set; }
}
public class InventoryInPlace
{
public int ELC_SIZE { get; set; }
public int STO_COUNT { get; set; }
public int STO_WGT { get; set; }
public int STO_COUNTER { get; set; }
public string ELC_ID { get; set; }
}
public class 2500113075262000 {
public string pair { get; set; }
public string type { get; set; }
public double amount { get; set; }
public int rate { get; set; }
public string timestamp_created { get; set; }
public int status { get; set; }
}
public class Return {
public 2500113075262000 2500113075262000 { get; set; }
}
public class Root {
public int success { get; set; }
public Return #return { get; set; }
}
class 2500113075262000 is constantly changing, this is the order ID, like deserialize
{"success":1,"return":{"2500113075262000":{"pair":"eth_rur","type":"sell","amount":0.00110569,"rate":46100,"timestamp_created":"1608918997","status":0}}}
It looks like it's only the key - presumably the order ID - which is changing. I would suggest removing your Return class entirely, and changing your Root class to have a Dictionary<string, Order>. I'd also suggest writing your classes with idiomatic .NET property names, and using JsonPropertyAttribute to specify the representation in the JSON. So it would be something like this:
public class Order
{
[JsonProperty("pair")]
public string Pair { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
// etc, other properties
}
public class Root
{
[JsonProperty("success")]
public int Success { get; set; }
[JsonProperty("return")]
public Dictionary<string, Order> Returns { get; set; }
}
I have a List<dynamic> and I need to create clone of that. I've tried thousand way to solve it, you are the last hope.
Code looks like this:
public class Basic
{
public int Start { get; set; }
public int Leng { get; set; }
public string Name { get; set; }
}
public class MyObj
{
public Basic Basic { get; set; }
public string Value { get; set; }
}
public class MyObj1
{
public Basic Basic { get; set; }
public int Value { get; set; }
}
public class MyObj2
{
public Basic Basic { get; set; }
public string Value { get; set; }
}
public class MyObj3
{
public Basic Basic { get; set; }
public decimal Value { get; set; }
}
All this (MyObj, MyObj1, MyObj2, MyObj3) objects are in my List<dynamic> list
I need to create deep clone of List<dynamic> list.
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.
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
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();
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; }
}
I am trying to deserialize an object, the type is populated but I am getting null for the List<Sport>. Any ideas?
My classes:
class Sports
{
public MsgTypes type { get; set; }
public List<Sport> Sport { get; set; }
}
class Sport
{
public int Id { get; set; }
public int Import_id { get; set; }
public string Name { get; set; }
public int Active { get; set; }
public int Order { get; set; }
public int Min_bet { get; set; }
public int Max_bet { get; set; }
public int Updated { get; set; }
public string Feed_type { get; set; }
public string Locale { get; set; }
}
The command for deserialization:
Sports _sports = (Sports) JsonConvert.DeserializeObject<Sports>(jsonObj);
This is my JSON object:
"{\"code\":0,\"type\":4,\"Sports\":[{\"Sport\":{\"id\":\"1\",\"import_id\":\"1\",\"name\":\"Soccer\",\"active\":true,\"order\":\"1\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194889\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"2\",\"import_id\":\"5\",\"name\":\"Tennis\",\"active\":true,\"order\":\"3\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194771\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"3\",\"import_id\":\"6\",\"name\":\"Handball\",\"active\":true,\"order\":\"6\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403152901\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"4\",\"import_id\":\"4\",\"name\":\"Ice Hockey\",\"active\":true,\"order\":\"4\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403080245\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"7\",\"import_id\":\"2\",\"name\":\"Basketball\",\"active\":true,\"order\":\"2\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194830\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"8\",\"import_id\":\"23\",\"name\":\"Volleyball\",\"active\":true,\"order\":\"5\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194591\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"9\",\"import_id\":\"12\",\"name\":\"Rugby\",\"active\":true,\"order\":\"7\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194710\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"12\",\"import_id\":\"11\",\"name\":\"Motorsport\",\"active\":true,\"order\":\"12\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403065699\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"13\",\"import_id\":\"3\",\"name\":\"Baseball\",\"active\":true,\"order\":\"13\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194834\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"14\",\"import_id\":\"16\",\"name\":\"American Football\",\"active\":true,\"order\":\"14\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403143326\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}},{\"Sport\":{\"id\":\"16\",\"import_id\":\"34\",\"name\":\"Beach Volley\",\"active\":true,\"order\":\"16\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194417\",\"feed_type\":\"Betradar\",\"locale\":\"en_us\"}}]}"
You need one more level of nesting and different class names. You should be able to deserialize to such structure:
class SportsParent
{
//Code for MsgTypes was not provided so it is commented out
public List<SportGroup> Sports { get; set; }
}
class SportGroup
{
public SportItem Sport { get; set; }
}
class SportItem
{
public int Id { get; set; }
public int Import_id { get; set; }
public string Name { get; set; }
public bool Active { get; set; } //need to be converted to bool instead of int
public int Order { get; set; }
public int Min_bet { get; set; }
public int Max_bet { get; set; }
public int Updated { get; set; }
public string Feed_type { get; set; }
public string Locale { get; set; }
}
You can deserialize using such code:
SportsParent _sports = JsonConvert.DeserializeObject<SportsParent>(jsonObj);