How to deserialize an array of JSON RPC response with JSON.NET in C#?

1

I have a JSON string response that looks like this:

[{"result":"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f","error":null,"id":"0"},
{"result":"00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048","error":null,"id":"1"},
{"result":"000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da3fdcc99ddbd","error":null,"id":"2"},
{"result":"0000000082b5015589a3fdf2d4baff403e6f0be035a5d9742c1cae6295464449","error":null,"id":"3"}]

I want to deserialize this into an array of strings of just the result attribute and return that.

I have created this class to hold each object in the array, but unsure how to deserialize it.

public class RPCResponse<T>
{
    public T result { get; set; }
    public string error { get; set; }
    public string id { get; set; }
}

I tried var hashes = JsonConvert.DeserializeObject<RPCResponse<string>>(response); but got error

Newtonsoft.Json.JsonSerializationException
  HResult=0x80131500
  Message=Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'RPCResponse`1[System.String]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
c#
json
asked on Stack Overflow Jan 3, 2019 by Mark Allison

2 Answers

3

You have an array of them. Try to deserialize with:

JsonConvert.DeserializeObject<RPCResponse<string>[]>(response);
answered on Stack Overflow Jan 3, 2019 by Xiaoy312
-3

I think you should remove the [ ] sign from your json. Your json should look like this:

{"result":"gg","error":null,"id":"0"},
{"result":"gg","error":null,"id":"1"}
answered on Stack Overflow Jan 3, 2019 by Marjan Kalanaki

User contributions licensed under CC BY-SA 3.0