求一代码 正则表达式解析字符串

stevenhzj 2009-11-16 02:02:17
原字符串:
"[{Name:\\u4e2d\\u56fd,IdNo:a,\"BirthDt\":\"1990-10-10\",\"Leader\":0,\"ResOnly\":0,\"Mail\":\"aa@a.com\",\"Tel\":\"010888888\",\"Bnfs\":\"ss\"}]"

要求解析出来为数组
Name:\u4e2d\u56fd
IdNo:a
BirthDt:1990-10-10
Leader:0
ResOnly:0
Mail:aa@a.com
Tel:010888888
Bnfs:ss

...全文
173 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lostgdi731 2009-11-16
  • 打赏
  • 举报
回复
原字符串:
"[{Name:\\u4e2d\\u56fd,IdNo:a,\"BirthDt\":\"1990-10-10\",\"Leader\":0,\"ResOnly\":0,\"Mail\":\"aa@a.com\",\"Tel\":\"010888888\",\"Bnfs\":\"ss\"}]"

打包成这样的字符串的时候,需要对一些特殊字段进行编码,调用端再进行解码才合理,因为你不知道字符串字段是否包含里像上边那样的分隔符。
stevenhzj 2009-11-16
  • 打赏
  • 举报
回复
晕了 在页面上显示是没问题 Name:\u4e2d\u56fd
但按编码进行转化时,不能解码为汉字:"中国"
System.Text.Encoding.UTF8.GetString(Encoding.UTF8.GetBytes("\\u4e2d\\u56fd")
十八道胡同 2009-11-16
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = @"[{Name:\\u4e2d\\u56fd,IdNo:a,\""BirthDt\"":\""1990-10-10\"",\""Leader\"":0,\""ResOnly\"":0,\""Mail\"":\""aa@a.com\"",\""Tel\"":\""010888888\"",\""Bnfs\"":\""ss\""}]";
str = str.Replace("[", "");
str = str.Replace("}", "");
str = str.Replace("]", "");
str = str.Replace("{", "");
str = str.Replace(@"\""", "");
string[] s = str.Split(',');
foreach (string ss in s)
{
Console.WriteLine(ss.Replace (@"\\","\\"));
}
}
}
}


就像这样一样
Name:\u4e2d\u56fd
IdNo:a
BirthDt:1990-10-10
Leader:0
ResOnly:0
Mail:aa@a.com
Tel:010888888
Bnfs:ss
Press any key to continue . . .
十八道胡同 2009-11-16
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = @"[{Name:\\u4e2d\\u56fd,IdNo:a,\""BirthDt\"":\""1990-10-10\"",\""Leader\"":0,\""ResOnly\"":0,\""Mail\"":\""aa@a.com\"",\""Tel\"":\""010888888\"",\""Bnfs\"":\""ss\""}]";
Regex re = new Regex(@"({|""|,)(?<key>[^,}]*)");
MatchCollection mc = re.Matches(str);
foreach (Match m in mc)
Console.WriteLine(m.Groups["key"].Value.Replace (@"\""","").Replace(@"\\","\\")); }
}
}

Name:\u4e2d\u56fd
IdNo:a
BirthDt:1990-10-10
Leader:0
ResOnly:0
Mail:aa@a.com
Tel:010888888
Bnfs:ss
Press any key to continue . . .

7楼的代码也是一样的
加个红色部分就可以了
十八道胡同 2009-11-16
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = @"[{Name:\\u4e2d\\u56fd,IdNo:a,\""BirthDt\"":\""1990-10-10\"",\""Leader\"":0,\""ResOnly\"":0,\""Mail\"":\""aa@a.com\"",\""Tel\"":\""010888888\"",\""Bnfs\"":\""ss\""}]";
Regex re = new Regex(@"({|""|,)(?<key>[^,}]*)");
MatchCollection mc = re.Matches(str);
foreach (Match m in mc)
Console.WriteLine(m.Groups["key"].Value.Replace (@"\""",""));
}
}
}

Name:\\u4e2d\\u56fd
IdNo:a
BirthDt:1990-10-10
Leader:0
ResOnly:0
Mail:aa@a.com
Tel:010888888
Bnfs:ss
Press any key to continue . . .
stevenhzj 2009-11-16
  • 打赏
  • 举报
回复
谢谢楼上各位
能处理这个不?
主要是这个地方:\\u4e2d\\u56fd 转化为变为:\u4e2d\u56fd
十八道胡同 2009-11-16
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = @"[{Name:\\u4e2d\\u56fd,IdNo:a,\""BirthDt\"":\""1990-10-10\"",\""Leader\"":0,\""ResOnly\"":0,\""Mail\"":\""aa@a.com\"",\""Tel\"":\""010888888\"",\""Bnfs\"":\""ss\""}]";
str=str.Replace("[","");
str = str.Replace("}", "");
str = str.Replace("]", "");
str = str.Replace("{", "");
str = str.Replace(@"\""", "");
string[] s = str.Split(',');
foreach (string ss in s)
{
Console.WriteLine(ss);
}
}
}
}

Name:\\u4e2d\\u56fd
IdNo:a
BirthDt:1990-10-10
Leader:0
ResOnly:0
Mail:aa@a.com
Tel:010888888
Bnfs:ss
Press any key to continue . . .
herotang101 2009-11-16
  • 打赏
  • 举报
回复
用replace("\","")和split(",")就可以了
但你字符串怎么这么多双引号?
肯定要先处理掉才能写代码

stevenhzj 2009-11-16
  • 打赏
  • 举报
回复
主要是这个地方:\\u4e2d 要变为:\u4e2d
mbh0210 2009-11-16
  • 打赏
  • 举报
回复
有专门解析json对象的组件的,Newtonsoft.Json
搜搜看
lee_b 2009-11-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 alifriend 的回复:]
先把括号replace掉,然后用spilt函数直接以冒号分割放到数组里吧
[/Quote]
应该是以逗号分割吧???呵。。
Jack2013tong 2009-11-16
  • 打赏
  • 举报
回复
用split(',')就可以了吧
波导终结者 2009-11-16
  • 打赏
  • 举报
回复
先把括号replace掉,然后用spilt函数直接以冒号分割放到数组里吧

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧