码迷,mamicode.com
首页 > Windows程序 > 详细

C#文本转换为Json格式

时间:2019-07-16 18:33:49      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:div   转换   indent   iter   index   csharp   return   ons   arp   

private string ConvertJsonString(string str)

       {
           //格式化json字符串
           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;
           }         
       }

C#文本转换为Json格式

标签:div   转换   indent   iter   index   csharp   return   ons   arp   

原文地址:https://www.cnblogs.com/LCLBook/p/11196362.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!