J'ai le code ci-dessous.Accès aux propriétés DataContractJsonSerializer <T> via la boucle foreach re-factorisée
La ligne string content = twitterMsg.text;
crée l'erreur 'Utilisation de variable locale non affectée' pour twitterMsg. Je ne parviens pas à accéder à mes champs TwitterSearchResponse.results.text
dans ma collection DataContractJsonSerializer<TwitterMain>
.
TwitterSearchResponse.results
TwitterSearchResponse.results
est un tableau (ensemble de propriétés d'objet) avec plusieurs champs de type chaîne associés à des noms tels que text
et user_info
.
Quelqu'un peut-il aider avec cela ??
Mise à jour du code ci-dessous. Je suis encore très confus au sujet de la raison pour laquelle je ne suis pas en mesure de parcourir mon TwitterSearchResponse.results
correctement et affecter content = twitterMsg.text
Pour ce que ça vaut la peine, voici ma méthode 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
}
Et voici le code affiché original où le problème réside.
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;
}
J'ai supprimé mon motif «RegEx» car il faisait SO se comporter étrangement (ne me laisserais pas poster!) – Alex