-1

我怎样才能将这样的字符串序列化:"Yes,No"变成这样的东西{"0":"Yes", "1":"No"}

我已将字符串拆分,','并将值存储到KeyValuePair<int,string>. 序列化后,结果是这样的:

{"Key" : "0" , "Value" : "Yes"},{"Key":"1","Value":"No"}

4

4 回答 4

3
var result = yourString.Split(',').Select((v, k) => new { k, v, })
    .ToDictionary(a => a.k, a => a.v);
于 2013-10-13T22:36:13.667 回答
0

要获得您要求的最终结果,您可以这样做:

String.Join(",", input.Split(',')
                 .Select((s,i) => "{\"Key\":\""+i+"\",\"Value\":\""+s+"\"}"));
于 2013-10-13T22:41:56.580 回答
0

你总是可以自己序列化它:

string theString = "True,False";
var result = theString.Split(',')
                      .Select((v, i) => string.Format("\"{0}\":\"{1}\"", i, v));
于 2013-10-13T22:44:01.497 回答
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]);
}
于 2013-10-13T22:44:36.117 回答