如何将这样的字符串"Yes,No"
序列化为类似{"0":"Yes", "1":"No"}
的内容?
我已将字符串拆分为','
,并且已将值存储到KeyValuePair<int,string>
中。序列化后,结果如下:
{"Key" : "0" , "Value" : "Yes"},{"Key":"1","Value":"No"}
答案 0 :(得分:3)
var result = yourString.Split(',').Select((v, k) => new { k, v, })
.ToDictionary(a => a.k, a => a.v);
答案 1 :(得分:0)
要获得你要求的最终结果,你可以这样做:
String.Join(",", input.Split(',')
.Select((s,i) => "{\"Key\":\""+i+"\",\"Value\":\""+s+"\"}"));
答案 2 :(得分:0)
您可以随时自行序列化:
string theString = "True,False";
var result = theString.Split(',')
.Select((v, i) => string.Format("\"{0}\":\"{1}\"", i, v));
答案 3 :(得分:0)
string s = "yes,no";
var d = s.Split(',');
var result = new Dictionary<int,string>(){};
for (int i = 0; i < d.Length; i++) {
result.Add(i, d[i]);
}