【发布时间】: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,然后更新您的问题。