当前位置: 代码迷 >> C# >> 用c#解析一个字符串,掏出一段数据
  详细解决方案

用c#解析一个字符串,掏出一段数据

热度:150   发布时间:2016-04-28 08:41:48.0
用c#解析一个字符串,取出一段数据
string json = "{
"Alias": "[email protected]",
"Name": "qwe",
"Gender": 1,
"SlaveList": "[email protected],[email protected]",
"Position": "xx",
"Tel": "",
"Mobile": "12178879669",
"ExtId": "",
"PartyList": {
"Count": 1,
"List": [{
"Value": ""
}
]
}"

本人要从以上这个字符串中取出"[email protected],[email protected]",求大神帮忙,很急,用c#
------解决思路----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = @"{
                ""Alias"": ""[email protected]"",
                ""Name"": ""qwe"",
                ""Gender"": 1,
                ""SlaveList"": ""[email protected],[email protected]"",
                ""Position"": ""xx"",
                ""Tel"": """",
                ""Mobile"": ""12178879669"",
                ""ExtId"": """",
                ""PartyList"": {
                    ""Count"": 1,
                    ""List"": [{
                    ""Value"": """"
                    }]
                  }
                }";

            var obj = JsonConvert.DeserializeObject<dynamic>(json);
            Console.WriteLine(obj.SlaveList);
            Console.ReadKey();
        }
    }
}

------解决思路----------------------
string json = @"{
            ""Alias"": ""[email protected]"",
            ""Name"": ""qwe"",
            ""Gender"": 1,
            ""SlaveList"": ""[email protected],[email protected]"",
            ""Position"": ""xx"",
            ""Tel"": """",
            ""Mobile"": ""12178879669"",
            ""ExtId"": """",
            ""PartyList"": {
                ""Count"": 1,
                ""List"": [{
                ""Value"": """"
                }]
                }
            }";
string pattern = @"(?<=""SlaveList"":\s*"")[^""]*";
foreach (Match m in Regex.Matches(json, pattern))
{
    Console.WriteLine(m.Value);
}

如果就你这点的话,不用反序列化,直接正则也可以
  相关解决方案