字符串格式的json字符串转换为json格式的字符串

 

/*

 string src= {respcode:00,resmsg:success,out_trade_no:20160415100822}

 string src={respcode:01,resmsg:failure,respmsg:500704_非正常商户状态,out_trade_no:20160415100822}

*/

 

public static string convertEasyToJson(string src)
{
if (null == src)
{
return null;
}
String[] keys0 = { "respcode", "resmsg","out_trade_no"};
String[] keys1 = { "respcode", "resmsg", "respmsg",
"out_trade_no" };
Dictionary<string, string> result = new Dictionary<string, string>();
string respcode = src.Substring(10, 2);
string[] keys = ("00".Equals(respcode) ? keys0 : keys1);
for (int i = 0; i < keys.Count(); i++)
{
if (i == keys.Count() - 1)
{
var s1 = src.IndexOf(keys[i] + ":");
var s2 = (keys[i] + ":").Length;
var s = src.Count() - 1;
result.Add(
keys[i],
src.Substring(s1 + s2,
s - (s1 + s2)));
}
else
{
var s = src.IndexOf("," + keys[i + 1] + ":");
var s1 = src.IndexOf(keys[i] + ":");
var s2 = (keys[i] + ":").Count();
result.Add(
keys[i],
src.Substring(s1 + s2,
s - (s1 + s2)));
}
}
string strDic = Newtonsoft.Json.JsonConvert.SerializeObject(result);
return strDic;
}

posted @ 2018-03-29 10:15  糖晒晒  阅读(214)  评论(0编辑  收藏  举报