0

I have a requirement of deserializing JSON string and store it into a list or an array. Hence have used bellow give code

dynamic jr = JsonConvert.DeserializeObject(paramList);

foreach (var item.Value in jr)
{

} 

String value in paramList is like

{"0":["1234","2222","4321","211000","90024","12","2121","322223","2332","3232"],"1":["0856","6040222","175002","23572","","","","","",""]}

String value in item.Value is like -

{[  "1234",  "2222",  "4321",  "211000",  "900224",  "12",  "2121",  "322223",  "2332",  "3232"]}

Hence kindly help me to iterate this string, so that i can put into an array or list.

Thanks

soccer7
  • 3,547
  • 3
  • 29
  • 50
dipak
  • 35
  • 6
  • Check out this question. Sounds similar https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp – David Brossard Dec 18 '18 at 23:29
  • Note that `{[ ... ]}` is not valid Json. You'd need just the array (`[...]`) or a key (`{"key": [ ... ]}`). – p.s.w.g Dec 19 '18 at 00:10

1 Answers1

0

Try this-

var jr = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(paramList);

foreach (var item in jr)
{
     // item.Value
} 

Update 1:

Now if you only want to get all values into a list, you can use the following-

var allValues = jr.SelectMany(x => x.Value).ToList();
soccer7
  • 3,547
  • 3
  • 29
  • 50
  • Thanks for your reply, but receiving an error - Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.String]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. – dipak Dec 18 '18 at 23:27
  • @dipak Please check again – soccer7 Dec 18 '18 at 23:34
  • Thanks, it also worked for me, but kindly let me know , how to put "item" which into an List. For eg. foreach (var item in jr) { // Some thing like bellow // List.add( item.Value) } – dipak Dec 19 '18 at 07:31
  • I have updated my answer. Let me know if that works for you. – soccer7 Dec 19 '18 at 15:52