Failing to convert json with number property names to c# object - c#

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());
}

Related

c# json with a changing class

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; }
}

Sending Json Arrays within Object to API

i need to POST arrays within object to api that will looks like this:
{
"ds_seatInfo": [
{
"SEAT_LOC_NO": "00201901",
"SEAT_LOC_NO": "00201902"
}
],
"SCN_SCH_SEQ": "13178",
"REQ_FG_CD": "01",
"LOCK_APRV_KEY": "123123"
}
i was tried using Models that define as follow:
public class ds_seatInfo
{
public List<string> SEAT_LOC_NO { get; set; }
}
public class BookParam
{
public string SCN_SCH_SEQ { get; set; }
public ds_seatInfo ds_seatInfo { get; set; }
public string REQ_FG_CD { get; set; }
public string LOCK_APRV_KEY { get; set; }
}
but the result aren't as expected, that's model return:
"{\"SCN_SCH_SEQ\":\"13178\",\"ds_seatInfo\":{\"SEAT_LOC_NO\":[\"00201901\",\"00201902\"]},\"REQ_FG_CD\":\"01\",\"LOCK_APRV_KEY\":\"123123\"}"
which means the SEAT_LOC_NO doesn't read as expected. i am using Newtonsoft for Serialize the Model.
What should i do?
Haven't tested it, but may be this will help you or get you in the right direction:
public class BookParam
{
[JsonProperty("ds_seatInfo")]
public List<KeyValuePair<string, string>> SetInfos = new List<KeyValuePair<string, string>>();
[JsonProperty("SCN_SCH_SEQ")]
public string ScnSchSeq { get; set; }
[JsonProperty("REQ_FG_CD")]
public string ReqFgCd { get; set; }
[JsonProperty("LOCK_APRV_KEY")]
public string LockAprvKey { get; set; }
}
And when you add items to SetInfos try like this:
SetInfos.Add(new KeyValuePair<string, string>("SEAT_LOC_NO", "00201901"));
Edit
Another possible implementation
public class BookParam
{
[JsonProperty("ds_seatInfo")]
public List<SeatInfo> DsSeatInfo = new List<SeatInfo>();
[JsonProperty("SCN_SCH_SEQ")]
public string ScnSchSeq { get; set; }
[JsonProperty("REQ_FG_CD")]
public string ReqFgCd { get; set; }
[JsonProperty("LOCK_APRV_KEY")]
public string LockAprvKey { get; set; }
}
public class SeatInfo()
{
[JsonProperty("SEAT_LOC_NO")]
public string SeatLocNo { get; set; }
}

Json not mapping properly in C#

Here is a json document that I wanted to map it to C# poco classes. I wrote some classes but they didn't work. I got null in my result object. Any ideas?
I used Newtonsoft's json converter.
{
"retrieval-response":{
"cdata":{
"identifier":"777400",
"document-count":"62"
},
"index":"10",
"count":"25"
}
}
C# map classes;
public class result
{
[JsonProperty("retrieval-response")]
public aResult res { get; set; }
public int index { get; set; }
public int count { get; set; }
}
public class aResult
{
public cdata data { get; set; }
}
public class cdata
{
[JsonProperty("identifier")]
public string identif { get; set; }
[JsonProperty("document-count")]
public string count { get; set; }
}
You model is wrong. Try this:
public class Wrapper
{
[JsonProperty("retrieval-response")]
public Result Result { get; set; }
}
public class Result
{
[JsonProperty("cdata")]
public Data Data { get; set; }
public int Index { get; set; }
public int Count { get; set; }
}
public class Data
{
[JsonProperty("identifier")]
public string Identifier { get; set; }
[JsonProperty("document-count")]
public string Count { get; set; }
}
Then you can deserialize it with the following line:
var myResult = JsonConvert.DeserializeObject<Wrapper>(json);
Please note that I've also wrote your property and class names in pascal case. These are the naming conventions from Microsoft.

Deserialize Json Object with for each

var serverData = serverConnection.connect("login.php", pairs);
RootObject json = JsonConvert.DeserializeObject<RootObject>(await serverData);
foreach (Logined m in json.logined)
{
}
public class Logined
{
public string id { get; set; }
public string firsname { get; set; }
public string lastname { get; set; }
public string email { get; set; }
public string phone { get; set; }
public string profilePic { get; set; }
public string thumbnail { get; set; }
}
public class RootObject
{
public Logined logined { get; set; }
}
the error in the for each it says cannot operate on variables of type public definition for getenumerator
Your root object contains only a single Logined, so there is nothing to enumerate.
public class RootObject
{
public Logined logined { get; set; } //not a collection
}
foreach (Logined m in json.logined) //json.logined is a single object (not a collection)
{
}
If your server is returning a collection of Logined, you have to change your RootObject definition.
public class RootObject
{
public Logined[] logined { get; set; } //array of Logined
}

JSON.NET deserialize results in list with default values

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.

Categories