Am I missing something obvious here? JSON:
{"p":[{},{"clientId":102102059663,"checkbox1Ticked":false,"checkbox2Ticked":false},{"clientId":23841,"checkbox1Ticked":false,"checkbox2Ticked":false},{"clientId":102102111426,"checkbox1Ticked":false,"checkbox2Ticked":false}]}
C#: (checkboxData is the string above)
public JsonResult SubmitSelectedChanges(string checkboxData)
{
var deserializedClients = JsonConvert.DeserializeObject<ChangeList>(checkboxData);
return null;
}
public class ChangeList
{
public List<Change> p { get; set; }
}
public class Change
{
string clientId { get; set; }
bool checkbox1Ticked { get; set; }
bool checkbox2Ticked { get; set; }
}
After deserializing the clientId is always null and the checbox1Ticked and checkbox2Ticked is false.
It was because I had forgotten the access modifiers for the change class:
public class Change
{
public string clientId { get; set; }
public bool checkbox1Ticked { get; set; }
public bool checkbox2Ticked { get; set; }
}
I would have thought this would have thrown an exception.
Related
I am using ASP.NET Core 3.1 and angular.
I send a post request from the object model to the controller method.
The controller method accepts an object at the input, but the bindingParameters field is empty.
The bindingParameters field is a list of KeyValueItem objects.
TmObject.cs
namespace v1.Atm
{
public class TmObject
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("binding")]
public string Binding { get; set; }
[JsonProperty("sourceBindingParameters")]
public string SourceBindingParameters { get; set; }
[NotMapped]
[JsonProperty("bindingParameters")]
public List<TmKeyValueItem> BindingParameters
{
get
{
return JsonConvert.DeserializeObject<List<TmKeyValueItem>>(string.IsNullOrEmpty(SourceBindingParameters) ? "" : SourceBindingParameters);
}
set
{
SourceBindingParameters = JsonConvert.SerializeObject(value);
}
}
[JsonProperty("caption")]
public string Caption { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonIgnore]
public string SourceParameterGroups { get; set; }
[NotMapped]
[JsonProperty("parameterGroups")]
public List<string> ParameterGroups
{
get
{
return JsonConvert.DeserializeObject<List<string>>(string.IsNullOrEmpty(SourceParameterGroups) ? "" : SourceParameterGroups);
}
set
{
SourceParameterGroups = JsonConvert.SerializeObject(value);
}
}
[NotMapped]
[JsonProperty("parameters")]
public List<TmObjectParameter> Parameters;
[NotMapped]
[JsonProperty("removeParameters")]
public List<int> RemoveParameters { get; set; }
public TmObject()
{
Parameters = new List<TmObjectParameter>();
RemoveParameters = new List<int>();
}
}
}
If the bindingParameters field is changed, it works:
[NotMapped]
[JsonProperty("bindingParameters")]
public List<BindingParameter> BindingParameters{ get; set; }
Tell me, please, what could be the problem?
P.S. Prior to this, the project was implemented on ASP.NET Webforms and there the code described above worked.
update
I got out of the problem as follows. Opened the SourceBindingParameters field for visibility on the client by adding [JsonProperty ("sourceBindingParameters")]. And before sending data to the server, I serialize the values from BindingParameters to sourceBindingParameters.
public updateTmObject(tmObject: TmObject) {
tmObject.sourceBindingParameters = JSON.stringify(tmObject.bindingParameters);
return this.httpService.post('/v1/Editor/UpdateObject', JSON.stringify(tmObject.bindingParameters), this.httpOptions).subscribe(
(response: any) => {
this.reset();
this.getTmObjects();
return true;
},
error => {
console.error("TmObjects|TmObjectsService.updateTmObject(): " + error.status);
}
);
}
The bindingParameters from request is not a list of string values but complex objects. You'd better create a class for this objects
public class BindingParameter
{
public int Id { get; set; }
public string Key { get; set; }
public string Value { get; set; }
public bool Visible { get; set; }
}
and declare the property as the following
public List<BindingParameter> BindingParameters{ get; set; }
This is my json :
"{\"sctoken\":\"a50395d5-571983f3-b394-4c6f-a26a-f95ae125fad6\",\"tms\":[{\"name\":\"ELTE MapCenterCache\",\"url\":\"http://cache2.smok.net.pl/cache_2015/element?s=%zoom%&x=%x%&y=%y%\"},{\"name\":\"OSM Mapnik\",\"url\":\"http://tile.openstreetmap.org/%zoom%/%x%/%y%.png\"},{\"name\":\"OSMElteMapa\",\"url\":\"http://osm.smok.net.pl/osm_tiles/%zoom%/%x%/%y%.png\"}],\"user_descr\":\"Pawel Okarmus\"}"
this how I convert json to class :
LoginResult loginResult = JsonConvert.DeserializeObject<LoginResult>(result.ToString());
class LoginResult
{
string sctoken { set; get; }
List<Maps> tms { set; get; }
}
class Maps
{
public string name { get; set; }
public string url { get; set; }
}
and my object LoginResult is empty
Try This
LoginResult loginResult = JsonConvert.DeserializeObject<RootObject>(result.ToString());
public class Tm
{
public string name { get; set; }
public string url { get; set; }
}
public class RootObject
{
public string sctoken { get; set; }
public List<Tm> tms { get; set; }
public string user_descr { get; set; }
}
It should work if you make the properties and the classes all public, so the deserializer can find & access them:
public class LoginResult
{
public string sctoken { set; get; }
public List<Maps> tms { set; get; }
}
public class Maps
{
public string name { get; set; }
public string url { get; set; }
}
You are also missing the element user_descr but that is not essential (you can still add it, or omit it if you don't need it).
You are missing Public in your LoginResult class , just put Public access modifier and it would start working (like this):
class LoginResult
{
public string sctoken { set; get; }
public List<Maps> tms { set; get; }
}
I have a json collection that uses numbers as property names. I am using Newtonsoft JsonConvert to try and deserialize to a c# object but it fails. Is this even possible?
{
"0":{"_bnd":{"_path":"Style","_parts":["Style"],"_key":"Style"}},
"1":{"_bnd":{"_path":"AcctPerfAsOfDate","_parts":"AcctPerfAsOfDate"],"_key":"AcctPerfAsOfDate"}},"length":2,"_updating":0,"collectionChanged":{"_handlers":[{}]}
}
{
"0":{"_bnd":{"_path":"Style","_parts":["Style"],"_key":"Style"}},
"1":{"_bnd":{"_path":"AcctPerfAsOfDate","_parts":"AcctPerfAsOfDate"],"_key":"AcctPerfAsOfDate"}},"length":2,"_updating":0,"collectionChanged":{"_handlers":[{}]}
Remove this ^
}
Remove the ] to get a valid JSON.
UPDATE:
This is the class structure for your JSON:
public class Bnd
{
public string _path { get; set; }
public List<string> _parts { get; set; }
public string _key { get; set; }
}
public class __invalid_type__0
{
public Bnd _bnd { get; set; }
}
public class Bnd2
{
public string _path { get; set; }
public string _parts { get; set; }
public string _key { get; set; }
}
public class __invalid_type__1
{
public Bnd2 _bnd { get; set; }
}
public class Handler
{
}
public class CollectionChanged
{
public List<Handler> _handlers { get; set; }
}
public class RootObject
{
public __invalid_type__0 __invalid_name__0 { get; set; }
public __invalid_type__1 __invalid_name__1 { get; set; }
public int length { get; set; }
public int _updating { get; set; }
public CollectionChanged collectionChanged { get; set; }
}
The second item in the collection is ambiguous. Not sure if length, _updating and collectionChanged fields belong to second item or to the collection.
I removed it to create the example, but you can easily change the anonymous object to reflect your needs.
void Main()
{
JsonConvert.DeserializeAnonymousType(U.InputText(), new Dictionary<int, object> { })
.ToDictionary(a=>a.Key,a=> ((JObject) a.Value).ToAnonymous(new { _bnd = new {_path=""}})).Dump();
}
public static class ext
{
public static T ToAnonymous<T>(this JObject source, T obj) => (T)source.ToObject(obj.GetType());
}
I am New to JSON and API
I am trying to get some data from an API for which I have Json and want to make a class for it.when I convert it using josn2csharp I get following code. Please let me know what sholud be the proper format of my class so that it converts into json.
public class DataSet {
public string __invalid_name__-i:nil { get; set; } }
public class GenericObject {
public string __invalid_name__-xmlns:d2p1 { get; set; }
public string __invalid_name__-i:type { get; set; }
public string __invalid_name__#text { get; set; } }
public class Message {
public string __invalid_name__-i:nil { get; set; } }
public class FunctionResponse {
public string __invalid_name__-xmlns:i { get; set; }
public string __invalid_name__-xmlns { get; set; }
public DataSet DataSet { get; set; }
public GenericObject GenericObject { get; set; }
public Message Message { get; set; }
public string Success { get; set; } }
public class RootObject {
public FunctionResponse FunctionResponse { get; set; } }
use this question as reference in this I came to know how to write for
public string invalid_name#text { get; set; }
but what about others.
please help
edit 1:
{
"DataSet": {
"#nil": "true"
},
"GenericObject": {
"#type": "d2p1:boolean",
"#text": "true"
},
"Message": {
"#nil": "true"
},
"Success": "true"
}
Example:
{
"json":
{
"#thekeyinsidejsonproperty": "value"
}
}
you shoud have
public class RootObject
{
[JsonProperty("json")]
public Json jsonobj;
}
public class Json
{
[JsonProperty("#thekeyinsidejsonproperty")]
public string somepropertyname{ get; set; }
}
hope it helps! :)
I have a class:
public class LotInfo
{
public string lotn { get; set; }
public string imlitm { get; set; }
public string imdsc { get; set; }
public string wplotn { get; set; }
public int wptrdj { get; set; }
public DateTime wptrdj_d { get; set; }
public string wplitm { get; set; }
public int wptrqt { get; set; }
public string wpkyfn { get; set; }
public int wpdoco { get; set; }
public string iolitm { get; set; }
public string iodcto { get; set; }
public int iodoco { get; set; }
public int ioub04 { get; set; }
}
I have 2 instances.
Object1 and Object2
I want to inject object2 -> object1 for specific properties.
So i have overridden the Match method like this:
public class LotInfoInject : ConventionInjection
{
protected override bool Match(ConventionInfo c)
{
return c.SourceProp.Name.StartsWith("io");
}
}
and i am using the injecter like this:
object1.InjectFrom(object2);
I cant figure out why i am getting the exception.
{"Object of type 'System.String' cannot be converted to type
'System.Int32'."}
If i DONT override the Match method it works but i am getting properties that i dont want replaced from object1
any ideas?
You're trying to put iolitm (string) in iodoco (int).
Try like this:
public class LotInfoInject : ConventionInjection
{
protected override bool Match(ConventionInfo c)
{
return c.SourceProp.Name.StartsWith("io")
&& c.SourceProp.Name == c.TargetProp.Name;
}
}