获取特定JSON字符串所有键值对,拼接进行签名

时间:2019-04-18
本文章向大家介绍获取特定JSON字符串所有键值对,拼接进行签名,主要包括获取特定JSON字符串所有键值对,拼接进行签名使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

public static string GetSignNew(object obj, string timestamp)
{
List<string> listStr = new List<string>();
listStr.Add("timestamp" + "="+timestamp+"&");
listStr.Add("apiKey"+"="+apiKey+"&");
JObject job1 = JObject.FromObject(obj);
JProperty[] job1strArr=job1.Properties().ToArray();
List<string> nsolvedList = new List<string>();//存储未解决的值
foreach (var x in job1)
{
if (!string.IsNullOrEmpty(x.Value.ToString()))
{
if (x.Key != "segmentList" && x.Key != "fareBreakdownList" && x.Key != "touristList" && x.Key != "distributeInfo" && x.Key != "requirement" && x.Key != "flightsInfoList" && x.Key != "delivery" && x.Key != "invoiceList" && x.Key != "priceList")
listStr.Add(x.Key+"="+x.Value.ToString()+"&");//添加
else
nsolvedList.Add(x.Value.ToString());//添加到List,进一步处理
}
}
string tmpprice = "";//记录下单的prices
for (int i = 0; i < nsolvedList.Count; i++)
{
if (!string.IsNullOrEmpty(nsolvedList[i]))
{
nsolvedList[i] = nsolvedList[i].Replace("[", string.Empty);
nsolvedList[i] = nsolvedList[i].Replace("]", string.Empty);
JObject JobTmp = JObject.Parse(nsolvedList[i]);
foreach (var x in JobTmp)
{
if (!string.IsNullOrEmpty(x.Value.ToString()) && x.Key != "prices")
{
listStr.Add(x.Key + "=" + x.Value.ToString() + "&");//添加
}
else
tmpprice = x.Value.ToString();
}
}
}
//有记录才执行
if (!string.IsNullOrEmpty(tmpprice))
{
JObject JobTmp = JObject.Parse(tmpprice);
foreach (var x in JobTmp)
{
if (!string.IsNullOrEmpty(x.Value.ToString()))
{
listStr.Add(x.Key + "=" + x.Value.ToString() + "&");//添加
}
}
}
//转换为数组,执行区分大小写的排序
string[] listArr = listStr.ToArray();
Array.Sort(listArr,StringComparer.Ordinal);
string tmpSign = "";
for (int i = 0; i < listArr.Length; i++)
{
tmpSign += listArr[i];
}
tmpSign = SecretKey + "&" + tmpSign + SecretKey;
string sign = MD5en.StringToMD5(tmpSign, "UTF-8");//MD5
return sign.ToUpper();//返回大写的MD5
}