0

I'm having troubles parsing a json to an object using json.net deserializer. My objects:

        public class Employee
        {
            public int id { get; set; }
            public string name { get; set; }
            public string work_email { get; set; }
            public Partner address_id { get; set; }
        }

        public class Partner
        {
            public int id { get; set; } //in the example "5021"
            public string name { get; set; } //in the example "company x"
        }

The JSON:

{
  "address_id": [
    5021,
    "Company X"
  ],
  "work_email": false,
  "id": 37,
  "name": "John Doe"
}

The error, which I understand: the current JSON array (e.g. [1,2,3]) cannot convert to 'Odoo.OdooConnect+Partner' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

Changing the json to "address_id": {5021,"Company X"} would of course fix the error but not possible.

I cannot imagine there is no better option than to use lists and manually converting all multi level keys to properties like this:

if (records.Property("address_id") != null) { Employee.Partner.id = (int)records.SelectToken( "address_id[0]" ); Employee.Partner.name = (int)records.SelectToken( "address_id[1]" ); }

Some help?

guido6
  • 1
  • Well, I don't understand why you're so surprised: JSON syntax is clear, [ ] means you have an array and you can't convert it to a simple class. If you want to parse your JSON automatically you have to make the class compliant to the JSON or vice-versa. I can't see any other way than to manually parse and do some magic behind (like you did) if JSON and classes don't match. – Marco Mar 07 '20 at 09:35
  • Well, I'm not surprised, I just need a solution without having to manually cast all keys from the json with an id in it to a property field. Maybe by customizing the JSON deserializer somehow or looping through the original json and changing the square brackets to curly brackets if the key has "_id" in it. – guido6 Mar 07 '20 at 10:01
  • You could apply `ObjectToArrayConverter` from [this answer](https://stackoverflow.com/a/39462464/3744182) to [How to deserialize objects containing arrays of values with a fixed schema to strongly typed data classes?](https://stackoverflow.com/q/39461518/3744182). You would also need to add `[JsonProperty(Order = N)]` attributes to the members of `Partner`. In fact I think this is a duplicate, agree? – dbc Mar 07 '20 at 16:18
  • 1
    @dbc, thanks, this helped me. It is a identical problem. Only mine is even more simple because the array only consists of 2 items max. – guido6 Mar 07 '20 at 18:37

1 Answers1

0

Solved by adding this helper to parse the json. If the count of the "array" is <2, in my case false, I simply do not add the token to the jobject.

            JObject newJSON = new JObject();
            foreach (KeyValuePair<string, JToken> token in JSON)
            {
                string _keyname = token.Key;
                JToken _value = token.Value;
                if (!token.Key.Contains("_id"))
                {
                    newJSON.Add(_keyname, _value);
                }
                else if (token.Value.Count() < 2)
                {
                    //do nothing
                }
                else
                {
                    newJSON.Add(_keyname, _value.First);
                    newJSON.Add(_keyname + "_name", _value.Last);

                }
            }
            return newJSON;}
guido6
  • 1