//找出dictionary中key值最大的值
var keys =dit.OrderByDescending(t=>t.Value).Select(q => q.Key);
List<string> keyList=keys.ToList<string>();
Console.WriteLine(keyList[0]);
// hashids加密和解密单个id
Hashids hashids= new Hashids("this is my salt");
//加密数字1
String hashStr = hashids.encode(1);
System.out.println("1加密后值为:"+hashStr);
//解密数字1
long[] hashNums = hashids.decode(hashStr);
for (int i = 0; i < hashNums.length; i++) {
System.out.println("解密后值为:" + hashNums[i]);
}
//JsonConvert.SerializeObject输出的格式化
var dic = new Dictionary<string,object>
{
["a"] = 10,
["b"] = null,
["c"] = new object[] {1,5,"3"},
["d"] = new Dictionary<string, object>
{
["e"] = "E"
},
};
dic.Dump();
JsonConvert.SerializeObject(dic, Newtonsoft.Json.Formatting.Indented).Dump();
Console.WriteLine( JsonConvert.SerializeObject(dic, Newtonsoft.Json.Formatting.Indented));
//返回缩进的 Json 字符串
JsonConvert.SerializeObject(person, Formatting.Indented);
//将不缩进的 JSON 字符串转成缩进形式
private string JsonIndentation(string str)
{
//string str = JsonConvert.SerializeObject(entity);
JsonSerializer serializer = new JsonSerializer();
TextReader tr = new StringReader(str);
JsonTextReader jtr = new JsonTextReader(tr);
object obj = serializer.Deserialize(jtr);
if (obj != null)
{
StringWriter textWriter = new StringWriter();
JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
{
Formatting = Formatting.Indented,
Indentation = 4,
IndentChar = ' '
};
serializer.Serialize(jsonWriter, obj);
return textWriter.ToString();
}
else
{
return str;
}
}
//或者
private string JsonIndentation(string json)
{
JObject obj = JObject.Parse(json);
return obj.ToString();
}
//其他的设置
JsonSerializerSettings settings = new JsonSerializerSettings();
// 设置日期格式
settings.DateFormatString = "yyyy-MM-dd";
// 忽略空值
settings.NullValueHandling = NullValueHandling.Ignore;
// 缩进
settings.Formatting = Formatting.Indented;
JsonConvert.SerializeObject(person, settings);
1.4 Json 反序列化
JsonConvert.DeserializeObject<Person>(json);
2 JObject 使用
2.1 创建对象
JObject obj = new JObject(); obj.Add("ID", 1); obj.Add("Name", "张三"); obj.Add("Birthday", DateTime.Parse("2000-01-02")); obj.Add("IsVIP", true); obj.Add("Account", 12.34f); // 创建数组 JArray array = new JArray(); array.Add(new JValue("吃饭")); array.Add(new JValue("睡觉")); obj.Add("Favorites", array); obj.Add("Remark", null);
2.2 JObject 中添加数组
上例中的代码可以简化为:
JArray array = new JArray("吃饭", "睡觉");
2.3 从 Json 字符串创建 JObject
string json = "{\"ID\":1,\"Name\":\"张三\",\"Birthday\":\"2000-01-02T00:00:00\",\"IsVIP\":true,\"Account\":12.34,\"Favorites\":[\"吃饭\",\"睡觉\"],\"Remark\":null}";
JObject obj = JObject.Parse(json);
2.4 从 Entity 创建 JObject
JObject obj = JObject.FromObject(person);
用匿名对象创建 JObject
JObject obj = JObject.FromObject(new { name = "jack", age = 18 });
//显示
{
"name": "jack",
"age": 18
}
用初始化器
JObject obj = new JObject()
{
{ "name", "jack" },
{ "age", 18 }
};
2.5 获取值
int id;
if (obj["ID"] != null)
id = obj["ID"].Value<int>();
2.6 获取数组
Newtonsoft.Json.Linq 不支持直接获取数组,但是可以获取 List,然后再转化为数组。
string[] favorites;
if (obj["Favorites"] != null)
favorites = obj["Favorites"].Value<List<string>>().ToArray();