【问题标题】:How to retrieve values from json data response using regex regulations?如何使用正则表达式规则从 json 数据响应中检索值?
【发布时间】:2016-06-29 00:06:32
【问题描述】:

根据我从回复中收到的内容,我很难阅读我的正则表达式匹配。任何人都可以指导我正则表达式如何适用于以下 json 格式?

 HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
            myReq.ContentType = "application/json";

            // here's how to set response content type:
            Response.ContentType = "application/json"; // that's all

            var response = (HttpWebResponse)myReq.GetResponse();
            string text;

            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                text = sr.ReadToEnd();
            }

            MatchCollection nameCollection = Regex.Matches(text, @"<_id>\s*(.+?)\s*</_id>", RegexOptions.Singleline);
            MatchCollection postalCollection = Regex.Matches(text, @"<postal_code>\s*(.+?)\s*</postal_code>", RegexOptions.Singleline)
            MatchCollection schoolCollection= Regex.Matches(text, @"<all_schools_2016>\s*(.+?)\s*</postal_code>,", RegexOptions.Singleline);

文字

{“帮助”: "https://data.gov.sg/api/3/action/help_show?name=datastore_search", “成功”:真,“结果”:{“resource_id”: “36e6b5fc-9acc-4344-8f5f-5f67d52c525f”,“字段”:[{“类型”:“int4”, “id”:“_id”},{“type”:“text”,“id”:“all_schools_2016”},{“type”: “文本”,“id”:“地址”},{“type”:“numeric”,“id”:“postal_code”}], “记录”:[{“_id”:1,“邮政编码”:“738907”,“all_schools_2016”: “海军部小学”,“地址”:“11 WOODLANDS CIRCLE”},{“_id”: 2、“postal_code”:“737916”、“all_schools_2016”:“ADMIRALTY SECONDARY 学校”,“地址”:“31 WOODLANDS CRESCENT”},{“_id”:3, “postal_code”:“768643”、“all_schools_2016”:“AHMAD IBRAHIM PRIMARY SCHOOL", "address": "10 YISHUN STREET 11"}, {"_id": 4, "postal_code": "768928", "all_schools_2016": "艾哈迈德·易卜拉欣中学", "address": "751 YISHUN AVENUE 7"}, {"_id": 5, "postal_code": "579646", "all_schools_2016": "AI TONG SCHOOL", "address": "100 Bright Hill 驱动器"}], "_links": {"start": "/api/action/datastore_search?limit=5&resource_id=36e6b5fc-9acc-4344-8f5f-5f67d52c525f", “下一个”: "/api/action/datastore_search?offset=5&limit=5&resource_id=36e6b5fc-9acc-4344-8f5f-5f67d52c525f"}, “限制”:5,“总计”:367}}

【问题讨论】:

  • JSON 不是 HTML,但用 RegEx 解析它仍然是个坏主意。
  • 你为什么用Regex解析JSON?你可以直接解析那些值。
  • 什么意思? url 只给了我需要的部分数据,因此我使用这种方法来检索数据
  • 为什么不使用 Json.NET 和 SelectToken? newtonsoft.com/json/help/html/SelectToken.htm
  • 你得到了什么结果,你想得到什么结果。请阅读minimal reproducible example,然后更新您的问题。

标签: c# json regex


【解决方案1】:

为什么你需要一个正则表达式来完成这个任务?

使用JSON.NET

string json = "...";
dynamic data = JsonConvert.DeserializeObject(json);
if((bool)data.success)
{
     IEnumerable<dynamic> records = data.result.records;

     IEnumerable<int> names = records.Select(r => (int)r._id);
     IEnumerable<string> postal = records.Select(r => (string)r.postal_code);
     IEnumerable<string> schools = records.Select(r => (string)r.all_schools_2016);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-01
    • 2020-08-04
    • 2016-11-18
    • 2019-09-29
    • 2021-12-06
    • 2023-03-28
    • 2012-08-25
    相关资源
    最近更新 更多