.net 将string字符串转为json对象的两种方法

初秋的小犀牛 / 2024-09-27 / 原文

1)将string直接转为json 【注:适合信息量比较少的情况】

   string str = "{\"id\":\"s001\",\"name\":\"张三\",\"gender\":\"男\"}"

    【注:上述中\起转义作用】



2)将string信息转为list对象后再通过list对象转为json 【注:适合信息量比较少的情况】

   Dictionary dic = new Dictionary();"

   dic.Add("id", "s001");"

   dic.Add("name","李亖");"

   dic.Add("gender","男");"

   string str = JsonConvert.SerializeObject(dic);

   str的值为: "{\"id\":\"s001\",\"name\":\"李亖\",\"gender\":\"男\"}"