Cannot deserialize the current JSON array (e.g. [1,2,3]) into type












26














I have a class like this;



public  class MyStok
{
public int STId { get; set; }
public int SM { get; set; }
public string CA { get; set; }
public string Br { get; set; }
public string BNo { get; set; }
public decimal Vat { get; set; }
public decimal Price { get; set; }
}


I deserilaze like this;



string sc=  e.ExtraParams["sc"].ToString();
MyStok myobj = JSON.Deserialize<MyStok>(sc);


my output seems to like this ( string sc) on fiddler



[
{
"STId": 2,
"CA": "hbh",
"Br": "jhnj",
"SM": 20,
"Vat": 10,
"Price": 566,
"BNo": "1545545"
}
]


but I get the error Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
whats wrong in those codes.



thank you .










share|improve this question





























    26














    I have a class like this;



    public  class MyStok
    {
    public int STId { get; set; }
    public int SM { get; set; }
    public string CA { get; set; }
    public string Br { get; set; }
    public string BNo { get; set; }
    public decimal Vat { get; set; }
    public decimal Price { get; set; }
    }


    I deserilaze like this;



    string sc=  e.ExtraParams["sc"].ToString();
    MyStok myobj = JSON.Deserialize<MyStok>(sc);


    my output seems to like this ( string sc) on fiddler



    [
    {
    "STId": 2,
    "CA": "hbh",
    "Br": "jhnj",
    "SM": 20,
    "Vat": 10,
    "Price": 566,
    "BNo": "1545545"
    }
    ]


    but I get the error Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
    whats wrong in those codes.



    thank you .










    share|improve this question



























      26












      26








      26


      3





      I have a class like this;



      public  class MyStok
      {
      public int STId { get; set; }
      public int SM { get; set; }
      public string CA { get; set; }
      public string Br { get; set; }
      public string BNo { get; set; }
      public decimal Vat { get; set; }
      public decimal Price { get; set; }
      }


      I deserilaze like this;



      string sc=  e.ExtraParams["sc"].ToString();
      MyStok myobj = JSON.Deserialize<MyStok>(sc);


      my output seems to like this ( string sc) on fiddler



      [
      {
      "STId": 2,
      "CA": "hbh",
      "Br": "jhnj",
      "SM": 20,
      "Vat": 10,
      "Price": 566,
      "BNo": "1545545"
      }
      ]


      but I get the error Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
      whats wrong in those codes.



      thank you .










      share|improve this question















      I have a class like this;



      public  class MyStok
      {
      public int STId { get; set; }
      public int SM { get; set; }
      public string CA { get; set; }
      public string Br { get; set; }
      public string BNo { get; set; }
      public decimal Vat { get; set; }
      public decimal Price { get; set; }
      }


      I deserilaze like this;



      string sc=  e.ExtraParams["sc"].ToString();
      MyStok myobj = JSON.Deserialize<MyStok>(sc);


      my output seems to like this ( string sc) on fiddler



      [
      {
      "STId": 2,
      "CA": "hbh",
      "Br": "jhnj",
      "SM": 20,
      "Vat": 10,
      "Price": 566,
      "BNo": "1545545"
      }
      ]


      but I get the error Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
      whats wrong in those codes.



      thank you .







      c# asp.net json






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 3 '16 at 22:37









      Danny Fardy Jhonston Bermúdez

      4,83651833




      4,83651833










      asked Jul 20 '13 at 11:58









      sakir

      2,08062349




      2,08062349
























          3 Answers
          3






          active

          oldest

          votes


















          57














          It looks like the string contains an array with a single MyStok object in it. If you remove square brackets from both ends of the input, you should be able to deserialize the data as a single object:



          MyStok myobj = JSON.Deserialize<MyStok>(sc.Substring(1, sc.Length-2));


          You could also deserialize the array into a list of MyStok objects, and take the object at index zero.



          var myobjList = JSON.Deserialize<List<MyStok>>(sc);
          var myObj = myobjList[0];





          share|improve this answer





















          • thank you ,It works fine
            – sakir
            Jul 20 '13 at 12:10






          • 2




            Hmmm. Almost ready to downvote for your first suggestion, but saved by your second.
            – spender
            Jul 20 '13 at 12:43






          • 14




            @spender I prefer the second way, too, but I think it's nice to give people options: it puts them in a position to make a choice, ultimately resulting in better understanding of the problem.
            – dasblinkenlight
            Jul 20 '13 at 12:53



















          18














          For array type Please try this one.



           List<MyStok> myDeserializedObjList = (List<MyStok>)Newtonsoft.Json.JsonConvert.DeserializeObject(sc), typeof(List<MyStok>));


          Please See here for details to deserialise Json






          share|improve this answer























          • thank you your code works fine as well
            – sakir
            Jul 20 '13 at 12:29






          • 2




            There is a open parenthese that is missing in your code...
            – Sven Borden
            Apr 12 '17 at 8:48



















          1














          HttpClient webClient = new HttpClient();
          Uri uri = new Uri("your url");
          HttpResponseMessage response = await webClient.GetAsync(uri)
          var jsonString = await response.Content.ReadAsStringAsync();
          var objData = JsonConvert.DeserializeObject<List<CategoryModel>>(jsonString);





          share|improve this answer





















          • This doesn't really add any value to the question or the other answers.
            – infl3x
            Apr 25 '17 at 10:26










          • this will apply only if you have HttpClient Call I marked as the answer is useful Cause the last line has the answer which is Deserialize it as a list of the class JsonConvert.DeserializeObject<List<CategoryModel>>(jsonString);
            – khaled Dehia
            Jul 3 '17 at 17:00











          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%2f17762032%2fcannot-deserialize-the-current-json-array-e-g-1-2-3-into-type%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          57














          It looks like the string contains an array with a single MyStok object in it. If you remove square brackets from both ends of the input, you should be able to deserialize the data as a single object:



          MyStok myobj = JSON.Deserialize<MyStok>(sc.Substring(1, sc.Length-2));


          You could also deserialize the array into a list of MyStok objects, and take the object at index zero.



          var myobjList = JSON.Deserialize<List<MyStok>>(sc);
          var myObj = myobjList[0];





          share|improve this answer





















          • thank you ,It works fine
            – sakir
            Jul 20 '13 at 12:10






          • 2




            Hmmm. Almost ready to downvote for your first suggestion, but saved by your second.
            – spender
            Jul 20 '13 at 12:43






          • 14




            @spender I prefer the second way, too, but I think it's nice to give people options: it puts them in a position to make a choice, ultimately resulting in better understanding of the problem.
            – dasblinkenlight
            Jul 20 '13 at 12:53
















          57














          It looks like the string contains an array with a single MyStok object in it. If you remove square brackets from both ends of the input, you should be able to deserialize the data as a single object:



          MyStok myobj = JSON.Deserialize<MyStok>(sc.Substring(1, sc.Length-2));


          You could also deserialize the array into a list of MyStok objects, and take the object at index zero.



          var myobjList = JSON.Deserialize<List<MyStok>>(sc);
          var myObj = myobjList[0];





          share|improve this answer





















          • thank you ,It works fine
            – sakir
            Jul 20 '13 at 12:10






          • 2




            Hmmm. Almost ready to downvote for your first suggestion, but saved by your second.
            – spender
            Jul 20 '13 at 12:43






          • 14




            @spender I prefer the second way, too, but I think it's nice to give people options: it puts them in a position to make a choice, ultimately resulting in better understanding of the problem.
            – dasblinkenlight
            Jul 20 '13 at 12:53














          57












          57








          57






          It looks like the string contains an array with a single MyStok object in it. If you remove square brackets from both ends of the input, you should be able to deserialize the data as a single object:



          MyStok myobj = JSON.Deserialize<MyStok>(sc.Substring(1, sc.Length-2));


          You could also deserialize the array into a list of MyStok objects, and take the object at index zero.



          var myobjList = JSON.Deserialize<List<MyStok>>(sc);
          var myObj = myobjList[0];





          share|improve this answer












          It looks like the string contains an array with a single MyStok object in it. If you remove square brackets from both ends of the input, you should be able to deserialize the data as a single object:



          MyStok myobj = JSON.Deserialize<MyStok>(sc.Substring(1, sc.Length-2));


          You could also deserialize the array into a list of MyStok objects, and take the object at index zero.



          var myobjList = JSON.Deserialize<List<MyStok>>(sc);
          var myObj = myobjList[0];






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 20 '13 at 12:01









          dasblinkenlight

          609k567711189




          609k567711189












          • thank you ,It works fine
            – sakir
            Jul 20 '13 at 12:10






          • 2




            Hmmm. Almost ready to downvote for your first suggestion, but saved by your second.
            – spender
            Jul 20 '13 at 12:43






          • 14




            @spender I prefer the second way, too, but I think it's nice to give people options: it puts them in a position to make a choice, ultimately resulting in better understanding of the problem.
            – dasblinkenlight
            Jul 20 '13 at 12:53


















          • thank you ,It works fine
            – sakir
            Jul 20 '13 at 12:10






          • 2




            Hmmm. Almost ready to downvote for your first suggestion, but saved by your second.
            – spender
            Jul 20 '13 at 12:43






          • 14




            @spender I prefer the second way, too, but I think it's nice to give people options: it puts them in a position to make a choice, ultimately resulting in better understanding of the problem.
            – dasblinkenlight
            Jul 20 '13 at 12:53
















          thank you ,It works fine
          – sakir
          Jul 20 '13 at 12:10




          thank you ,It works fine
          – sakir
          Jul 20 '13 at 12:10




          2




          2




          Hmmm. Almost ready to downvote for your first suggestion, but saved by your second.
          – spender
          Jul 20 '13 at 12:43




          Hmmm. Almost ready to downvote for your first suggestion, but saved by your second.
          – spender
          Jul 20 '13 at 12:43




          14




          14




          @spender I prefer the second way, too, but I think it's nice to give people options: it puts them in a position to make a choice, ultimately resulting in better understanding of the problem.
          – dasblinkenlight
          Jul 20 '13 at 12:53




          @spender I prefer the second way, too, but I think it's nice to give people options: it puts them in a position to make a choice, ultimately resulting in better understanding of the problem.
          – dasblinkenlight
          Jul 20 '13 at 12:53













          18














          For array type Please try this one.



           List<MyStok> myDeserializedObjList = (List<MyStok>)Newtonsoft.Json.JsonConvert.DeserializeObject(sc), typeof(List<MyStok>));


          Please See here for details to deserialise Json






          share|improve this answer























          • thank you your code works fine as well
            – sakir
            Jul 20 '13 at 12:29






          • 2




            There is a open parenthese that is missing in your code...
            – Sven Borden
            Apr 12 '17 at 8:48
















          18














          For array type Please try this one.



           List<MyStok> myDeserializedObjList = (List<MyStok>)Newtonsoft.Json.JsonConvert.DeserializeObject(sc), typeof(List<MyStok>));


          Please See here for details to deserialise Json






          share|improve this answer























          • thank you your code works fine as well
            – sakir
            Jul 20 '13 at 12:29






          • 2




            There is a open parenthese that is missing in your code...
            – Sven Borden
            Apr 12 '17 at 8:48














          18












          18








          18






          For array type Please try this one.



           List<MyStok> myDeserializedObjList = (List<MyStok>)Newtonsoft.Json.JsonConvert.DeserializeObject(sc), typeof(List<MyStok>));


          Please See here for details to deserialise Json






          share|improve this answer














          For array type Please try this one.



           List<MyStok> myDeserializedObjList = (List<MyStok>)Newtonsoft.Json.JsonConvert.DeserializeObject(sc), typeof(List<MyStok>));


          Please See here for details to deserialise Json







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 20 '13 at 12:08

























          answered Jul 20 '13 at 12:07









          Janty

          1,41321025




          1,41321025












          • thank you your code works fine as well
            – sakir
            Jul 20 '13 at 12:29






          • 2




            There is a open parenthese that is missing in your code...
            – Sven Borden
            Apr 12 '17 at 8:48


















          • thank you your code works fine as well
            – sakir
            Jul 20 '13 at 12:29






          • 2




            There is a open parenthese that is missing in your code...
            – Sven Borden
            Apr 12 '17 at 8:48
















          thank you your code works fine as well
          – sakir
          Jul 20 '13 at 12:29




          thank you your code works fine as well
          – sakir
          Jul 20 '13 at 12:29




          2




          2




          There is a open parenthese that is missing in your code...
          – Sven Borden
          Apr 12 '17 at 8:48




          There is a open parenthese that is missing in your code...
          – Sven Borden
          Apr 12 '17 at 8:48











          1














          HttpClient webClient = new HttpClient();
          Uri uri = new Uri("your url");
          HttpResponseMessage response = await webClient.GetAsync(uri)
          var jsonString = await response.Content.ReadAsStringAsync();
          var objData = JsonConvert.DeserializeObject<List<CategoryModel>>(jsonString);





          share|improve this answer





















          • This doesn't really add any value to the question or the other answers.
            – infl3x
            Apr 25 '17 at 10:26










          • this will apply only if you have HttpClient Call I marked as the answer is useful Cause the last line has the answer which is Deserialize it as a list of the class JsonConvert.DeserializeObject<List<CategoryModel>>(jsonString);
            – khaled Dehia
            Jul 3 '17 at 17:00
















          1














          HttpClient webClient = new HttpClient();
          Uri uri = new Uri("your url");
          HttpResponseMessage response = await webClient.GetAsync(uri)
          var jsonString = await response.Content.ReadAsStringAsync();
          var objData = JsonConvert.DeserializeObject<List<CategoryModel>>(jsonString);





          share|improve this answer





















          • This doesn't really add any value to the question or the other answers.
            – infl3x
            Apr 25 '17 at 10:26










          • this will apply only if you have HttpClient Call I marked as the answer is useful Cause the last line has the answer which is Deserialize it as a list of the class JsonConvert.DeserializeObject<List<CategoryModel>>(jsonString);
            – khaled Dehia
            Jul 3 '17 at 17:00














          1












          1








          1






          HttpClient webClient = new HttpClient();
          Uri uri = new Uri("your url");
          HttpResponseMessage response = await webClient.GetAsync(uri)
          var jsonString = await response.Content.ReadAsStringAsync();
          var objData = JsonConvert.DeserializeObject<List<CategoryModel>>(jsonString);





          share|improve this answer












          HttpClient webClient = new HttpClient();
          Uri uri = new Uri("your url");
          HttpResponseMessage response = await webClient.GetAsync(uri)
          var jsonString = await response.Content.ReadAsStringAsync();
          var objData = JsonConvert.DeserializeObject<List<CategoryModel>>(jsonString);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 25 '17 at 9:43









          Aditi_Systematix

          735




          735












          • This doesn't really add any value to the question or the other answers.
            – infl3x
            Apr 25 '17 at 10:26










          • this will apply only if you have HttpClient Call I marked as the answer is useful Cause the last line has the answer which is Deserialize it as a list of the class JsonConvert.DeserializeObject<List<CategoryModel>>(jsonString);
            – khaled Dehia
            Jul 3 '17 at 17:00


















          • This doesn't really add any value to the question or the other answers.
            – infl3x
            Apr 25 '17 at 10:26










          • this will apply only if you have HttpClient Call I marked as the answer is useful Cause the last line has the answer which is Deserialize it as a list of the class JsonConvert.DeserializeObject<List<CategoryModel>>(jsonString);
            – khaled Dehia
            Jul 3 '17 at 17:00
















          This doesn't really add any value to the question or the other answers.
          – infl3x
          Apr 25 '17 at 10:26




          This doesn't really add any value to the question or the other answers.
          – infl3x
          Apr 25 '17 at 10:26












          this will apply only if you have HttpClient Call I marked as the answer is useful Cause the last line has the answer which is Deserialize it as a list of the class JsonConvert.DeserializeObject<List<CategoryModel>>(jsonString);
          – khaled Dehia
          Jul 3 '17 at 17:00




          this will apply only if you have HttpClient Call I marked as the answer is useful Cause the last line has the answer which is Deserialize it as a list of the class JsonConvert.DeserializeObject<List<CategoryModel>>(jsonString);
          – khaled Dehia
          Jul 3 '17 at 17:00


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f17762032%2fcannot-deserialize-the-current-json-array-e-g-1-2-3-into-type%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

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks