0

I'm having some trouble parsing a nested JSON response from an API.

The API returns an array, with an array of objects. The problem I'm facing is the int value contained in each array before the objects in the array (see attached example JSON snippet).

I've currently set up the deserialized types and objects, which is seemingly fine, however, the problem occurs when restsharp fails to deserialize the INT values to my deserialized type.

The Json

    [
         [
            1064,
            {
                "trx_id": "",
                "block": 0,
                "trx_in_block": 0,
                "op_in_trx": 0,
                "virtual_op": 0,
                "timestamp": "",
                "op": [
                    "vote",
                    {
                        "voter": "user1",
                        "author": "user2",
                        "permlink": "UUID",
                        "weight": 0
                    }
                ]
            }
        ]
    ]

The request

var response = await restClient.Execute<List<List<Models.Responses.AccountHistory.Transaction>>>(request);

Transaction.CS

    public class Transaction
        {
            public string trx_id { get; set; }
            public long block { get; set; }
            public long trx_in_block { get; set; }
            public long op_in_trx { get; set; }
            public long virtual_op { get; set; }
            //Etc..
    }

I'm trying to get a nested array of transactions deserialized, the int value is not something I have any practical use for. I'm looking for a way to ignore the integer and only parse the objects.

Error

Unhandled Exception: Newtonsoft.Json.JsonSerializationException: Error converting value 0 to type 'Models.Responses.AccountHistory.Transaction'. Path '[0][0]', line 1, position 3. occurred

1

2 Answers 2

1

You can write your own custom deserializer for the RestSharp client.

This will then need to be set on the client when it is instantiated.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the pointer towards custom deserializers. Posted the solution here now.
0

Thank you for the pointer towards custom deserializers, that was helpful. Snippet of solution below:

[JsonConverter(typeof(HistoryDeserializer))]
    public class AccountHistory
    {
        public long Integer { get; set; }
        public Transaction transaction { get; set; }
    }

Custom JsonConverter

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            switch (reader.TokenType)
            {
                case JsonToken.StartObject:
                    var objectValue = serializer.Deserialize<Transaction>(reader);
                    return new AccountHistory { transaction = objectValue };
                case JsonToken.Integer:
                    var integerValue = serializer.Deserialize<long>(reader);
                    return new AccountHistory { Integer = integerValue };
            }
            return null;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.