JSON输出字符串到数组或列表中

时间:2018-12-19 08:03:08

标签: json list dictionary

嗨,我正在尝试将json输出设置为数组或列表,但收到错误 “无法从system.collections.generic.list转换为String []”

在paramValue中收到的字符串-

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

List<string> strlist;
strlist = new List<string>();

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

foreach (var item in jr)
{
   //Need some thing like this  
  // strList.Add(item)
}

为了在列表中使用“ item”值,我还尝试了item.Value.CopyTo(strlist);,但收到错误消息“无法从system.collections.generic.list转换为String []”。

实际上,我需要将“ item”值存储在List中,以便可以传递到另一个函数中。

谢谢

1 个答案:

答案 0 :(得分:0)

dictionary上迭代时,您在值中包含List<string>,因此无法使用add将值数组添加到List<string> strlist; {{1} }期望它是单个值而不是多个,因为要添加多个值,可以使用AddRange

像这样

add

如果您想使用短号,请使用

foreach (var item in jr)
{
     //Need some thing like this  
     strlist.AddRange(item.Value);
}