C# newtonsoft对json进行排序

/// 
/// 对json得键进行排序
/// 
/// 
/// 
public static string SortJson(string json)
{
	var tokenType = GetJsonType(json);
	if (tokenType == JTokenType.Object)
	{
		var dic = JsonConvert.DeserializeObject<SortedDictionary<string, object>>(json);
		SortedDictionary<string, object> keyValues = new SortedDictionary<string, object>(dic);
		keyValues.OrderBy(m => m.Key);//升序 把Key换成Value 就是对Value进行排序
									  //keyValues.OrderByDescending(m => m.Key);//降序
		SortedDictionary<string, object> tempKeyValues = new SortedDictionary<string, object>(keyValues);
		foreach (KeyValuePair<string, object> kv in tempKeyValues)
		{
			// 判断value是不是JObject类型
			Type t1 = kv.Value.GetType();
			if (t1 == typeof(JObject))
			{
				// value是JObject类型
				string jsonItem = JsonConvert.SerializeObject(kv.Value);
				jsonItem = SortJson(jsonItem);
				keyValues[kv.Key] = JsonConvert.DeserializeObject<JObject>(jsonItem);
			}
			else if (t1 == typeof(JArray))
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.Append("[");
				bool isFirst = true;
				foreach (JToken item in (JArray)kv.Value)
				{
					if (item.Type == JTokenType.Object)
					{
						string jsonArrayItem = JsonConvert.SerializeObject(item);
						jsonArrayItem = SortJson(jsonArrayItem);
						if (isFirst)
						{
							stringBuilder.Append(jsonArrayItem);
						}
						else
						{
							stringBuilder.Append($",{jsonArrayItem}");
						}

					}
					else if (item.Type == JTokenType.Array)
					{
						var arrayTmpRes = SortJson(JsonConvert.SerializeObject(item));
						if (isFirst)
						{
							stringBuilder.Append(arrayTmpRes);
						}
						else
						{
							stringBuilder.Append($",{arrayTmpRes}");
						}
					}
					else
					{
						string jsonArrayItem = JsonConvert.SerializeObject(item);
						if (isFirst)
						{
							stringBuilder.Append(jsonArrayItem);
						}
						else
						{
							stringBuilder.Append($",{jsonArrayItem}");
						}
					}
					isFirst = false;
				}
				stringBuilder.Append("]");
				var tmpJsonStr = stringBuilder.ToString();
				keyValues[kv.Key] = JsonConvert.DeserializeObject<JArray>(tmpJsonStr);
			}
		}
		return JsonConvert.SerializeObject(keyValues);
	}
	else if (tokenType == JTokenType.Array)
	{
		var arraies = JsonConvert.DeserializeObject<JArray>(json);
		StringBuilder stringBuilder2 = new StringBuilder();
		stringBuilder2.Append("[");
		bool isFirst2 = true;
		foreach (JToken item in arraies)
		{
			if (item.Type == JTokenType.Object)
			{
				string jsonArrayItem = JsonConvert.SerializeObject(item);
				jsonArrayItem = SortJson(jsonArrayItem);
				if (isFirst2)
				{
					stringBuilder2.Append(jsonArrayItem);
				}
				else
				{
					stringBuilder2.Append($",{jsonArrayItem}");
				}

			}
			else if (item.Type == JTokenType.Array)
			{
				var arrayTmpRes = SortJson(JsonConvert.SerializeObject(item));
				if (isFirst2)
				{
					stringBuilder2.Append(arrayTmpRes);
				}
				else
				{
					stringBuilder2.Append($",{arrayTmpRes}");
				}
			}
			else
			{
				string jsonArrayItem = JsonConvert.SerializeObject(item);
				if (isFirst2)
				{
					stringBuilder2.Append(jsonArrayItem);
				}
				else
				{
					stringBuilder2.Append($",{jsonArrayItem}");
				}
			}
			isFirst2 = false;
		}
		stringBuilder2.Append("]");
		return stringBuilder2.ToString();
	}
	return json;
}

/// 
/// 根据json判断json类型
/// 
/// 
/// 
public static JTokenType GetJsonType(string json)
{
	JToken token = JToken.Parse(json);
	return token.Type;
}

测试

var settings = new JsonSerializerSettings
{
	DefaultValueHandling = DefaultValueHandling.Ignore
};
var paramModel = new ParamModel();
paramModel.PageNumber = 20;
paramModel.PageSize = 1;
paramModel.StaffStatus = "1222";
paramModel.StaffNameMobile = "xxxx";
var json = JsonConvert.SerializeObject(paramModel, settings);
var res = Utils.SortJson(json);
//{"pageNumber":20,"pageSize":1,"staffNameMobile":"xxxx","staffStatus":"1222"}
Assert.NotNull(res);
var param2 = new Param2Model();
param2 .StaffList = new List<Param3Model>();
param2 .StaffList.Add(new Param3Model
{
	IdCard = "22222",
	//Mobile = "111",
	OrgId = "2233",
	OrgName = "sdfsdf",
	StaffName = "dsss",
});

json = JsonConvert.SerializeObject(param2 , settings);
res = Utils.SortJson(json);
Assert.NotNull(res);

你可能感兴趣的:(dotnet,c#,json,开发语言)