zoukankan      html  css  js  c++  java
  • 使用Json比用string返回数据更友好,也更面向对象一些

    以前的程序:

    ViewData["msg"] = "{result:true,FileName:\"" + new FileInfo(Request.Files[0].FileName).Name +
                      "\",FileUrl:\"" + fileurl.Replace("\"", "") + "\"}";
    重构之后的程序:
     JsonResult j = Json(new
                     {
                         Result = true,
                         FileName = new FileInfo(Request.Files[0].FileName).Name,
                         FileUrl = fileurl.Replace("\"", "")
                     });
                    ViewData["msg"] = j.Data.ToString();

    很明显,第二种方式可读性更强,在性能上第一种略好于第二种,但对于程序来讲,性能只是一方面

    在面向对象的程序来讲,它的可能性,编写代码不易出错率也很重要,所以,建议在返回复杂字条符时,使用C#提供的Json,而不需要自己去拼写JS返回的Json格式。

  • 相关阅读:
    For each···in / For···in / For···of
    JavaScript object
    specific word count (index of )
    history of program
    js的回调函数
    promise
    js的事件流事件机制
    js的closures(闭包)
    baidu-map
    基于封装优点的类设计习惯
  • 原文地址:https://www.cnblogs.com/lori/p/2174586.html
Copyright © 2011-2022 走看看