How to deserialize an array of JSON RPC response with JSON.NET in C#?
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
add a comment |
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
1
Have you triedJsonConvert.DeserializeObject<RPCResponse<string>>(response)?
– Pawel
Jan 3 at 15:51
add a comment |
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
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
c# json
asked Jan 3 at 15:42
Mark AllisonMark Allison
2,5592073122
2,5592073122
1
Have you triedJsonConvert.DeserializeObject<RPCResponse<string>>(response)?
– Pawel
Jan 3 at 15:51
add a comment |
1
Have you triedJsonConvert.DeserializeObject<RPCResponse<string>>(response)?
– Pawel
Jan 3 at 15:51
1
1
Have you tried
JsonConvert.DeserializeObject<RPCResponse<string>>(response)?– Pawel
Jan 3 at 15:51
Have you tried
JsonConvert.DeserializeObject<RPCResponse<string>>(response)?– Pawel
Jan 3 at 15:51
add a comment |
2 Answers
2
active
oldest
votes
You have an array of them. Try to deserialize with:
JsonConvert.DeserializeObject<RPCResponse<string>>(response);
add a comment |
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"}
1
This is not even a valid json,
– Xiaoy312
Jan 3 at 16:54
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025489%2fhow-to-deserialize-an-array-of-json-rpc-response-with-json-net-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have an array of them. Try to deserialize with:
JsonConvert.DeserializeObject<RPCResponse<string>>(response);
add a comment |
You have an array of them. Try to deserialize with:
JsonConvert.DeserializeObject<RPCResponse<string>>(response);
add a comment |
You have an array of them. Try to deserialize with:
JsonConvert.DeserializeObject<RPCResponse<string>>(response);
You have an array of them. Try to deserialize with:
JsonConvert.DeserializeObject<RPCResponse<string>>(response);
answered Jan 3 at 15:51
Xiaoy312Xiaoy312
11.5k12234
11.5k12234
add a comment |
add a comment |
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"}
1
This is not even a valid json,
– Xiaoy312
Jan 3 at 16:54
add a comment |
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"}
1
This is not even a valid json,
– Xiaoy312
Jan 3 at 16:54
add a comment |
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"}
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 Jan 3 at 16:52
Marjan KalanakiMarjan Kalanaki
63129
63129
1
This is not even a valid json,
– Xiaoy312
Jan 3 at 16:54
add a comment |
1
This is not even a valid json,
– Xiaoy312
Jan 3 at 16:54
1
1
This is not even a valid json,
– Xiaoy312
Jan 3 at 16:54
This is not even a valid json,
– Xiaoy312
Jan 3 at 16:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025489%2fhow-to-deserialize-an-array-of-json-rpc-response-with-json-net-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown

1
Have you tried
JsonConvert.DeserializeObject<RPCResponse<string>>(response)?– Pawel
Jan 3 at 15:51