How to convert a list of string
如何转换字符串列表
List<string> keys = new List<string>() { "1-12VEXP", "1-124DH9"};
To json format same as :
格式为:
[["1-12VEXP"],["1-124DH9"]]
in .net.
在。net。
I'm using Newtonsoft.Json .
我用Newtonsoft。Json。
Any help will be greatly appreciated.
如有任何帮助,我们将不胜感激。
2 个解决方案
#1
16
Straight-up serialization won't work, since the items are not equivalent. If you truly want what you're asking for, then you need an array which contains arrays, then serialize that array:
直接序列化将不起作用,因为这些项是不相等的。如果你真的想要你想要的,那么你需要一个包含数组的数组,然后序列化这个数组:
You can do that by first converting your collection, then simple JSON serialization:
您可以通过首先转换集合,然后简单的JSON序列化来实现:
string[][] newKeys = keys.Select(x => new string[]{x}).ToArray();
string json = JsonConvert.SerializeObject(newKeys);
#2
0
With Newtonsoft.Json:
Newtonsoft.Json:
JsonConvert.SerializeObject(keys);
will give you JSON.
会给你JSON。
#1
16
Straight-up serialization won't work, since the items are not equivalent. If you truly want what you're asking for, then you need an array which contains arrays, then serialize that array:
直接序列化将不起作用,因为这些项是不相等的。如果你真的想要你想要的,那么你需要一个包含数组的数组,然后序列化这个数组:
You can do that by first converting your collection, then simple JSON serialization:
您可以通过首先转换集合,然后简单的JSON序列化来实现:
string[][] newKeys = keys.Select(x => new string[]{x}).ToArray();
string json = JsonConvert.SerializeObject(newKeys);
#2
0
With Newtonsoft.Json:
Newtonsoft.Json:
JsonConvert.SerializeObject(keys);
will give you JSON.
会给你JSON。