【发布时间】:2010-06-30 18:48:53
【问题描述】:
我有下面的代码。
string content = twitterMsg.text; 行正在为 twitterMsg 创建错误“使用未分配的局部变量”。我似乎无法访问我的 DataContractJsonSerializer<TwitterMain> 集合中的 TwitterSearchResponse.results.text 字段。
TwitterSearchResponse.results 是一个数组(一组对象属性),其中包含多个字符串字段,名称为text 和user_info。
有人可以帮忙吗?
更新代码如下。我仍然很困惑为什么我不能正确地迭代我的TwitterSearchResponse.results 并分配content = twitterMsg.text
对于它的价值,这是我的 DataContractJsonSerializer 方法:
String url = String.Format("http://search.twitter.com/search.json?q={0}&rpp=20", Server.UrlEncode(txtSearchFor.Text));
// parse the JSON data
using (MemoryStream ms = new MemoryStream(wc.DownloadData(url)))
{
DataContractJsonSerializer jsonSerializer =
new DataContractJsonSerializer(typeof(TwitterMain));
TwitterSearchResponse = jsonSerializer.ReadObject(ms) as TwitterMain; // read as JSON and map as TwitterOut
}
这是问题所在的原始发布代码。
public List<MatchCollection> returnMatches(DataContractJsonSerializer<TwitterMain> TwitterSearchResponse)
{
List<MatchCollection> messageLinks = new List<MatchCollection>();
foreach (TwitterResult twitterMsg in TwitterSearchResponse.results)
{
string content = twitterMsg.text;
// capture internet protocol pre-fixed words from message
string pattern = @"...";
messageLinks.Add(Regex.Matches(content, pattern, RegexOptions.IgnoreCase));
// capture @username twitter users from message
string atUsernamePattern = @"@([a-zA-Z0-9-_]+)";
MatchCollection PeopleMatches = Regex.Matches(content, atUsernamePattern, RegexOptions.IgnoreCase);
}
return messageLinks;
}
【问题讨论】:
-
我已经删除了我的 RegEx
pattern,因为它使 SO 行为异常(不让我发帖!)
标签: c# asp.net .net datacontractjsonserializer