[英]How to retrieve values from json data response using regex regulations?
根据我从回复中收到的信息,我很难阅读我的正则表达式匹配项。 任何人都可以指导我正则表达式如何用于以下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 ”,“成功”:true,“结果”:{“ resource_id”:“ 36e6b5fc-9acc-4344 -8f5f-5f67d52c525f“,”字段“:[{”类型“:” int4“,” id“:” _ id“},{”类型“:”文本“,” id“:” all_schools_2016“},{”类型“:” text“,” id“:”地址“},{” type“:”数字“,” id“:” postal_code“}},”记录“:[{” _id“:1,” postal_code“: “ 738907”,“ all_schools_2016”:“ ADMIRALTY小学”,“地址”:“ 11 WOODLANDS CIRCLE”},{“ _id”:2,“邮政编码”:“ 737916”,“ all_schools_2016”:“ ADMIRALTY中学”, “ address”:“ 31 WOODLANDS CRESCENT”},{“ _id”:3,“ postal_code”:“ 768643”,“ all_schools_2016”:“ AHMAD IBRAHIM小学”,“地址”:“ 10 YISHUN STREET 11”},{ “ _id”:4,“邮政编码”:“ 768928”,“ all_schools_2016”:“ AHMAD IBRAHIM中学”,“地址”:“ 751 YISHUN AVENUE 7”},{“ _ id”:5,“邮政编码”:“ 579646” “,” all_schools_2016“:” AI TONG SCHOOL“,”地址“:” 100 Bright Hill Drive“}},” _links“:{”开始“:” / api / action / datastore_search?limit = 5&re source_id = 36e6b5fc-9acc-4344-8f5f-5f67d52c525f“,” next“:” / api / action / datastore_search?offset = 5&limit = 5&resource_id = 36e6b5fc-9acc-4344-8f5f-5f67d52c525f“},” limit“:5,”总计”:367}}
为什么要为此任务使用正则表达式?
使用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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.