var tStr = "0|1:开门|2:关门|3:门检失败|4:开门|5:开门|6:关门"; var result = string.Format("[{0}]", string.Join(",", tStr.Substring(2).Split('|').Select(s => string.Format("{{doorid:{0}, status:{1}}}", s.Split(':')[0], s.Split(':')[1]).Replace("开门", "1").Replace("关门", "0").Replace("门检失败", "-1")).ToArray())); MessageBox.Show(result); //string tStr = "0|1:开门|2:关门|3:门检失败|4:开门|5:开门|6:关门"; //tStr = tStr.Replace(":", ","); //tStr = tStr.Replace("开门", "status:1}"); //tStr = tStr.Replace("关门", "status:0}"); //tStr = tStr.Replace("门检失败", "status:-1}"); ////tStr = tStr.Replace("|", "{doorid:"); //string[] str = tStr.Split('|'); //string strs = "["; //for (int i = 1; i < str.Length; i++) //{ // MessageBox.Show(i.ToString()); // if (i < str.Length-1) // { // strs += "{doorid:" + str[i] + ","; // } else if( i== str.Length - 1) // { // strs += "{doorid:" + str[i] + "]"; // } //} //MessageBox.Show(strs);