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












0















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.









share|improve this question


















  • 1





    Have you tried JsonConvert.DeserializeObject<RPCResponse<string>>(response)?

    – Pawel
    Jan 3 at 15:51
















0















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.









share|improve this question


















  • 1





    Have you tried JsonConvert.DeserializeObject<RPCResponse<string>>(response)?

    – Pawel
    Jan 3 at 15:51














0












0








0








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.









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 15:42









Mark AllisonMark Allison

2,5592073122




2,5592073122








  • 1





    Have you tried JsonConvert.DeserializeObject<RPCResponse<string>>(response)?

    – Pawel
    Jan 3 at 15:51














  • 1





    Have you tried JsonConvert.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












2 Answers
2






active

oldest

votes


















2














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



JsonConvert.DeserializeObject<RPCResponse<string>>(response);





share|improve this answer































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





    share|improve this answer



















    • 1





      This is not even a valid json,

      – Xiaoy312
      Jan 3 at 16:54












    Your Answer






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


    }
    });














    draft saved

    draft discarded


















    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









    2














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



    JsonConvert.DeserializeObject<RPCResponse<string>>(response);





    share|improve this answer




























      2














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



      JsonConvert.DeserializeObject<RPCResponse<string>>(response);





      share|improve this answer


























        2












        2








        2







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



        JsonConvert.DeserializeObject<RPCResponse<string>>(response);





        share|improve this answer













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



        JsonConvert.DeserializeObject<RPCResponse<string>>(response);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 15:51









        Xiaoy312Xiaoy312

        11.5k12234




        11.5k12234

























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





            share|improve this answer



















            • 1





              This is not even a valid json,

              – Xiaoy312
              Jan 3 at 16:54
















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





            share|improve this answer



















            • 1





              This is not even a valid json,

              – Xiaoy312
              Jan 3 at 16:54














            -3












            -3








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





            share|improve this answer













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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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














            • 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


















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Mossoró

            Can't read property showImagePicker of undefined in react native iOS

            Pushsharp Apns notification error: 'InvalidToken'