JSON格式传递后转其他格式;转换后数据有杂值问题

JSON格式传递后转其他格式;转换后数据有杂值问题,第1张

    //前后端传值时,数组类型数据无法直接传递,需要转换class="superseo">JSON,将JSON数组数据转为string类型,并通过“,”分割
string[] checkedCities = form["checkedCities"].ToString().Split(',');
            List result = new List(FormatOrganizationName(checkedCities));


    //传值时因出现了不必要的'/r/n'换行符和一些杂乱字母,用了正则表达式取到有用信息
/* public string[] FormatOrganizationName(string[] checkedCities)
        {
            for (int i=0; i

除了正则表达式,还可用其他方法提取需要的值(传递的是中文字符,所以正则筛选的是中文字符)

1.正则表达式
string after = Regex.Replace(before, @"[^a-zA-Z0-9\u4e00-\u9fa5\s]", "") 

2.返回新字符串
//选取原来字符串的起始位置beginIndex,取长度为length的字符,注意,数组起始下标为0
after = before.Substring(beginIndex,length)

如
"unhappy".substring(2) returns"happy
"hamburger".substring(3,8) returns "burge"
"smiles".substring(0,5) returns "smile"

3.直接去除
after = before.Replace('a','b','c')

前端传值时别忘了声明JSON字符串类型

getTotalCount() {
      var self = this
      request({
        url: self.url + '/api/GlitchView/GetTotalCount',
        method: 'get',
        headers: {
        'Authorization': 'Bearer ' + getToken(),
        //作为请求头告诉服务端消息主体是序列化的JSON字符串
        'Content-Type': 'application/json'
        },
        //消息主体是序列化的JSON字符串
        contentType: "application/json",
        params:{ 
        listQuery:self.listQuery,
        //将传输的数据转为JSON格式
        checkedCities:JSON.stringify(self.checkedCities),
        }
      }).then(response => {
        self.total = response
      })
    },

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/734869.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)