我怎样才能将这样的字符串序列化:"Yes,No"
变成这样的东西{"0":"Yes", "1":"No"}
?
我已将字符串拆分,','
并将值存储到KeyValuePair<int,string>
. 序列化后,结果是这样的:
{"Key" : "0" , "Value" : "Yes"},{"Key":"1","Value":"No"}
我怎样才能将这样的字符串序列化:"Yes,No"
变成这样的东西{"0":"Yes", "1":"No"}
?
我已将字符串拆分,','
并将值存储到KeyValuePair<int,string>
. 序列化后,结果是这样的:
{"Key" : "0" , "Value" : "Yes"},{"Key":"1","Value":"No"}
var result = yourString.Split(',').Select((v, k) => new { k, v, })
.ToDictionary(a => a.k, a => a.v);
要获得您要求的最终结果,您可以这样做:
String.Join(",", input.Split(',')
.Select((s,i) => "{\"Key\":\""+i+"\",\"Value\":\""+s+"\"}"));
你总是可以自己序列化它:
string theString = "True,False";
var result = theString.Split(',')
.Select((v, i) => string.Format("\"{0}\":\"{1}\"", i, v));
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]);
}